-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_db.py
38 lines (28 loc) · 1.11 KB
/
create_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from config import Config
from os import getenv
import application
from boto3 import client
from sqlalchemy import text
APP_NAME = "backend"
ssm_client = client('ssm', region_name=getenv("AWS_REGION", 'us-east-1'))
app = application.create_app()
print("Starting DB creation")
def get_param(param_name, decrypt=True):
try:
response = ssm_client.get_parameter(
Name=f'/{getenv("ENVIRONMENT", "dev")}/{APP_NAME}/{param_name}',
WithDecryption=decrypt
)
return response.get("Parameter").get("Value")
except ssm_client.exceptions.ParameterNotFound:
return ""
app.config["SQLALCHEMY_DATABASE_URI"] = get_param("SQLALCHEMY_DATABASE_URI") if getenv("ENVIRONMENT") is not None else getenv("SQLALCHEMY_DATABASE_URI")
extensions_query = text('CREATE EXTENSION if not EXISTS cube; CREATE EXTENSION if not EXISTS earthdistance;')
with app.app_context():
from application.database import models
models.db.create_all()
# add extensions for GIS data
# Only done in local, RDS requires superuser to do this
if getenv("ENVIRONMENT") is None:
models.db.engine.execute(extensions_query)
print("Finished")