Commit 999f3e0c by luoqi

feat:数据库备份

parent 9a6b11c4
Pipeline #3231 passed with stage
in 23 seconds
......@@ -50,12 +50,8 @@ log_info "📋 第一步:等待数据库就绪..."
python database/scripts/wait-for-db.py
log_success "数据库连接就绪"
# 第二步:备份数据库
log_info "📋 第二步:备份数据库..."
python database/scripts/backup_database.py || log_warning "数据库备份失败,但继续启动应用"
# 第三步:执行数据库迁移
log_info "📋 第三步:执行数据库迁移..."
# 第二步:执行数据库迁移
log_info "📋 第二步:执行数据库迁移..."
# 使用专门的迁移管理器
python database/scripts/migration_manager.py
......@@ -67,8 +63,8 @@ else
exit 1
fi
# 第步:数据导入(如果需要)
log_info "📋 第步:检查数据导入需求..."
# 第步:数据导入(如果需要)
log_info "📋 第步:检查数据导入需求..."
# 检查是否有患者数据需要导入
if [ -f "database/scripts/safe_import_patients.py" ] && [ -d "data/patients/clinics" ]; then
......@@ -79,8 +75,8 @@ else
log_info "无需数据导入"
fi
# 第步:启动Flask应用
log_info "📋 第步:启动Flask应用..."
# 第步:启动Flask应用
log_info "📋 第步:启动Flask应用..."
log_info "🌐 应用将在端口 5000 启动"
log_info "🔗 健康检查端点: http://localhost:5000/api/health"
......
......@@ -94,6 +94,56 @@ def get_current_revision():
os.environ['FLASK_APP'] = 'app.py'
return run_command('flask db current', '获取当前迁移状态')
def check_pending_migrations():
"""检查是否有待执行的迁移"""
try:
os.environ['FLASK_APP'] = 'app.py'
# 方法1:比较当前版本和最新版本
current_result = subprocess.run(
'flask db current',
shell=True,
capture_output=True,
text=True,
cwd=project_root
)
heads_result = subprocess.run(
'flask db heads',
shell=True,
capture_output=True,
text=True,
cwd=project_root
)
if current_result.returncode == 0 and heads_result.returncode == 0:
current_version = current_result.stdout.strip()
head_version = heads_result.stdout.strip()
print(f"🔍 当前数据库版本: {current_version}")
print(f"🔍 最新迁移版本: {head_version}")
# 提取版本号(去掉 "(head)" 等标记)
current_clean = current_version.split()[0] if current_version else ""
head_clean = head_version.split()[0] if head_version else ""
# 如果当前版本和head版本不同,说明需要迁移
needs_migration = current_clean != head_clean
if needs_migration:
print(f"📋 需要迁移:{current_clean} -> {head_clean}")
else:
print(f"✅ 数据库已是最新版本:{current_clean}")
return needs_migration
except Exception as e:
print(f"⚠️ 检查迁移状态时出错: {e}")
# 出错时保守处理,假设需要迁移
return True
return False
def check_alembic_version_table():
"""检查是否存在alembic_version表"""
try:
......@@ -141,7 +191,25 @@ def main():
# 步骤4: 获取当前状态
get_current_revision()
# 步骤4.5: 执行迁移
# 步骤4.5: 智能检查是否需要迁移
print("🔍 检查是否需要执行迁移...")
# 检查是否有待执行的迁移
needs_migration = check_pending_migrations()
if needs_migration:
print("📋 发现待执行的迁移,执行备份...")
# 执行迁移前备份
print("🔄 迁移前备份数据库...")
backup_result = run_command("python database/scripts/backup_database.py", "迁移前备份数据库")
if not backup_result:
print("⚠️ 备份失败,但继续执行迁移")
else:
print("✅ 迁移前备份完成")
else:
print("✅ 数据库已是最新版本,无需备份")
# 步骤5: 执行迁移
if not upgrade_database():
print("❌ 数据库迁移失败")
return False
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment