JhHardwareWRS_BackPoint/scripts/migrate_work_session_repeated_molds.py
2026-06-24 15:19:14 +08:00

38 lines
1.2 KiB
Python

from pathlib import Path
import sys
from sqlalchemy import text
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
from app.database import engine # noqa: E402
def main() -> None:
with engine.begin() as conn:
session_index_exists = conn.execute(text("""
SELECT COUNT(1)
FROM information_schema.statistics
WHERE table_schema = DATABASE()
AND table_name = 'work_session_devices'
AND index_name = 'idx_session_devices_session'
""")).scalar()
if not session_index_exists:
conn.execute(text("CREATE INDEX idx_session_devices_session ON work_session_devices (session_id)"))
index_exists = conn.execute(text("""
SELECT COUNT(1)
FROM information_schema.statistics
WHERE table_schema = DATABASE()
AND table_name = 'work_session_devices'
AND index_name = 'uk_session_device'
""")).scalar()
if index_exists:
conn.execute(text("ALTER TABLE work_session_devices DROP INDEX uk_session_device"))
print("work session repeated molds migrated")
if __name__ == "__main__":
main()