Skip to content

Commit 318fdab

Browse files
author
JiayuXu
committed
fix(init_db): 改进数据库迁移失败时的错误处理和日志记录
不再自动删除migrations文件夹以避免意外丢失迁移历史
1 parent 18fdc01 commit 318fdab

1 file changed

Lines changed: 6 additions & 7 deletions

File tree

src/core/init_app.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import shutil
21
from functools import partial
32

43
from aerich import Command
@@ -220,12 +219,12 @@ async def init_db():
220219
await command.init()
221220
try:
222221
await command.migrate(no_input=True)
223-
except AttributeError:
224-
logger.warning(
225-
"unable to retrieve model history from database, model history will be created from scratch"
226-
)
227-
shutil.rmtree("migrations")
228-
await command.init_db(safe=True)
222+
except AttributeError as e:
223+
logger.error(f"数据库迁移失败: {e}")
224+
logger.warning("请手动检查数据库和migrations状态")
225+
# 不再自动删除migrations文件夹,避免意外丢失迁移历史
226+
# 如需重置migrations,请手动执行:rm -rf migrations && uv run aerich init-db
227+
raise RuntimeError("数据库迁移失败,请检查数据库连接和migrations状态") from e
229228

230229
await command.upgrade(run_in_transaction=True)
231230

0 commit comments

Comments
 (0)