Create database with separate script.
Creating the ATAT database requires a separate connection to one of the default Postgres databases, like `postgres`. This updates the scripts and secrets-tool command to handle creating the database. It also removes database creation from Terraform and updates the documentation.
This commit is contained in:
41
script/create_database.py
Normal file
41
script/create_database.py
Normal file
@@ -0,0 +1,41 @@
|
||||
# Add root application dir to the python path
|
||||
import os
|
||||
import sys
|
||||
|
||||
parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
||||
sys.path.append(parent_dir)
|
||||
|
||||
import sqlalchemy
|
||||
|
||||
from atst.app import make_config
|
||||
|
||||
|
||||
def _root_connection(config, root_db):
|
||||
# Assemble DATABASE_URI value
|
||||
database_uri = "postgresql://{}:{}@{}:{}/{}".format( # pragma: allowlist secret
|
||||
config.get("PGUSER"),
|
||||
config.get("PGPASSWORD"),
|
||||
config.get("PGHOST"),
|
||||
config.get("PGPORT"),
|
||||
root_db,
|
||||
)
|
||||
engine = sqlalchemy.create_engine(database_uri)
|
||||
return engine.connect()
|
||||
|
||||
|
||||
def create_database(conn, dbname):
|
||||
conn.execute("commit")
|
||||
conn.execute(f"CREATE DATABASE {dbname};")
|
||||
conn.close()
|
||||
|
||||
return True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
dbname = sys.argv[1]
|
||||
config = make_config()
|
||||
|
||||
conn = _root_connection(config, "postgres")
|
||||
|
||||
print(f"Creating database {dbname}")
|
||||
create_database(conn, dbname)
|
Reference in New Issue
Block a user