-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_security.py
More file actions
66 lines (57 loc) · 2.1 KB
/
Copy pathmigrate_security.py
File metadata and controls
66 lines (57 loc) · 2.1 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
62
63
64
65
66
"""
migrate_security.py — Ongeza columns mpya za security kwenye DB iliyopo
Run mara moja tu: python migrate_security.py
"""
from dotenv import load_dotenv
from sqlalchemy import text
from app import app, db
load_dotenv()
def run_migration():
with app.app_context():
conn = db.engine.connect()
migrations = [
# Ongeza accepted_terms column kwenye users
"""
DO $$ BEGIN
ALTER TABLE users ADD COLUMN IF NOT EXISTS accepted_terms BOOLEAN DEFAULT FALSE;
EXCEPTION WHEN duplicate_column THEN NULL;
END $$;
""",
# Ongeza terms_accepted_at column kwenye users
"""
DO $$ BEGIN
ALTER TABLE users ADD COLUMN IF NOT EXISTS terms_accepted_at TIMESTAMP;
EXCEPTION WHEN duplicate_column THEN NULL;
END $$;
""",
# Unda banned_emails table kama haipo
"""
CREATE TABLE IF NOT EXISTS banned_emails (
id SERIAL PRIMARY KEY,
email VARCHAR(120) UNIQUE,
phone VARCHAR(20) UNIQUE,
reason VARCHAR(300),
banned_at TIMESTAMP DEFAULT NOW(),
banned_by INTEGER REFERENCES users(id)
);
""",
# Index kwa speed
"""
CREATE INDEX IF NOT EXISTS idx_banned_email ON banned_emails(email);
CREATE INDEX IF NOT EXISTS idx_banned_phone ON banned_emails(phone);
""",
]
for sql in migrations:
try:
conn.execute(text(sql))
conn.commit()
print("✓ Migration OK")
except Exception as e:
print(f"✗ Migration error: {e}")
conn.rollback()
conn.close()
print("\n✓ Migrations zote zimekamilika!")
print(" Columns mpya: users.accepted_terms, users.terms_accepted_at")
print(" Table mpya: banned_emails")
if __name__ == "__main__":
run_migration()