Skip to content

Commit

Permalink
add drop-database task
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-iohk committed Mar 14, 2024
1 parent 2e7549b commit 6004469
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tasks.py
Original file line number Diff line number Diff line change
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()

0 comments on commit 6004469

Please sign in to comment.