Skip to content

Commit

Permalink
Merge pull request #39 from MinaFoundation/idempotent-create-database
Browse files Browse the repository at this point in the history
PM-1322 Idempotent create database
  • Loading branch information
piotr-iohk authored Mar 14, 2024
2 parents de8c9b9 + 89861dd commit 236d3b7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**:

Expand All @@ -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:
Expand Down
34 changes: 30 additions & 4 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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);

0 comments on commit 236d3b7

Please sign in to comment.