Spaces:
Runtime error
Runtime error
Commit ·
ee882c1
1
Parent(s): c388752
Add idempotent light migrations so new columns (storage_path, etc.) apply to existing Postgres tables
Browse files- backend/database.py +26 -0
backend/database.py
CHANGED
|
@@ -11,12 +11,16 @@ from sqlalchemy import (
|
|
| 11 |
Text,
|
| 12 |
DateTime,
|
| 13 |
Float,
|
|
|
|
| 14 |
)
|
| 15 |
from sqlalchemy.ext.declarative import declarative_base
|
| 16 |
from sqlalchemy.orm import sessionmaker
|
| 17 |
from datetime import datetime
|
|
|
|
| 18 |
from config import settings
|
| 19 |
|
|
|
|
|
|
|
| 20 |
# Normalize the DB URL so SQLAlchemy uses the psycopg2 driver for Postgres
|
| 21 |
# (Supabase gives "postgresql://..."). SQLite stays as-is for local dev.
|
| 22 |
_db_url = settings.DATABASE_URL
|
|
@@ -99,8 +103,30 @@ class UploadedDocument(Base):
|
|
| 99 |
uploaded_at = Column(DateTime, default=datetime.utcnow)
|
| 100 |
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
def init_db():
|
| 103 |
Base.metadata.create_all(bind=engine)
|
|
|
|
| 104 |
|
| 105 |
|
| 106 |
def get_db():
|
|
|
|
| 11 |
Text,
|
| 12 |
DateTime,
|
| 13 |
Float,
|
| 14 |
+
text,
|
| 15 |
)
|
| 16 |
from sqlalchemy.ext.declarative import declarative_base
|
| 17 |
from sqlalchemy.orm import sessionmaker
|
| 18 |
from datetime import datetime
|
| 19 |
+
import logging
|
| 20 |
from config import settings
|
| 21 |
|
| 22 |
+
logger = logging.getLogger(__name__)
|
| 23 |
+
|
| 24 |
# Normalize the DB URL so SQLAlchemy uses the psycopg2 driver for Postgres
|
| 25 |
# (Supabase gives "postgresql://..."). SQLite stays as-is for local dev.
|
| 26 |
_db_url = settings.DATABASE_URL
|
|
|
|
| 103 |
uploaded_at = Column(DateTime, default=datetime.utcnow)
|
| 104 |
|
| 105 |
|
| 106 |
+
def _run_light_migrations():
|
| 107 |
+
"""Add columns that were introduced after a table was first created.
|
| 108 |
+
create_all() never ALTERs existing tables, so on a long-lived Postgres DB
|
| 109 |
+
we patch in new nullable columns idempotently."""
|
| 110 |
+
if not IS_POSTGRES:
|
| 111 |
+
return # local SQLite is recreated from the models anyway
|
| 112 |
+
stmts = [
|
| 113 |
+
"ALTER TABLE uploaded_documents ADD COLUMN IF NOT EXISTS storage_path VARCHAR",
|
| 114 |
+
"ALTER TABLE memory_entries ADD COLUMN IF NOT EXISTS client_id VARCHAR",
|
| 115 |
+
"ALTER TABLE memory_entries ADD COLUMN IF NOT EXISTS updated_at TIMESTAMP",
|
| 116 |
+
"ALTER TABLE chat_sessions ADD COLUMN IF NOT EXISTS client_id VARCHAR",
|
| 117 |
+
]
|
| 118 |
+
try:
|
| 119 |
+
with engine.begin() as conn:
|
| 120 |
+
for s in stmts:
|
| 121 |
+
conn.execute(text(s))
|
| 122 |
+
logger.info("✅ Light migrations applied")
|
| 123 |
+
except Exception as e:
|
| 124 |
+
logger.warning(f"Light migration skipped: {e}")
|
| 125 |
+
|
| 126 |
+
|
| 127 |
def init_db():
|
| 128 |
Base.metadata.create_all(bind=engine)
|
| 129 |
+
_run_light_migrations()
|
| 130 |
|
| 131 |
|
| 132 |
def get_db():
|