36 lines
1.2 KiB
Python
36 lines
1.2 KiB
Python
"""Migration: Create indexes on date columns used by the 18:00 daily reminder.
|
|
|
|
Run this script to add indexes on the date columns that the Discord bot's
|
|
daily reminder queries (Match.date, TeamMatch.date, Tryout.date,
|
|
OneOnOneRequest.date). The app uses create_all() for new tables, but existing
|
|
production databases need these added explicitly.
|
|
|
|
Usage: python migrations/add_reminder_date_indexes.py
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from app.app import create_app
|
|
from app.extensions import db
|
|
from sqlalchemy import text
|
|
|
|
app = create_app()
|
|
|
|
with app.app_context():
|
|
indexes = [
|
|
('ix_matches_date', 'matches', 'date'),
|
|
('ix_team_matches_date', 'team_matches', 'date'),
|
|
('ix_tryouts_date', 'tryouts', 'date'),
|
|
('ix_one_on_one_requests_date', 'one_on_one_requests', 'date'),
|
|
]
|
|
|
|
for index_name, table, column in indexes:
|
|
db.session.execute(text(
|
|
f'CREATE INDEX IF NOT EXISTS {index_name} ON {table} ({column})'
|
|
))
|
|
print(f'Index {index_name} on {table}({column}) ensured.')
|
|
|
|
db.session.commit()
|
|
print('Migration complete.') |