forked from abhimana2003/BigDataNutritionAgent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_db.py
More file actions
61 lines (54 loc) · 2.46 KB
/
Copy pathinit_db.py
File metadata and controls
61 lines (54 loc) · 2.46 KB
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
from sqlalchemy import text
from sqlalchemy.exc import ProgrammingError
from database import engine
from models import Base
Base.metadata.create_all(bind=engine)
def run_migrations() -> None:
try:
with engine.begin() as conn:
conn.execute(text("ALTER TABLE user_profiles ADD COLUMN IF NOT EXISTS username VARCHAR"))
conn.execute(text("ALTER TABLE user_profiles ADD COLUMN IF NOT EXISTS email VARCHAR"))
conn.execute(text("ALTER TABLE user_profiles ADD COLUMN IF NOT EXISTS full_name VARCHAR"))
conn.execute(text("ALTER TABLE user_profiles ADD COLUMN IF NOT EXISTS password_hash VARCHAR"))
conn.execute(text("ALTER TABLE user_profiles ADD COLUMN IF NOT EXISTS password_salt VARCHAR"))
conn.execute(
text(
"UPDATE user_profiles "
"SET username = 'user_' || id "
"WHERE username IS NULL OR username = ''"
)
)
conn.execute(
text(
"UPDATE user_profiles "
"SET goal = 'maintenance' "
"WHERE goal IS NULL OR goal NOT IN ('weight loss', 'maintenance', 'high protein')"
)
)
conn.execute(
text(
"UPDATE user_profiles "
"SET cooking_time = 'medium (30-60 min)' "
"WHERE cooking_time IS NULL OR cooking_time NOT IN "
"('short (<30 mins)', 'medium (30-60 min)', 'long (>60 mins)')"
)
)
conn.execute(
text(
"CREATE UNIQUE INDEX IF NOT EXISTS ix_user_profiles_username "
"ON user_profiles (username)"
)
)
conn.execute(text("ALTER TABLE user_profiles ALTER COLUMN username SET NOT NULL"))
conn.execute(
text(
"CREATE UNIQUE INDEX IF NOT EXISTS ix_user_profiles_email "
"ON user_profiles (email)"
)
)
conn.execute(text("ALTER TABLE recipes ADD COLUMN IF NOT EXISTS username VARCHAR"))
conn.execute(text("ALTER TABLE meal_plan_history ADD COLUMN IF NOT EXISTS meal_plan JSON"))
except ProgrammingError:
# Some environments connect with a non-owner DB user; run_project.sh applies the same SQL via psql owner context.
pass
run_migrations()