galaxy-db-migration

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
Persona: 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

可用的三个脚本

  1. manage_db.sh
    - Admin script for production (upgrade, downgrade, init)
  2. scripts/db_dev.sh
    - Dev script with full Alembic features (includes
    revision
    command)
  3. scripts/run_alembic.sh
    - Advanced wrapper for direct Alembic CLI access

  1. manage_db.sh
    - 生产环境管理脚本(升级、降级、初始化)
  2. scripts/db_dev.sh
    - 开发脚本,包含完整Alembic功能(包括
    revision
    命令)
  3. scripts/run_alembic.sh
    - 高级包装器,用于直接访问Alembic CLI

If $ARGUMENTS is empty: Display Task Menu

若$ARGUMENTS为空:显示任务菜单

Present this menu to the user:
Available tasks:
  1. create - Create a new migration revision
  2. upgrade - Upgrade database to latest version
  3. downgrade - Downgrade database to previous version
  4. status - Check current database version vs codebase
  5. troubleshoot - Diagnose migration errors
Quick commands:
  • ./scripts/db_dev.sh dbversion
    - Show current DB version
  • ./scripts/db_dev.sh version
    - Show head revision in codebase
  • ./scripts/db_dev.sh history --indicate-current
    - Show migration history with current position

向用户展示以下菜单:
可用任务:
  1. create - 创建新的迁移修订版本
  2. upgrade - 将数据库升级到最新版本
  3. downgrade - 将数据库降级到上一版本
  4. status - 检查当前数据库版本与代码库的差异
  5. 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:
  1. Updated SQLAlchemy models in
    lib/galaxy/model/__init__.py
  2. Added tests to
    test/unit/data/model/mapping/test_*model_mapping.py
If not, remind them these are prerequisites before creating a migration.
询问用户是否已完成:
  1. 更新
    lib/galaxy/model/__init__.py
    中的SQLAlchemy模型
  2. 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
lib/galaxy/model/migrations/alembic/versions_gxy/
with format:
<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>.py

Step 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_table(table_name, *columns)
    - Create new table
  • drop_table(table_name)
    - Drop table
  • add_column(table_name, column)
    - Add column
  • drop_column(table_name, column_name)
    - Drop column
  • alter_column(table_name, column_name, **kw)
    - Modify column
  • create_index(index_name, table_name, columns, **kw)
    - Create index
  • drop_index(index_name, table_name)
    - Drop index
  • create_foreign_key(constraint_name, table_name, columns, referent_table, referent_columns)
    - Create FK
  • create_unique_constraint(constraint_name, table_name, columns)
    - Create unique constraint
  • drop_constraint(constraint_name, table_name)
    - Drop constraint
  • transaction()
    - Context manager for transaction wrapping
Check functions (for conditional migrations):
  • table_exists(table_name, default)
    - Check if table exists
  • column_exists(table_name, column_name, default)
    - Check if column exists
  • index_exists(index_name, table_name, default)
    - Check if index exists
  • foreign_key_exists(constraint_name, table_name, default)
    - Check if FK exists
  • unique_constraint_exists(constraint_name, table_name, default)
    - Check if constraint exists
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():
        # 反向迁移操作
        pass

Step 4: Review Example

步骤4:参考示例

Suggest reading the most recent migration for reference:
bash
undefined
建议查看最新的迁移文件作为参考:
bash
undefined

Find 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 upgrade
bash
./manage_db.sh upgrade

Step 6: Verify

步骤6:验证

Check that:
  1. Migration runs without errors
  2. Database schema matches model
  3. Tests pass:
    ./run_tests.sh -unit test/unit/data/model/mapping/test_*model_mapping.py

检查以下内容:
  1. 迁移运行无错误
  2. 数据库架构与模型匹配
  3. 测试通过:
    ./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 upgrade
This 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.05

or

./manage_db.sh upgrade release_22.05

**Upgrade only gxy branch:**
```bash
./scripts/run_alembic.sh upgrade gxy@head
Upgrade by relative steps:
bash
./scripts/run_alembic.sh upgrade gxy@+1  # One revision forward
Check status before upgrading:
bash
./scripts/db_dev.sh dbversion    # Current version
./scripts/db_dev.sh version      # Head version in codebase
Important 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>-1
Downgrade 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.01

or

./manage_db.sh downgrade release_22.01

**Downgrade gxy branch only:**
```bash
./scripts/run_alembic.sh downgrade gxy@-1  # One revision back
Downgrade to base (empty database):
bash
./scripts/run_alembic.sh downgrade gxy@base
Check current position first:
bash
./scripts/db_dev.sh history --indicate-current
Important 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 dbversion
Output shows current revision(s) with
(head)
marker if up-to-date.
Check head revision in codebase:
bash
./scripts/db_dev.sh version
Shows latest revision IDs for both branches.
View migration history:
bash
./scripts/db_dev.sh history --indicate-current
Shows chronological list with
(current)
and
(head)
markers.
Show specific revision details:
bash
./scripts/db_dev.sh show <revision_id>
Compare database vs codebase:
If
dbversion
shows different revision than
version
, database needs upgrade/downgrade.

