MCPcopy Index your code
hub / github.com/vastsa/FileCodeBox / execute_migrations

Function execute_migrations

core/database.py:105–146  ·  view source on GitHub ↗

执行数据库迁移

()

Source from the content-addressed store, hash-verified

103
104
105async def execute_migrations():
106 """执行数据库迁移"""
107 try:
108 # 收集迁移文件
109 migration_files = []
110 for root, dirs, files in os.walk("apps"):
111 if "migrations" in dirs:
112 migration_path = os.path.join(root, "migrations")
113 migration_files.extend(glob.glob(os.path.join(migration_path, "migrations_*.py")))
114
115 # 按文件名排序
116 migration_files.sort()
117
118 for migration_file in migration_files:
119 file_name = os.path.basename(migration_file)
120
121 # 检查是否已执行
122 executed = await Tortoise.get_connection("default").execute_query(
123 "SELECT id FROM migrates WHERE migration_file = ?", [file_name]
124 )
125
126 if not executed[1]:
127 logger.info(f"执行迁移: {file_name}")
128 # 导入并执行migration
129 module_path = migration_file.replace("/", ".").replace("\\", ".").replace(".py", "")
130 try:
131 migration_module = importlib.import_module(module_path)
132 if hasattr(migration_module, "migrate"):
133 await migration_module.migrate()
134 # 记录执行
135 await Tortoise.get_connection("default").execute_query(
136 "INSERT INTO migrates (migration_file) VALUES (?)",
137 [file_name]
138 )
139 logger.info(f"迁移完成: {file_name}")
140 except Exception as e:
141 logger.error(f"迁移 {file_name} 执行失败: {str(e)}")
142 raise
143
144 except Exception as e:
145 logger.error(f"迁移过程发生错误: {str(e)}")
146 raise

Callers 1

init_dbFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected