"""
执行数据库迁移脚本
用于将数据库升级到最新版本或指定版本
"""

import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))

from backend.modules.database.migrations import DatabaseMigration
from backend.utils.logger_config import setup_logger

logger = setup_logger(__name__)

def main():
    """执行数据库迁移"""
    try:
        # 创建迁移实例
        migration = DatabaseMigration()
        
        # 获取当前版本
        current_version = migration.get_current_version()
        logger.info(f"当前数据库版本: {current_version}")
        
        # 获取待执行的迁移
        pending_migrations = migration.get_pending_migrations()
        if not pending_migrations:
            logger.info("没有待执行的迁移")
            return
            
        logger.info(f"发现 {len(pending_migrations)} 个待执行的迁移")
        for m in pending_migrations:
            logger.info(f"- {m['version']}: {m['description']}")
            
        # 执行迁移
        if migration.migrate_up():
            logger.info("数据库迁移成功")
            new_version = migration.get_current_version()
            logger.info(f"当前数据库版本: {new_version}")
        else:
            logger.error("数据库迁移失败")
            
    except Exception as e:
        logger.error(f"执行迁移时发生错误: {e}")
        sys.exit(1)

if __name__ == "__main__":
    main() 