Spaces:
Running
Running
Sync from GitHub
Browse files
app/db.py
CHANGED
|
@@ -115,10 +115,42 @@ def get_db() -> Generator[Session, None, None]:
|
|
| 115 |
session.close()
|
| 116 |
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
def init_db():
|
| 119 |
"""
|
| 120 |
Initialize the database - create all tables.
|
| 121 |
Safe to call multiple times (uses CREATE IF NOT EXISTS).
|
|
|
|
| 122 |
"""
|
| 123 |
# Import models to register them with Base
|
| 124 |
from app import models # noqa: F401
|
|
@@ -126,6 +158,9 @@ def init_db():
|
|
| 126 |
engine = get_engine()
|
| 127 |
Base.metadata.create_all(bind=engine)
|
| 128 |
logger.info("Database tables created/verified")
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
|
| 131 |
def get_db_type() -> str:
|
|
|
|
| 115 |
session.close()
|
| 116 |
|
| 117 |
|
| 118 |
+
def _run_migrations(engine):
|
| 119 |
+
"""
|
| 120 |
+
Run necessary database migrations for schema changes.
|
| 121 |
+
These are safe to run multiple times (idempotent).
|
| 122 |
+
"""
|
| 123 |
+
settings = get_settings()
|
| 124 |
+
is_sqlite = settings.database_url.startswith("sqlite")
|
| 125 |
+
|
| 126 |
+
with engine.connect() as conn:
|
| 127 |
+
# Migration: Add ai_stance column to ai_commentaries if it doesn't exist
|
| 128 |
+
try:
|
| 129 |
+
if is_sqlite:
|
| 130 |
+
# SQLite doesn't support IF NOT EXISTS for columns, check manually
|
| 131 |
+
result = conn.execute(text("PRAGMA table_info(ai_commentaries)"))
|
| 132 |
+
columns = [row[1] for row in result.fetchall()]
|
| 133 |
+
if 'ai_stance' not in columns:
|
| 134 |
+
conn.execute(text("ALTER TABLE ai_commentaries ADD COLUMN ai_stance VARCHAR(20) DEFAULT 'NEUTRAL'"))
|
| 135 |
+
conn.commit()
|
| 136 |
+
logger.info("Migration: Added ai_stance column to ai_commentaries")
|
| 137 |
+
else:
|
| 138 |
+
# PostgreSQL supports IF NOT EXISTS via DO block
|
| 139 |
+
conn.execute(text("""
|
| 140 |
+
ALTER TABLE ai_commentaries
|
| 141 |
+
ADD COLUMN IF NOT EXISTS ai_stance VARCHAR(20) DEFAULT 'NEUTRAL'
|
| 142 |
+
"""))
|
| 143 |
+
conn.commit()
|
| 144 |
+
logger.info("Migration: Ensured ai_stance column exists in ai_commentaries")
|
| 145 |
+
except Exception as e:
|
| 146 |
+
logger.debug(f"Migration check for ai_stance: {e}")
|
| 147 |
+
|
| 148 |
+
|
| 149 |
def init_db():
|
| 150 |
"""
|
| 151 |
Initialize the database - create all tables.
|
| 152 |
Safe to call multiple times (uses CREATE IF NOT EXISTS).
|
| 153 |
+
Also runs any necessary migrations for schema changes.
|
| 154 |
"""
|
| 155 |
# Import models to register them with Base
|
| 156 |
from app import models # noqa: F401
|
|
|
|
| 158 |
engine = get_engine()
|
| 159 |
Base.metadata.create_all(bind=engine)
|
| 160 |
logger.info("Database tables created/verified")
|
| 161 |
+
|
| 162 |
+
# Run migrations for existing tables
|
| 163 |
+
_run_migrations(engine)
|
| 164 |
|
| 165 |
|
| 166 |
def get_db_type() -> str:
|