This commit is contained in:
parent
b8454fefc7
commit
d887c3be91
|
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit b8454fefc775fa273b57d0932ff3e52370763604
|
||||||
|
|
@ -61,24 +61,44 @@ async def init_db():
|
||||||
return
|
return
|
||||||
|
|
||||||
async with engine.begin() as conn:
|
async with engine.begin() as conn:
|
||||||
# Create all tables
|
# Create all tables first
|
||||||
await conn.run_sync(Base.metadata.create_all)
|
await conn.run_sync(Base.metadata.create_all)
|
||||||
|
|
||||||
# Manual migrations for existing databases
|
# Manual migrations for existing databases
|
||||||
try:
|
try:
|
||||||
# Add language column if it doesn't exist
|
# Check if reports table exists
|
||||||
await conn.execute(text("ALTER TABLE reports ADD COLUMN IF NOT EXISTS language VARCHAR(10);"))
|
result = await conn.execute(text("""
|
||||||
# Add indexes to optimize queries
|
SELECT EXISTS (
|
||||||
await conn.execute(text("CREATE INDEX IF NOT EXISTS ix_reports_user_id ON reports (user_id);"))
|
SELECT FROM information_schema.tables
|
||||||
await conn.execute(text("CREATE INDEX IF NOT EXISTS ix_reports_created_at ON reports (created_at);"))
|
WHERE table_name = 'reports'
|
||||||
# Add composite index for common query pattern (user_id + created_at DESC)
|
)
|
||||||
await conn.execute(text("CREATE INDEX IF NOT EXISTS ix_reports_user_created ON reports (user_id, created_at DESC);"))
|
"""))
|
||||||
# Add index for language filtering
|
table_exists = result.scalar()
|
||||||
await conn.execute(text("CREATE INDEX IF NOT EXISTS ix_reports_language ON reports (language);"))
|
|
||||||
# Add composite index for user + market_type + language queries
|
if table_exists:
|
||||||
await conn.execute(text("CREATE INDEX IF NOT EXISTS ix_reports_user_market_lang ON reports (user_id, market_type, language);"))
|
# Add language column if it doesn't exist
|
||||||
# Covering index for counts query (GROUP BY market_type with language filter)
|
await conn.execute(text("""
|
||||||
await conn.execute(text("CREATE INDEX IF NOT EXISTS ix_reports_user_market_lang_count ON reports (user_id, market_type, COALESCE(language, 'zh-TW'));"))
|
ALTER TABLE reports ADD COLUMN IF NOT EXISTS language VARCHAR(10);
|
||||||
|
"""))
|
||||||
|
# Add indexes to optimize queries
|
||||||
|
await conn.execute(text("""
|
||||||
|
CREATE INDEX IF NOT EXISTS ix_reports_user_id ON reports (user_id);
|
||||||
|
"""))
|
||||||
|
await conn.execute(text("""
|
||||||
|
CREATE INDEX IF NOT EXISTS ix_reports_created_at ON reports (created_at);
|
||||||
|
"""))
|
||||||
|
# Add composite index for common query pattern (user_id + created_at DESC)
|
||||||
|
await conn.execute(text("""
|
||||||
|
CREATE INDEX IF NOT EXISTS ix_reports_user_created ON reports (user_id, created_at DESC);
|
||||||
|
"""))
|
||||||
|
# Add index for language filtering
|
||||||
|
await conn.execute(text("""
|
||||||
|
CREATE INDEX IF NOT EXISTS ix_reports_language ON reports (language);
|
||||||
|
"""))
|
||||||
|
# Add composite index for user + market_type + language queries
|
||||||
|
await conn.execute(text("""
|
||||||
|
CREATE INDEX IF NOT EXISTS ix_reports_user_market_lang ON reports (user_id, market_type, language);
|
||||||
|
"""))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Skipping manual migration (might be SQLite or syntax not supported): {e}")
|
print(f"Skipping manual migration (might be SQLite or syntax not supported): {e}")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue