Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9d15336144 |
No files matched your search
@@ -148,48 +148,3 @@ def execute_write(conn, query: str, params: tuple = ()) -> None:
|
|||||||
"""
|
"""
|
||||||
with get_cursor(conn) as cur:
|
with get_cursor(conn) as cur:
|
||||||
cur.execute(query, params)
|
cur.execute(query, params)
|
||||||
|
|
||||||
|
|
||||||
def init_db():
|
|
||||||
"""
|
|
||||||
Initialize database with required data.
|
|
||||||
|
|
||||||
Ensures critical data exists (e.g., pipeline master prompt).
|
|
||||||
Safe to call multiple times - checks before inserting.
|
|
||||||
Called automatically on app startup.
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
|
|
||||||
# Check if table exists first
|
|
||||||
cur.execute("""
|
|
||||||
SELECT EXISTS (
|
|
||||||
SELECT FROM information_schema.tables
|
|
||||||
WHERE table_schema = 'public'
|
|
||||||
AND table_name = 'ai_prompts'
|
|
||||||
) as table_exists
|
|
||||||
""")
|
|
||||||
if not cur.fetchone()['table_exists']:
|
|
||||||
print("⚠️ ai_prompts table doesn't exist yet - skipping pipeline prompt creation")
|
|
||||||
return
|
|
||||||
|
|
||||||
# Ensure "pipeline" master prompt exists
|
|
||||||
cur.execute("SELECT COUNT(*) as count FROM ai_prompts WHERE slug='pipeline'")
|
|
||||||
if cur.fetchone()['count'] == 0:
|
|
||||||
cur.execute("""
|
|
||||||
INSERT INTO ai_prompts (slug, name, description, template, active, sort_order)
|
|
||||||
VALUES (
|
|
||||||
'pipeline',
|
|
||||||
'Mehrstufige Gesamtanalyse',
|
|
||||||
'Master-Schalter für die gesamte Pipeline. Deaktiviere diese Analyse, um die Pipeline komplett zu verstecken.',
|
|
||||||
'PIPELINE_MASTER',
|
|
||||||
true,
|
|
||||||
-10
|
|
||||||
)
|
|
||||||
""")
|
|
||||||
conn.commit()
|
|
||||||
print("✓ Pipeline master prompt created")
|
|
||||||
except Exception as e:
|
|
||||||
print(f"⚠️ Could not create pipeline prompt: {e}")
|
|
||||||
# Don't fail startup - prompt can be created manually
|
|
||||||
+26
-6
@@ -15,7 +15,7 @@ from slowapi.util import get_remote_address
|
|||||||
from slowapi.errors import RateLimitExceeded
|
from slowapi.errors import RateLimitExceeded
|
||||||
from starlette.requests import Request
|
from starlette.requests import Request
|
||||||
|
|
||||||
from db import get_db, get_cursor, r2d, init_db
|
from db import get_db, get_cursor, r2d
|
||||||
|
|
||||||
DATA_DIR = Path(os.getenv("DATA_DIR", "./data"))
|
DATA_DIR = Path(os.getenv("DATA_DIR", "./data"))
|
||||||
PHOTOS_DIR = Path(os.getenv("PHOTOS_DIR", "./photos"))
|
PHOTOS_DIR = Path(os.getenv("PHOTOS_DIR", "./photos"))
|
||||||
@@ -43,11 +43,31 @@ AVATAR_COLORS = ['#1D9E75','#378ADD','#D85A30','#EF9F27','#7F77DD','#D4537E','#6
|
|||||||
@app.on_event("startup")
|
@app.on_event("startup")
|
||||||
async def startup_event():
|
async def startup_event():
|
||||||
"""Run migrations and initialization on startup."""
|
"""Run migrations and initialization on startup."""
|
||||||
try:
|
init_db()
|
||||||
init_db()
|
|
||||||
except Exception as e:
|
def init_db():
|
||||||
print(f"⚠️ init_db() failed (non-fatal): {e}")
|
"""Initialize database - Schema is loaded by startup.sh"""
|
||||||
# Don't crash on startup - pipeline prompt can be created manually
|
# Schema loading and migration handled by startup.sh
|
||||||
|
# This function kept for backwards compatibility
|
||||||
|
|
||||||
|
# Ensure "pipeline" master prompt exists
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cur.execute("SELECT COUNT(*) as count FROM ai_prompts WHERE slug='pipeline'")
|
||||||
|
if cur.fetchone()['count'] == 0:
|
||||||
|
cur.execute("""
|
||||||
|
INSERT INTO ai_prompts (slug, name, description, template, active, sort_order)
|
||||||
|
VALUES (
|
||||||
|
'pipeline',
|
||||||
|
'Mehrstufige Gesamtanalyse',
|
||||||
|
'Master-Schalter für die gesamte Pipeline. Deaktiviere diese Analyse, um die Pipeline komplett zu verstecken.',
|
||||||
|
'PIPELINE_MASTER',
|
||||||
|
true,
|
||||||
|
-10
|
||||||
|
)
|
||||||
|
""")
|
||||||
|
conn.commit()
|
||||||
|
print("✓ Pipeline master prompt created")
|
||||||
|
|
||||||
# ── Helper: get profile_id from header ───────────────────────────────────────
|
# ── Helper: get profile_id from header ───────────────────────────────────────
|
||||||
def get_pid(x_profile_id: Optional[str] = Header(default=None)) -> str:
|
def get_pid(x_profile_id: Optional[str] = Header(default=None)) -> str:
|
||||||
|
|||||||
@@ -69,12 +69,6 @@ BOOLEAN_COLUMNS = {
|
|||||||
'ai_prompts': ['active'],
|
'ai_prompts': ['active'],
|
||||||
}
|
}
|
||||||
|
|
||||||
# Column whitelist for tables that have schema differences
|
|
||||||
# Only these columns will be migrated (filters out legacy columns)
|
|
||||||
COLUMN_WHITELIST = {
|
|
||||||
'ai_insights': ['id', 'profile_id', 'scope', 'content', 'created'],
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
# ================================================================
|
# ================================================================
|
||||||
# CONVERSION HELPERS
|
# CONVERSION HELPERS
|
||||||
@@ -179,17 +173,8 @@ def migrate_table(pg_conn, table: str) -> Dict[str, int]:
|
|||||||
# Convert rows
|
# Convert rows
|
||||||
converted_rows = [convert_row(row, table) for row in sqlite_rows]
|
converted_rows = [convert_row(row, table) for row in sqlite_rows]
|
||||||
|
|
||||||
# Get column names - filter if whitelist exists for this table
|
# Get column names
|
||||||
if table in COLUMN_WHITELIST:
|
columns = list(converted_rows[0].keys())
|
||||||
allowed_columns = COLUMN_WHITELIST[table]
|
|
||||||
# Filter rows to only include allowed columns
|
|
||||||
converted_rows = [
|
|
||||||
{k: v for k, v in row.items() if k in allowed_columns}
|
|
||||||
for row in converted_rows
|
|
||||||
]
|
|
||||||
columns = allowed_columns
|
|
||||||
else:
|
|
||||||
columns = list(converted_rows[0].keys())
|
|
||||||
cols_str = ', '.join(columns)
|
cols_str = ', '.join(columns)
|
||||||
placeholders = ', '.join(['%s'] * len(columns))
|
placeholders = ', '.join(['%s'] * len(columns))
|
||||||
|
|
||||||
|
|||||||
+3
-1
@@ -1,7 +1,7 @@
|
|||||||
services:
|
services:
|
||||||
postgres:
|
postgres:
|
||||||
image: postgres:16-alpine
|
image: postgres:16-alpine
|
||||||
container_name: mitai-db-prod
|
container_name: mitai-db
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_DB: mitai_prod
|
POSTGRES_DB: mitai_prod
|
||||||
@@ -9,6 +9,8 @@ services:
|
|||||||
POSTGRES_PASSWORD: ${DB_PASSWORD:-change_me_in_production}
|
POSTGRES_PASSWORD: ${DB_PASSWORD:-change_me_in_production}
|
||||||
volumes:
|
volumes:
|
||||||
- mitai_postgres_data:/var/lib/postgresql/data
|
- mitai_postgres_data:/var/lib/postgresql/data
|
||||||
|
ports:
|
||||||
|
- "127.0.0.1:5432:5432"
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "pg_isready -U mitai_prod"]
|
test: ["CMD-SHELL", "pg_isready -U mitai_prod"]
|
||||||
interval: 10s
|
interval: 10s
|
||||||
|
|||||||
Reference in new issue
Block a user