aboutsummaryrefslogtreecommitdiffstats
path: root/create_metricity_db.py
diff options
context:
space:
mode:
authorGravatar Chris Lovering <[email protected]>2021-09-09 17:02:51 +0100
committerGravatar Chris Lovering <[email protected]>2021-09-09 17:42:47 +0100
commit9942e1598e31ba9047ab5cba4bda29a0535befbe (patch)
tree83ec628d5323871c347e473412c20bffc41b34e5 /create_metricity_db.py
parentAdd a docker compose for local dev (diff)
Add a script to create metricity db if it doesn't exist
Diffstat (limited to 'create_metricity_db.py')
-rw-r--r--create_metricity_db.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/create_metricity_db.py b/create_metricity_db.py
new file mode 100644
index 0000000..edec2db
--- /dev/null
+++ b/create_metricity_db.py
@@ -0,0 +1,46 @@
+"""Ensures the metricity db exists before running migrations."""
+import os
+from urllib.parse import SplitResult, urlsplit
+
+import psycopg2
+from psycopg2 import sql
+from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
+
+
+def parse_db_url(db_url: str) -> SplitResult:
+ """Validate and split the given databse url."""
+ db_url_parts = urlsplit(db_url)
+ if not all((
+ db_url_parts.hostname,
+ db_url_parts.username,
+ db_url_parts.password
+ )):
+ raise ValueError(
+ "The given db_url is not a valid PostgreSQL database URL."
+ )
+ return db_url_parts
+
+
+if __name__ == "__main__":
+ database_parts = parse_db_url(os.environ["DATABASE_URI"])
+
+ conn = psycopg2.connect(
+ host=database_parts.hostname,
+ port=database_parts.port,
+ user=database_parts.username,
+ password=database_parts.password
+ )
+
+ db_name = database_parts.path[1:] or "metricity"
+
+ # Required to create a database in a .execute() call
+ conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
+ with conn.cursor() as cursor:
+ cursor.execute("SELECT 1 FROM pg_catalog.pg_database WHERE datname = %s", (db_name,))
+ exists = cursor.fetchone()
+ if not exists:
+ print("Creating metricity database.")
+ cursor.execute(
+ sql.SQL("CREATE DATABASE {dbname}").format(dbname=sql.Identifier(db_name))
+ )
+ conn.close()