38 lines
1.5 KiB
Python
38 lines
1.5 KiB
Python
import os
|
|
import django
|
|
from django.db import connection
|
|
|
|
# 设置 Django 环境
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'daren.settings')
|
|
django.setup()
|
|
|
|
with connection.cursor() as cursor:
|
|
# 检查 chat 应用的迁移是否已应用
|
|
cursor.execute("SELECT * FROM django_migrations WHERE app='chat' AND name='0001_initial'")
|
|
chat_migration = cursor.fetchone()
|
|
|
|
# 检查 knowledge_base 应用的迁移是否已应用
|
|
cursor.execute("SELECT * FROM django_migrations WHERE app='knowledge_base' AND name='0001_initial'")
|
|
kb_migration = cursor.fetchone()
|
|
|
|
print("当前迁移状态:")
|
|
print(f"chat.0001_initial: {'已应用' if chat_migration else '未应用'}")
|
|
print(f"knowledge_base.0001_initial: {'已应用' if kb_migration else '未应用'}")
|
|
|
|
if chat_migration and not kb_migration:
|
|
print("\n修复迁移依赖问题...")
|
|
# 插入 knowledge_base 迁移记录,设置应用时间在 chat 迁移之前
|
|
# 获取 chat 迁移的应用时间
|
|
chat_applied_time = chat_migration[3]
|
|
|
|
# 插入 knowledge_base 迁移记录,时间比 chat 迁移早一分钟
|
|
cursor.execute(
|
|
"INSERT INTO django_migrations (app, name, applied) VALUES (%s, %s, %s)",
|
|
['knowledge_base', '0001_initial', chat_applied_time]
|
|
)
|
|
connection.commit()
|
|
print("已成功修复迁移依赖问题")
|
|
elif kb_migration:
|
|
print("knowledge_base 迁移已应用,无需修复")
|
|
else:
|
|
print("chat 迁移未应用,无需修复依赖问题") |