"""DB 마이그레이션 실행 스크립트.""" import asyncio import sys from pathlib import Path import aiomysql from config.config_db import DB_HOST, DB_NAME, DB_PASSWORD, DB_PORT, DB_USER async def run_migration(): """SQL 마이그레이션 파일 실행.""" connection = await aiomysql.connect( host=DB_HOST, port=DB_PORT, user=DB_USER, password=DB_PASSWORD, db=DB_NAME, charset="utf8mb4", ) try: async with connection.cursor() as cursor: migration_file = Path(__file__).parent / "migrations" / "001_create_upload_tables.sql" sql_content = migration_file.read_text(encoding="utf-8") # SQL 파일의 각 명령문 실행 (';'으로 분리) statements = [stmt.strip() for stmt in sql_content.split(";") if stmt.strip()] for i, stmt in enumerate(statements, 1): print(f"[{i}/{len(statements)}] executing...") await cursor.execute(stmt) print(f" OK") await connection.commit() print("\nMigration complete!") return True except Exception as e: print(f"\nMigration failed: {e}") await connection.rollback() return False finally: connection.close() if __name__ == "__main__": success = asyncio.run(run_migration()) sys.exit(0 if success else 1)