@@ -164,11 +164,14 @@ def run_migrations(self):
164164 # 如果数据库已存在但没有版本表,创建版本表并设置为最新版本
165165 if current_version == 0 and latest_version > 0 and os .path .exists (self .db_path ):
166166 # 检查users表是否已有新字段,如果有,说明是通过SQLAlchemy创建的
167- if (
168- self .check_column_exists ("users" , "login_failed_count" )
169- and self .check_column_exists ("users" , "last_failed_login" )
170- and self .check_column_exists ("users" , "login_locked_until" )
171- ):
167+ required_columns = [
168+ "login_failed_count" ,
169+ "last_failed_login" ,
170+ "login_locked_until" ,
171+ "is_deleted" ,
172+ "deleted_at" ,
173+ ]
174+ if all (self .check_column_exists ("users" , column ) for column in required_columns ):
172175 # 字段已存在,直接设置为最新版本
173176 logger .info (f"检测到现有数据库已包含最新字段,设置版本为 v{ latest_version } " )
174177 self .set_version (latest_version )
@@ -217,9 +220,8 @@ def run_migrations(self):
217220
218221 def get_latest_migration_version (self ) -> int :
219222 """获取最新迁移版本号"""
220- # 这里返回硬编码的最新版本号,不依赖迁移定义
221- # 因为迁移定义可能为空(字段已存在)
222- return 1 # 当前最新版本是 v1
223+ migrations = self .get_migrations ()
224+ return max ((version for version , _ , _ in migrations ), default = 0 )
223225
224226 def get_migrations (self ) -> list [tuple [int , str , list [str ]]]:
225227 """获取所有迁移定义
@@ -243,9 +245,18 @@ def get_migrations(self) -> list[tuple[int, str, list[str]]]:
243245 if not self .check_column_exists ("users" , "login_locked_until" ):
244246 v1_commands .append ("ALTER TABLE users ADD COLUMN login_locked_until DATETIME" )
245247
246- # 如果有命令需要执行,才添加迁移
247- if v1_commands :
248- migrations .append ((1 , "为用户表添加登录失败限制字段" , v1_commands ))
248+ migrations .append ((1 , "为用户表添加登录失败限制字段" , v1_commands ))
249+
250+ # 迁移 v2: 为 users 表添加软删除字段
251+ v2_commands : list [str ] = []
252+
253+ if not self .check_column_exists ("users" , "is_deleted" ):
254+ v2_commands .append ("ALTER TABLE users ADD COLUMN is_deleted INTEGER NOT NULL DEFAULT 0" )
255+
256+ if not self .check_column_exists ("users" , "deleted_at" ):
257+ v2_commands .append ("ALTER TABLE users ADD COLUMN deleted_at DATETIME" )
258+
259+ migrations .append ((2 , "为用户表添加软删除字段" , v2_commands ))
249260
250261 # 未来的迁移可以在这里添加
251262 # migrations.append((
@@ -290,6 +301,8 @@ def validate_database_schema(db_path: str) -> tuple[bool, list[str]]:
290301 "login_failed_count" ,
291302 "last_failed_login" ,
292303 "login_locked_until" ,
304+ "is_deleted" ,
305+ "deleted_at" ,
293306 ],
294307 "operation_logs" : ["id" , "user_id" , "operation" , "details" , "ip_address" , "timestamp" ],
295308 }
@@ -337,7 +350,9 @@ def check_and_migrate(db_path: str):
337350 logger .warning (f" - { issue } " )
338351
339352 if os .path .exists (db_path ):
340- logger .info ("建议运行迁移脚本: docker exec api-dev python /app/scripts/migrate_user_fields.py" )
353+ logger .info (
354+ "建议运行迁移脚本: docker exec api-dev python /app/scripts/migrate_user_soft_delete.py"
355+ )
341356
342357 migrator = DatabaseMigrator (db_path )
343358
0 commit comments