-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_database.py
More file actions
93 lines (74 loc) · 3.61 KB
/
migrate_database.py
File metadata and controls
93 lines (74 loc) · 3.61 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
Database migration script to add new columns and tables.
"""
import sys
from pathlib import Path
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))
from sqlalchemy import text, inspect
from app.db import engine, Base
from app.models import Document, Transaction, DocumentCorrection, User, ChatSession
def _has_column(inspector, table: str, column: str) -> bool:
try:
columns = inspector.get_columns(table)
return column in [c["name"] for c in columns]
except Exception:
return False
def _table_exists(inspector, table: str) -> bool:
return table in inspector.get_table_names()
def migrate_database():
"""Run database migrations."""
print("Running database migrations...")
inspector = inspect(engine)
with engine.connect() as conn:
# --- Transactions: legacy columns ---
if _has_column(inspector, "transactions", "id"):
txn_cols = [c["name"] for c in inspector.get_columns("transactions")]
if "confidence_score" not in txn_cols:
print(" Adding confidence_score to transactions")
conn.execute(text("ALTER TABLE transactions ADD COLUMN confidence_score REAL"))
conn.commit()
if "is_corrected" not in txn_cols:
print(" Adding is_corrected to transactions")
conn.execute(text("ALTER TABLE transactions ADD COLUMN is_corrected INTEGER DEFAULT 0"))
conn.commit()
if "user_id" not in txn_cols:
print(" Adding user_id to transactions")
conn.execute(text("ALTER TABLE transactions ADD COLUMN user_id INTEGER"))
conn.commit()
# --- Documents: user_id ---
if _has_column(inspector, "documents", "id"):
doc_cols = [c["name"] for c in inspector.get_columns("documents")]
if "user_id" not in doc_cols:
print(" Adding user_id to documents")
conn.execute(text("ALTER TABLE documents ADD COLUMN user_id INTEGER"))
conn.commit()
# --- DocumentCorrections: user_id ---
if _table_exists(inspector, "document_corrections"):
dc_cols = [c["name"] for c in inspector.get_columns("document_corrections")]
if "user_id" not in dc_cols:
print(" Adding user_id to document_corrections")
conn.execute(text("ALTER TABLE document_corrections ADD COLUMN user_id INTEGER"))
conn.commit()
else:
print(" Creating document_corrections table")
Base.metadata.create_all(bind=engine, tables=[DocumentCorrection.__table__])
# --- Users table ---
if not _table_exists(inspector, "users"):
print(" Creating users table")
Base.metadata.create_all(bind=engine, tables=[User.__table__])
# --- ChatSessions table ---
if not _table_exists(inspector, "chat_sessions"):
print(" Creating chat_sessions table")
Base.metadata.create_all(bind=engine, tables=[ChatSession.__table__])
# --- Indexes ---
try:
conn.execute(text("CREATE INDEX IF NOT EXISTS idx_transactions_category ON transactions(category)"))
conn.execute(text("CREATE INDEX IF NOT EXISTS idx_transactions_user_id ON transactions(user_id)"))
conn.execute(text("CREATE INDEX IF NOT EXISTS idx_documents_user_id ON documents(user_id)"))
conn.commit()
except Exception as e:
print(f" Index creation note: {e}")
print("Migration complete!")
if __name__ == "__main__":
migrate_database()