-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_db.py
86 lines (58 loc) · 2.23 KB
/
run_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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import sys
import run_db_data
import run_db_mutator
from db.database import SQLALCHEMY_DATABASE_URL, engine
class Command:
def __init__(self, action, does=""):
self.action = action
self.does = does
def connection_string():
print("Db Connection String: \t" + SQLALCHEMY_DATABASE_URL)
print(engine.pool.status())
def close():
print(engine.dispose())
def data():
run_db_data.add_master_data()
def auto():
run_db_mutator.drop_db_version()
run_db_mutator.remove_local_migrations()
run_db_mutator.create_migration()
run_db_mutator.apply_migations()
data()
def scratch():
run_db_mutator.drop_all()
run_db_mutator.drop_db_version()
def help():
for name, value in switch().items():
# print(name +"\t \t" +value.does)
print("{0:20} {1}".format(name, value.does))
def unknown():
print("Unknown command argument, see 'python run_db.py help'")
def has_model_changes():
print(run_db_mutator.has_model_changes())
def add_migration_and_update():
if run_db_mutator.has_model_changes():
run_db_mutator.create_migration()
run_db_mutator.apply_migations()
def switch():
switcher = {
"connection": Command(connection_string, "show current connection string"),
"close": Command(close, "close all connection"),
"drop": Command(run_db_mutator.scratch, "drop alembic_version table and all alembic created objects"),
"auto": Command(auto,
"drop alembic_version table, remove migration files, create migration & update db, add master datas"),
"data": Command(data, "add master datas"),
"remove-version-files": Command(run_db_mutator.remove_local_migrations, "remove local migration files"),
"drop-version-table": Command(run_db_mutator.drop_db_version, "drop alembic_version table"),
"update": Command(run_db_mutator.apply_migations, "apply current migraion files to db"),
"help": Command(help, "show avaiable commands")
}
return switcher
if __name__ == "__main__":
command = ""
if len(sys.argv) > 1:
command = sys.argv[1].lower()
switcher = switch()
item = switcher.get(command, Command(unknown, ""))
print(item.does)
item.action()