Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
customer-recall
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ai-tools
customer-recall
Commits
999f3e0c
Commit
999f3e0c
authored
Sep 10, 2025
by
luoqi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:数据库备份
parent
9a6b11c4
Pipeline
#3231
passed with stage
in 23 seconds
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
75 additions
and
11 deletions
+75
-11
database/scripts/entrypoint.sh
+6
-10
database/scripts/migration_manager.py
+69
-1
No files found.
database/scripts/entrypoint.sh
View file @
999f3e0c
...
@@ -50,12 +50,8 @@ log_info "📋 第一步:等待数据库就绪..."
...
@@ -50,12 +50,8 @@ log_info "📋 第一步:等待数据库就绪..."
python database/scripts/wait-for-db.py
python database/scripts/wait-for-db.py
log_success
"数据库连接就绪"
log_success
"数据库连接就绪"
# 第二步:备份数据库
# 第二步:执行数据库迁移
log_info
"📋 第二步:备份数据库..."
log_info
"📋 第二步:执行数据库迁移..."
python database/scripts/backup_database.py
||
log_warning
"数据库备份失败,但继续启动应用"
# 第三步:执行数据库迁移
log_info
"📋 第三步:执行数据库迁移..."
# 使用专门的迁移管理器
# 使用专门的迁移管理器
python database/scripts/migration_manager.py
python database/scripts/migration_manager.py
...
@@ -67,8 +63,8 @@ else
...
@@ -67,8 +63,8 @@ else
exit
1
exit
1
fi
fi
# 第
四
步:数据导入(如果需要)
# 第
三
步:数据导入(如果需要)
log_info
"📋 第
四
步:检查数据导入需求..."
log_info
"📋 第
三
步:检查数据导入需求..."
# 检查是否有患者数据需要导入
# 检查是否有患者数据需要导入
if
[
-f
"database/scripts/safe_import_patients.py"
]
&&
[
-d
"data/patients/clinics"
]
;
then
if
[
-f
"database/scripts/safe_import_patients.py"
]
&&
[
-d
"data/patients/clinics"
]
;
then
...
@@ -79,8 +75,8 @@ else
...
@@ -79,8 +75,8 @@ else
log_info
"无需数据导入"
log_info
"无需数据导入"
fi
fi
# 第
五
步:启动Flask应用
# 第
四
步:启动Flask应用
log_info
"📋 第
五
步:启动Flask应用..."
log_info
"📋 第
四
步:启动Flask应用..."
log_info
"🌐 应用将在端口 5000 启动"
log_info
"🌐 应用将在端口 5000 启动"
log_info
"🔗 健康检查端点: http://localhost:5000/api/health"
log_info
"🔗 健康检查端点: http://localhost:5000/api/health"
...
...
database/scripts/migration_manager.py
View file @
999f3e0c
...
@@ -94,6 +94,56 @@ def get_current_revision():
...
@@ -94,6 +94,56 @@ def get_current_revision():
os
.
environ
[
'FLASK_APP'
]
=
'app.py'
os
.
environ
[
'FLASK_APP'
]
=
'app.py'
return
run_command
(
'flask db current'
,
'获取当前迁移状态'
)
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
():
def
check_alembic_version_table
():
"""检查是否存在alembic_version表"""
"""检查是否存在alembic_version表"""
try
:
try
:
...
@@ -141,7 +191,25 @@ def main():
...
@@ -141,7 +191,25 @@ def main():
# 步骤4: 获取当前状态
# 步骤4: 获取当前状态
get_current_revision
()
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
():
if
not
upgrade_database
():
print
(
"❌ 数据库迁移失败"
)
print
(
"❌ 数据库迁移失败"
)
return
False
return
False
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment