diff --git a/README.md b/README.md index ed422d4..56b9a5c 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ Create a database and relevant tables before first-time program execution using invoke create-database ``` - > :warning: This script drops all existing tables and creates new ones. + > **Note:** The script creates `POSTGRES_DB` if not exists and applies `./database/create_tables.sql` script. 2. **Initialize `bot_logs` table**: @@ -106,6 +106,12 @@ Create a database and relevant tables before first-time program execution using invoke init-database --mins-ago 20 ``` +It is possible to drop database using: + +``` +invoke drop-database +``` + ### AWS Keyspaces/Cassandra Configuration To connect to AWS Keyspaces/Cassandra, the following environment variables need to be set: diff --git a/tasks.py b/tasks.py index 99a36e8..80844fa 100644 --- a/tasks.py +++ b/tasks.py @@ -23,9 +23,9 @@ def create_database(ctx): # Creating the database try: cursor.execute(sql.SQL("CREATE DATABASE {}").format(sql.Identifier(db_name))) - print(f"Database {db_name} created successfully") + print(f"Database '{db_name}' created successfully") except psycopg2.errors.DuplicateDatabase: - print(f"Database {db_name} already exists") + print(f"Database '{db_name}' already exists, not creating") cursor.close() conn.close() @@ -38,13 +38,13 @@ def create_database(ctx): cursor = conn.cursor() # Path to the SQL script relative to tasks.py - sql_script_path = "uptime_service_validation/database/createDB.sql" + sql_script_path = "uptime_service_validation/database/create_tables.sql" # Running the SQL script file with open(sql_script_path, "r") as file: sql_script = file.read() cursor.execute(sql_script) - print("Database setup completed successfully") + print("'create_tables.sql' script completed successfully") cursor.close() conn.close() @@ -106,3 +106,29 @@ def init_database(ctx, batch_end_epoch=None, mins_ago=None, override_empty=False conn.commit() cursor.close() conn.close() + + +@task +def drop_database(ctx): + db_host = os.environ.get("POSTGRES_HOST") + db_port = os.environ.get("POSTGRES_PORT") + db_name = os.environ.get("POSTGRES_DB") + db_user = os.environ.get("POSTGRES_USER") + db_password = os.environ.get("POSTGRES_PASSWORD") + + # Establishing connection to PostgreSQL server + conn = psycopg2.connect( + host=db_host, port=db_port, user=db_user, password=db_password + ) + conn.autocommit = True + cursor = conn.cursor() + + # Dropping the database + try: + cursor.execute(sql.SQL("DROP DATABASE {}").format(sql.Identifier(db_name))) + print(f"Database '{db_name}' dropped!") + except Exception as e: + print(f"Error dropping database '{db_name}'! Error: {e}") + + cursor.close() + conn.close() diff --git a/uptime_service_validation/database/createDB.sql b/uptime_service_validation/database/create_tables.sql similarity index 67% rename from uptime_service_validation/database/createDB.sql rename to uptime_service_validation/database/create_tables.sql index 599a63b..dc2645c 100644 --- a/uptime_service_validation/database/createDB.sql +++ b/uptime_service_validation/database/create_tables.sql @@ -1,6 +1,4 @@ ---Need to check on this structure. Feels like some of these should be non-null. -DROP TABLE IF EXISTS bot_logs CASCADE; -CREATE TABLE bot_logs ( +CREATE TABLE IF NOT EXISTS bot_logs ( id SERIAL PRIMARY KEY, processing_time DOUBLE PRECISION, files_processed INT, @@ -9,15 +7,13 @@ CREATE TABLE bot_logs ( batch_end_epoch BIGINT ); -DROP TABLE IF EXISTS statehash CASCADE; -CREATE TABLE statehash ( +CREATE TABLE IF NOT EXISTS statehash ( id SERIAL PRIMARY KEY, value TEXT ); -CREATE UNIQUE INDEX uq_state_hash ON statehash USING btree (value); +CREATE UNIQUE INDEX IF NOT EXISTS uq_state_hash ON statehash USING btree (value); -DROP TABLE IF EXISTS bot_logs_statehash CASCADE; -CREATE TABLE bot_logs_statehash ( +CREATE TABLE IF NOT EXISTS bot_logs_statehash ( id SERIAL PRIMARY KEY, parent_statehash_id INT, statehash_id INT, @@ -34,8 +30,7 @@ CREATE TABLE bot_logs_statehash ( REFERENCES bot_logs(id) ); -DROP TABLE IF EXISTS nodes CASCADE; -CREATE TABLE nodes ( +CREATE TABLE IF NOT EXISTS nodes ( id SERIAL PRIMARY KEY, block_producer_key TEXT, updated_at TIMESTAMPTZ(6), @@ -46,8 +41,7 @@ CREATE TABLE nodes ( application_status BOOLEAN ); -DROP TABLE IF EXISTS points CASCADE; -CREATE TABLE points ( +CREATE TABLE IF NOT EXISTS points ( id SERIAL PRIMARY KEY, file_name TEXT, file_timestamps TIMESTAMPTZ(6), @@ -69,8 +63,7 @@ CREATE TABLE points ( REFERENCES statehash(id) ); -DROP TABLE IF EXISTS score_history CASCADE; -CREATE TABLE score_history ( +CREATE TABLE IF NOT EXISTS score_history ( id SERIAL PRIMARY KEY, node_id INT, score_at TIMESTAMP(6), @@ -80,6 +73,4 @@ CREATE TABLE score_history ( FOREIGN KEY(node_id) REFERENCES nodes(id) ); -CREATE UNIQUE INDEX uq_sh_node_score_at ON score_history USING btree (node_id, score_at); - --- Point Summary table that is auto-gen? +CREATE UNIQUE INDEX IF NOT EXISTS uq_sh_node_score_at ON score_history USING btree (node_id, score_at); \ No newline at end of file