-
Notifications
You must be signed in to change notification settings - Fork 0
/
createdb.py
54 lines (38 loc) · 1.5 KB
/
createdb.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
import click
from alembic import command
from alembic.config import Config
from sqlalchemy_utils import drop_database, create_database
from MyAppRest.settings import DevConfig, ProdConfig
from common.models.base import db
from MyAppRest.app import create_app
class ProdMigrateConfig(ProdConfig):
"""Production migration configuration."""
SQLALCHEMY_DATABASE_URI = ""
@click.command()
@click.option('--migrate', help='Migrate database', is_flag=True)
@click.option('--PROD', help='Production environment', is_flag=True)
@click.option('--drop', help='Drop database', is_flag=True)
@click.option('--create', help='Create database', is_flag=True)
@click.option('--dummy', help='Insert dummy data', is_flag=True)
def recreate(drop, create, dummy, migrate, prod):
app = create_app(ProdMigrateConfig if prod else DevConfig)
app.app_context().push()
alembic_cfg = Config("alembic.ini")
if migrate:
command.upgrade(alembic_cfg, "head")
if prod:
return # Prod safety catch so stuff doesn't get deleted
if drop:
db.drop_all()
with db.engine.connect().execution_options(autocommit=True) as conn:
conn.execute('DROP TABLE IF EXISTS alembic_version;')
if create:
db.create_all()
command.stamp(alembic_cfg, "head")
if dummy:
devdb_sql = open("devdb.sql").read()
with db.engine.connect().execution_options(autocommit=True) as conn:
conn.execute(devdb_sql)
if __name__ == '__main__':
recreate()