Skip to content

Commit edc4caa

Browse files
committed
Merge branch 'main' of https://github.com/xerrors/Yuxi-Know
2 parents 8b98b5a + 5894231 commit edc4caa

File tree

3 files changed

+68
-13
lines changed

3 files changed

+68
-13
lines changed

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ asyncio_default_fixture_loop_scope = "function"
8181

8282
[dependency-groups]
8383
dev = [
84-
"ipykernel>=6.30.0",
8584
"ruff>=0.12.1",
8685
]
8786
test = [
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python3
2+
"""
3+
用户表软删除字段迁移脚本
4+
5+
该脚本用于为历史数据库添加 `is_deleted` 与 `deleted_at` 字段,
6+
同时会执行现有的数据库迁移逻辑,确保用户表结构与最新模型保持一致。
7+
"""
8+
9+
from __future__ import annotations
10+
11+
import sys
12+
from pathlib import Path
13+
14+
# 将项目根目录加入到 Python 路径,便于脚本在容器中执行
15+
PROJECT_ROOT = Path(__file__).resolve().parent.parent
16+
if str(PROJECT_ROOT) not in sys.path:
17+
sys.path.insert(0, str(PROJECT_ROOT))
18+
19+
from server.utils.migrate import DatabaseMigrator # noqa: E402
20+
from src import config # noqa: E402
21+
22+
23+
def main() -> None:
24+
db_path = Path(config.save_dir) / "database" / "server.db"
25+
migrator = DatabaseMigrator(str(db_path))
26+
27+
print(f"检测数据库: {db_path}")
28+
current_version = migrator.get_current_version()
29+
latest_version = migrator.get_latest_migration_version()
30+
print(f"当前迁移版本: v{current_version}, 最新版本: v{latest_version}")
31+
32+
try:
33+
migrator.run_migrations()
34+
print("✅ 迁移完成,数据库结构已更新")
35+
except Exception as exc:
36+
print(f"❌ 迁移失败: {exc}")
37+
raise
38+
39+
40+
if __name__ == "__main__":
41+
main()

server/utils/migrate.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)