()
| 78 | |
| 79 | |
| 80 | async def init_db(): |
| 81 | try: |
| 82 | db_config = get_db_config() |
| 83 | |
| 84 | if not Tortoise._inited: |
| 85 | await Tortoise.init(config=db_config) |
| 86 | |
| 87 | async with db_startup_lock(): |
| 88 | # 创建migrations表 |
| 89 | await Tortoise.get_connection("default").execute_script(""" |
| 90 | CREATE TABLE IF NOT EXISTS migrates ( |
| 91 | id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 92 | migration_file VARCHAR(255) NOT NULL UNIQUE, |
| 93 | executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
| 94 | ) |
| 95 | """) |
| 96 | |
| 97 | # 执行迁移 |
| 98 | await execute_migrations() |
| 99 | |
| 100 | except Exception as e: |
| 101 | logger.error(f"数据库初始化失败: {str(e)}") |
| 102 | raise |
| 103 | |
| 104 | |
| 105 | async def execute_migrations(): |
no test coverage detected