检查当前数据库版本:
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>
比较数据库与代码库版本:
如果
dbversion
显示的修订版本与
version
不同,说明数据库需要升级或降级。

If $ARGUMENTS is "troubleshoot": Provide Troubleshooting Guidance

若$ARGUMENTS为"troubleshoot":提供排查指导

Problem: Deadlock detected

问题:检测到死锁

Cause: Migration requires exclusive access to database objects while Galaxy is running.
Solution:
  1. Shut down all Galaxy processes (web servers, job handlers, workflow schedulers)
  2. Run migration again
  3. Restart Galaxy after successful migration
原因: 迁移需要独占访问数据库对象,但Galaxy仍在运行。
解决方案:
  1. 关闭所有Galaxy进程(Web服务器、任务处理器、工作流调度器)
  2. 重新运行迁移
  3. 迁移成功后重启Galaxy

Problem: migrations.IncorrectVersionError

问题:migrations.IncorrectVersionError

Cause: Database not at expected SQLAlchemy Migrate version before Alembic upgrade.
Solution:
  1. Backup database
  2. Check
    migrate_version
    table - should be version 180
  3. If < 180: Checkout 22.01 branch, run old
    manage_db.sh upgrade
  4. If = 181 (rare): Downgrade to 180 using old manage_db.sh
  5. Switch back to current branch
  6. Run
    ./manage_db.sh upgrade
原因: 在进行Alembic升级前,数据库未处于预期的SQLAlchemy Migrate版本。
解决方案:
  1. 备份数据库
  2. 检查
    migrate_version
    表 - 版本应为180
  3. 如果版本<180:切换到22.01分支,运行旧版
    manage_db.sh upgrade
  4. 如果版本=181(罕见情况):使用旧版manage_db.sh降级到180
  5. 切换回当前分支
  6. 运行
    ./manage_db.sh upgrade

Problem: Database version mismatch on startup

问题:启动时数据库版本不匹配

Error: "Database is at revision X but codebase expects revision Y"
Solution:
  1. Check which is ahead:
    bash
    ./scripts/db_dev.sh dbversion
    ./scripts/db_dev.sh version
  2. If database behind:
    ./manage_db.sh upgrade
  3. If database ahead: Either upgrade codebase or downgrade database
错误信息: "Database is at revision X but codebase expects revision Y"
解决方案:
  1. 检查哪一方版本更新:
    bash
    ./scripts/db_dev.sh dbversion
    ./scripts/db_dev.sh version
  2. 如果数据库版本落后:
    ./manage_db.sh upgrade
  3. 如果数据库版本超前:升级代码库或降级数据库

Problem: Migration fails with "table already exists"

问题:迁移失败,提示"table already exists"

Cause: Migration not idempotent or database in unexpected state.
Solution:
  1. Check if table/column already exists in database
  2. 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", ...)
  3. Consider using
    --repair
    flag if implementing manual fixes
原因: 迁移不具备幂等性,或数据库处于意外状态。
解决方案:
  1. 检查表/列是否已存在于数据库中
  2. 在迁移中使用检查函数:
    python
    from galaxy.model.migrations.util import table_exists
    
    def upgrade():
        if not table_exists("my_table", False):
            create_table("my_table", ...)
  3. 如果需要手动修复,考虑使用
    --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:
  1. Clean up orphaned rows before adding constraint
  2. Add data migration in upgrade() before schema change
  3. Use
    with transaction():
    to ensure atomicity

原因: 迁移尝试添加外键,但违反了引用完整性。
解决方案:
  1. 在添加约束前清理孤立的行
  2. 在架构变更前的upgrade()中添加数据迁移
  3. 使用
    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:
    lib/galaxy/model/migrations/alembic/versions_gxy/
    (check latest files)
External documentation:
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/
    (查看最新文件)
外部文档:
需遵循的通用模式:
  • 始终将操作包裹在
    with transaction():
  • 使用Galaxy工具函数而非原始Alembic操作
  • 同时实现upgrade()和downgrade()
  • 在提交前在开发数据库上测试迁移
  • 使用描述性的修订消息