-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.py
30 lines (27 loc) · 1.08 KB
/
migrate.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
import inspect
from app import my_models
from aziktools.db.connection import db
from aziktools.db.models import Objects
for name, CLASS in inspect.getmembers(my_models):
if inspect.isclass(CLASS):
if 'my_models' in str(CLASS):
model = CLASS()
table_name = 'main_' + type(model).__name__.lower()
# print(table_name)
sql = f"""CREATE TABLE \"{table_name}\"(id serial PRIMARY KEY"""
attrs = inspect.getmembers(CLASS)
model_attrs = attrs[2][1]
for i in model_attrs:
if '__' not in i:
# print(i)
try:
attr = getattr(model, i)
sql += f""", {i} {attr.make_sql_field()}"""
except AttributeError:
pass
sql += ")"
existing_table_deletion_sql = f"""DROP TABLE IF EXISTS {table_name} """
db.cursor.execute(existing_table_deletion_sql)
db.connection.commit()
db.cursor.execute(sql)
db.connection.commit()