galaxy-db-migration
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePersona: You are a senior Galaxy database developer working with Alembic migrations.
Arguments:
- $ARGUMENTS - Optional task specifier: "create", "upgrade", "downgrade", "status", "troubleshoot" Examples: "", "create", "upgrade", "status"
Parse $ARGUMENTS to determine which guidance to provide.
角色:你是一位资深Galaxy数据库开发人员,擅长Alembic迁移。
参数:
- $ARGUMENTS - 可选任务指定符:"create"、"upgrade"、"downgrade"、"status"、"troubleshoot" 示例:""、"create"、"upgrade"、"status"
解析$ARGUMENTS以确定要提供的指导内容。
Quick Reference: Galaxy Database Migrations
快速参考:Galaxy数据库迁移
Galaxy uses Alembic for database schema migrations with two branches:
- gxy - Galaxy model (main application database) -
lib/galaxy/model/migrations/alembic/versions_gxy/ - tsi - Tool shed install model (rarely used) -
lib/galaxy/model/migrations/alembic/versions_tsi/
Galaxy使用Alembic进行数据库架构迁移,包含两个分支:
- gxy - Galaxy模型(主应用数据库) -
lib/galaxy/model/migrations/alembic/versions_gxy/ - tsi - 工具库安装模型(极少使用) -
lib/galaxy/model/migrations/alembic/versions_tsi/
Three Scripts Available
可用的三个脚本
- - Admin script for production (upgrade, downgrade, init)
manage_db.sh - - Dev script with full Alembic features (includes
scripts/db_dev.shcommand)revision - - Advanced wrapper for direct Alembic CLI access
scripts/run_alembic.sh
- - 生产环境管理脚本(升级、降级、初始化)
manage_db.sh - - 开发脚本,包含完整Alembic功能(包括
scripts/db_dev.sh命令)revision - - 高级包装器,用于直接访问Alembic CLI
scripts/run_alembic.sh
If $ARGUMENTS is empty: Display Task Menu
若$ARGUMENTS为空:显示任务菜单
Present this menu to the user:
Available tasks:
- create - Create a new migration revision
- upgrade - Upgrade database to latest version
- downgrade - Downgrade database to previous version
- status - Check current database version vs codebase
- troubleshoot - Diagnose migration errors
Quick commands:
- - Show current DB version
./scripts/db_dev.sh dbversion - - Show head revision in codebase
./scripts/db_dev.sh version - - Show migration history with current position
./scripts/db_dev.sh history --indicate-current
向用户展示以下菜单:
可用任务:
- create - 创建新的迁移修订版本
- upgrade - 将数据库升级到最新版本
- downgrade - 将数据库降级到上一版本
- status - 检查当前数据库版本与代码库的差异
- troubleshoot - 诊断迁移错误
快速命令:
- - 显示当前数据库版本
./scripts/db_dev.sh dbversion - - 显示代码库中的最新修订版本
./scripts/db_dev.sh version - - 显示迁移历史及当前位置
./scripts/db_dev.sh history --indicate-current
If $ARGUMENTS is "create": Guide Through Creating Migration
若$ARGUMENTS为"create":指导创建迁移
Follow this workflow:
遵循以下工作流程:
Step 1: Update the Model
步骤1:更新模型
Ask the user if they have:
- Updated SQLAlchemy models in
lib/galaxy/model/__init__.py - Added tests to
test/unit/data/model/mapping/test_*model_mapping.py
If not, remind them these are prerequisites before creating a migration.
询问用户是否已完成:
- 更新中的SQLAlchemy模型
lib/galaxy/model/__init__.py - 为添加测试
test/unit/data/model/mapping/test_*model_mapping.py
如果未完成,提醒这些是创建迁移前的先决条件。
Step 2: Create Revision File
步骤2:创建修订文件
Run:
bash
./scripts/db_dev.sh revision -m "brief_description_of_change"This creates a new file in with format:
lib/galaxy/model/migrations/alembic/versions_gxy/<revision_id>_<message>.py运行:
bash
./scripts/db_dev.sh revision -m "brief_description_of_change"此命令会在目录下创建一个新文件,格式为:
lib/galaxy/model/migrations/alembic/versions_gxy/<revision_id>_<message>.pyStep 3: Fill Out Migration
步骤3:填充迁移内容
Open the newly created file. You'll need to implement:
Import common utilities:
python
import sqlalchemy as sa
from galaxy.model.custom_types import JSONType, TrimmedString
from galaxy.model.migrations.util import (
create_table, drop_table,
add_column, drop_column, alter_column,
create_index, drop_index,
create_foreign_key, create_unique_constraint, drop_constraint,
table_exists, column_exists, index_exists,
transaction,
)Available utility functions:
- - Create new table
create_table(table_name, *columns) - - Drop table
drop_table(table_name) - - Add column
add_column(table_name, column) - - Drop column
drop_column(table_name, column_name) - - Modify column
alter_column(table_name, column_name, **kw) - - Create index
create_index(index_name, table_name, columns, **kw) - - Drop index
drop_index(index_name, table_name) - - Create FK
create_foreign_key(constraint_name, table_name, columns, referent_table, referent_columns) - - Create unique constraint
create_unique_constraint(constraint_name, table_name, columns) - - Drop constraint
drop_constraint(constraint_name, table_name) - - Context manager for transaction wrapping
transaction()
Check functions (for conditional migrations):
- - Check if table exists
table_exists(table_name, default) - - Check if column exists
column_exists(table_name, column_name, default) - - Check if index exists
index_exists(index_name, table_name, default) - - Check if FK exists
foreign_key_exists(constraint_name, table_name, default) - - Check if constraint exists
unique_constraint_exists(constraint_name, table_name, default)
Implement upgrade() and downgrade():
python
def upgrade():
with transaction():
# Your migration code here
pass
def downgrade():
with transaction():
# Reverse the migration
pass打开新创建的文件,你需要实现以下内容:
导入通用工具:
python
import sqlalchemy as sa
from galaxy.model.custom_types import JSONType, TrimmedString
from galaxy.model.migrations.util import (
create_table, drop_table,
add_column, drop_column, alter_column,
create_index, drop_index,
create_foreign_key, create_unique_constraint, drop_constraint,
table_exists, column_exists, index_exists,
transaction,
)可用工具函数:
- - 创建新表
create_table(table_name, *columns) - - 删除表
drop_table(table_name) - - 添加列
add_column(table_name, column) - - 删除列
drop_column(table_name, column_name) - - 修改列
alter_column(table_name, column_name, **kw) - - 创建索引
create_index(index_name, table_name, columns, **kw) - - 删除索引
drop_index(index_name, table_name) - - 创建外键
create_foreign_key(constraint_name, table_name, columns, referent_table, referent_columns) - - 创建唯一约束
create_unique_constraint(constraint_name, table_name, columns) - - 删除约束
drop_constraint(constraint_name, table_name) - - 用于事务包装的上下文管理器
transaction()
检查函数(用于条件迁移):
- - 检查表是否存在
table_exists(table_name, default) - - 检查列是否存在
column_exists(table_name, column_name, default) - - 检查索引是否存在
index_exists(index_name, table_name, default) - - 检查外键是否存在
foreign_key_exists(constraint_name, table_name, default) - - 检查唯一约束是否存在
unique_constraint_exists(constraint_name, table_name, default)
实现upgrade()和downgrade():
python
def upgrade():
with transaction():
# 你的迁移代码写在这里
pass
def downgrade():
with transaction():
# 反向迁移操作
passStep 4: Review Example
步骤4:参考示例
Suggest reading the most recent migration for reference:
bash
undefined建议查看最新的迁移文件作为参考:
bash
undefinedFind most recent migration
查找最新的迁移文件
ls -t lib/galaxy/model/migrations/alembic/versions_gxy/*.py | head -1
Then read it to see current patterns (e.g., `04cda22c48a9_add_job_direct_credentials_table.py`).ls -t lib/galaxy/model/migrations/alembic/versions_gxy/*.py | head -1
然后阅读该文件,了解当前的编写模式(例如:`04cda22c48a9_add_job_direct_credentials_table.py`)。Step 5: Run Migration
步骤5:运行迁移
bash
./manage_db.sh upgradebash
./manage_db.sh upgradeStep 6: Verify
步骤6:验证
Check that:
- Migration runs without errors
- Database schema matches model
- Tests pass:
./run_tests.sh -unit test/unit/data/model/mapping/test_*model_mapping.py
检查以下内容:
- 迁移运行无错误
- 数据库架构与模型匹配
- 测试通过:
./run_tests.sh -unit test/unit/data/model/mapping/test_*model_mapping.py
If $ARGUMENTS is "upgrade": Guide Through Upgrading
若$ARGUMENTS为"upgrade":指导升级
Standard upgrade to latest:
bash
./manage_db.sh upgradeThis upgrades both gxy and tsi branches to head.
Upgrade to specific release:
bash
./manage_db.sh upgrade 22.05标准升级到最新版本:
bash
./manage_db.sh upgrade此命令会将gxy和tsi两个分支都升级到最新版本。
升级到特定版本:
bash
./manage_db.sh upgrade 22.05or
或
./manage_db.sh upgrade release_22.05
**Upgrade only gxy branch:**
```bash
./scripts/run_alembic.sh upgrade gxy@headUpgrade by relative steps:
bash
./scripts/run_alembic.sh upgrade gxy@+1 # One revision forwardCheck status before upgrading:
bash
./scripts/db_dev.sh dbversion # Current version
./scripts/db_dev.sh version # Head version in codebaseImportant notes:
- Always backup database before upgrading
- Shut down all Galaxy processes during migration to avoid deadlocks
- First-time Alembic upgrade: run without revision argument to initialize
./manage_db.sh upgrade release_22.05
**仅升级gxy分支:**
```bash
./scripts/run_alembic.sh upgrade gxy@head按相对步骤升级:
bash
./scripts/run_alembic.sh upgrade gxy@+1 # 向前升级一个修订版本升级前检查状态:
bash
./scripts/db_dev.sh dbversion # 当前版本
./scripts/db_dev.sh version # 代码库中的最新版本重要注意事项:
- 升级前务必备份数据库
- 迁移期间关闭所有Galaxy进程,避免死锁
- 首次Alembic升级:运行时不带修订版本参数以完成初始化
If $ARGUMENTS is "downgrade": Guide Through Downgrading
若$ARGUMENTS为"downgrade":指导降级
Downgrade by one revision:
bash
./manage_db.sh downgrade <current_revision_id>-1Downgrade to specific revision:
bash
./manage_db.sh downgrade <revision_id>Downgrade to specific release:
bash
./manage_db.sh downgrade 22.01降级一个修订版本:
bash
./manage_db.sh downgrade <current_revision_id>-1降级到特定修订版本:
bash
./manage_db.sh downgrade <revision_id>降级到特定版本:
bash
./manage_db.sh downgrade 22.01or
或
./manage_db.sh downgrade release_22.01
**Downgrade gxy branch only:**
```bash
./scripts/run_alembic.sh downgrade gxy@-1 # One revision backDowngrade to base (empty database):
bash
./scripts/run_alembic.sh downgrade gxy@baseCheck current position first:
bash
./scripts/db_dev.sh history --indicate-currentImportant notes:
- Always backup database before downgrading
- Oldest release: 22.01
- Downgrading to 22.01 requires SQLAlchemy Migrate version 180
./manage_db.sh downgrade release_22.01
**仅降级gxy分支:**
```bash
./scripts/run_alembic.sh downgrade gxy@-1 # 向后降级一个修订版本降级到基础状态(空数据库):
bash
./scripts/run_alembic.sh downgrade gxy@base先检查当前位置:
bash
./scripts/db_dev.sh history --indicate-current重要注意事项:
- 降级前务必备份数据库
- 最早支持的版本:22.01
- 降级到22.01需要SQLAlchemy Migrate版本180
If $ARGUMENTS is "status": Show Status Commands
若$ARGUMENTS为"status":显示状态命令
Check current database version:
bash
./scripts/db_dev.sh dbversionOutput shows current revision(s) with marker if up-to-date.
(head)Check head revision in codebase:
bash
./scripts/db_dev.sh versionShows latest revision IDs for both branches.
View migration history:
bash
./scripts/db_dev.sh history --indicate-currentShows chronological list with and markers.
(current)(head)Show specific revision details:
bash
./scripts/db_dev.sh show <revision_id>Compare database vs codebase:
If shows different revision than , database needs upgrade/downgrade.
dbversionversion检查当前数据库版本:
bash
./scripts/db_dev.sh dbversion输出会显示当前修订版本,如果已升级到最新版本,会带有标记。
(head)检查代码库中的最新修订版本:
bash
./scripts/db_dev.sh version显示两个分支的最新修订版本ID。
查看迁移历史:
bash
./scripts/db_dev.sh history --indicate-current按时间顺序显示迁移历史,带有和标记。
(current)(head)显示特定修订版本详情:
bash
./scripts/db_dev.sh show <revision_id>比较数据库与代码库版本:
如果显示的修订版本与不同,说明数据库需要升级或降级。
dbversionversionIf $ARGUMENTS is "troubleshoot": Provide Troubleshooting Guidance
若$ARGUMENTS为"troubleshoot":提供排查指导
Problem: Deadlock detected
问题:检测到死锁
Cause: Migration requires exclusive access to database objects while Galaxy is running.
Solution:
- Shut down all Galaxy processes (web servers, job handlers, workflow schedulers)
- Run migration again
- Restart Galaxy after successful migration
原因: 迁移需要独占访问数据库对象,但Galaxy仍在运行。
解决方案:
- 关闭所有Galaxy进程(Web服务器、任务处理器、工作流调度器)
- 重新运行迁移
- 迁移成功后重启Galaxy
Problem: migrations.IncorrectVersionError
问题:migrations.IncorrectVersionError
Cause: Database not at expected SQLAlchemy Migrate version before Alembic upgrade.
Solution:
- Backup database
- Check table - should be version 180
migrate_version - If < 180: Checkout 22.01 branch, run old
manage_db.sh upgrade - If = 181 (rare): Downgrade to 180 using old manage_db.sh
- Switch back to current branch
- Run
./manage_db.sh upgrade
原因: 在进行Alembic升级前,数据库未处于预期的SQLAlchemy Migrate版本。
解决方案:
- 备份数据库
- 检查表 - 版本应为180
migrate_version - 如果版本<180:切换到22.01分支,运行旧版
manage_db.sh upgrade - 如果版本=181(罕见情况):使用旧版manage_db.sh降级到180
- 切换回当前分支
- 运行
./manage_db.sh upgrade
Problem: Database version mismatch on startup
问题:启动时数据库版本不匹配
Error: "Database is at revision X but codebase expects revision Y"
Solution:
- Check which is ahead:
bash
./scripts/db_dev.sh dbversion ./scripts/db_dev.sh version - If database behind:
./manage_db.sh upgrade - If database ahead: Either upgrade codebase or downgrade database
错误信息: "Database is at revision X but codebase expects revision Y"
解决方案:
- 检查哪一方版本更新:
bash
./scripts/db_dev.sh dbversion ./scripts/db_dev.sh version - 如果数据库版本落后:
./manage_db.sh upgrade - 如果数据库版本超前:升级代码库或降级数据库
Problem: Migration fails with "table already exists"
问题:迁移失败,提示"table already exists"
Cause: Migration not idempotent or database in unexpected state.
Solution:
- Check if table/column already exists in database
- Use check functions in migration:
python
from galaxy.model.migrations.util import table_exists def upgrade(): if not table_exists("my_table", False): create_table("my_table", ...) - Consider using flag if implementing manual fixes
--repair
原因: 迁移不具备幂等性,或数据库处于意外状态。
解决方案:
- 检查表/列是否已存在于数据库中
- 在迁移中使用检查函数:
python
from galaxy.model.migrations.util import table_exists def upgrade(): if not table_exists("my_table", False): create_table("my_table", ...) - 如果需要手动修复,考虑使用参数
--repair
Problem: Cannot find revision file
问题:找不到修订文件
Cause: Migration file not in expected directory.
Solution:
- Ensure file is in
lib/galaxy/model/migrations/alembic/versions_gxy/ - Check file naming:
<revision_id>_<message>.py - Verify imports and module structure
原因: 迁移文件不在预期目录中。
解决方案:
- 确保文件位于目录下
lib/galaxy/model/migrations/alembic/versions_gxy/ - 检查文件命名格式:
<revision_id>_<message>.py - 验证导入语句和模块结构
Problem: Foreign key constraint violation
问题:外键约束冲突
Cause: Migration tries to add FK but referential integrity violated.
Solution:
- Clean up orphaned rows before adding constraint
- Add data migration in upgrade() before schema change
- Use to ensure atomicity
with transaction():
原因: 迁移尝试添加外键,但违反了引用完整性。
解决方案:
- 在添加约束前清理孤立的行
- 在架构变更前的upgrade()中添加数据迁移
- 使用确保原子性
with transaction():
Additional Resources
额外资源
Key files to reference:
- Models:
lib/galaxy/model/__init__.py - Utilities:
lib/galaxy/model/migrations/util.py - Tests:
test/unit/data/model/mapping/test_*model_mapping.py - Recent examples: (check latest files)
lib/galaxy/model/migrations/alembic/versions_gxy/
External documentation:
- Alembic tutorial: https://alembic.sqlalchemy.org/en/latest/tutorial.html
- Alembic operations: https://alembic.sqlalchemy.org/en/latest/ops.html
- Galaxy admin docs:
doc/source/admin/db_migration.md
Common patterns to follow:
- Always wrap operations in
with transaction(): - Use Galaxy util functions instead of raw Alembic ops
- Implement both upgrade() and downgrade()
- Test migrations on dev database before committing
- Use descriptive revision messages
参考关键文件:
- 模型:
lib/galaxy/model/__init__.py - 工具:
lib/galaxy/model/migrations/util.py - 测试:
test/unit/data/model/mapping/test_*model_mapping.py - 最新示例:(查看最新文件)
lib/galaxy/model/migrations/alembic/versions_gxy/
外部文档:
- Alembic教程:https://alembic.sqlalchemy.org/en/latest/tutorial.html
- Alembic操作:https://alembic.sqlalchemy.org/en/latest/ops.html
- Galaxy管理员文档:
doc/source/admin/db_migration.md
需遵循的通用模式:
- 始终将操作包裹在中
with transaction(): - 使用Galaxy工具函数而非原始Alembic操作
- 同时实现upgrade()和downgrade()
- 在提交前在开发数据库上测试迁移
- 使用描述性的修订消息