You've already forked DataMate
Update data-annotation-init.sql and Alembic migration to support both new and old deployments: SQL Initialization Script (data-annotation-init.sql): - Add file_version column to t_dm_annotation_results table - Add Alembic version table creation and version insertion - New deployments using this script will have latest schema and Alembic version marked Alembic Migration (20250205_0001_add_file_version.py): - Add column_exists() helper function to detect if column already exists - Add compatibility check in upgrade(): skip if column exists (new SQL init) - Add informative print messages for deployment clarity - Enhanced docstrings explaining compatibility strategy Deployment Scenarios: 1. New deployment with latest SQL script: Schema created with file_version, Alembic marked as applied 2. Old deployment upgrade: Alembic detects missing column and adds it This ensures backward compatibility while supporting fresh installs with complete schema.
68 lines
2.2 KiB
Python
68 lines
2.2 KiB
Python
"""add file_version to annotation_results
|
|
|
|
Revision ID: 20250205_0001
|
|
Revises:
|
|
Create Date: 2025-02-05 00:00:00.000000
|
|
|
|
说明:
|
|
- 此迁移脚本用于升级旧版本数据库(使用 2025-02-05 之前的 SQL 脚本初始化)
|
|
- 如果使用最新的 data-annotation-init.sql 初始化,此脚本会自动跳过
|
|
(因为 alembic_version 表已包含此版本记录)
|
|
|
|
"""
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
from sqlalchemy import inspect
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision = "20250205_0001"
|
|
down_revision = None
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def column_exists(table_name: str, column_name: str) -> bool:
|
|
"""检查表中是否已存在指定列"""
|
|
conn = op.get_bind()
|
|
inspector = inspect(conn)
|
|
columns = [col['name'] for col in inspector.get_columns(table_name)]
|
|
return column_name in columns
|
|
|
|
|
|
def upgrade() -> None:
|
|
"""
|
|
升级:添加 file_version 字段到 t_dm_annotation_results 表
|
|
|
|
兼容性处理:
|
|
- 如果字段已存在(使用最新 SQL 脚本初始化),则跳过
|
|
- 如果字段不存在(旧版本数据库),则添加字段
|
|
"""
|
|
if column_exists("t_dm_annotation_results", "file_version"):
|
|
# 字段已存在,说明使用的是最新 SQL 初始化脚本
|
|
# Alembic 会自动记录版本,无需重复添加字段
|
|
print("字段 file_version 已存在,跳过添加操作(使用最新 SQL 脚本初始化)")
|
|
return
|
|
|
|
# 字段不存在,添加字段(旧版本数据库升级)
|
|
op.add_column(
|
|
"t_dm_annotation_results",
|
|
sa.Column(
|
|
"file_version", sa.BigInteger(), nullable=True, comment="标注时的文件版本号"
|
|
),
|
|
)
|
|
print("成功添加 file_version 字段到 t_dm_annotation_results 表")
|
|
|
|
|
|
def downgrade() -> None:
|
|
"""
|
|
回滚:删除 file_version 字段
|
|
|
|
注意:如果使用的是最新 SQL 脚本初始化,回滚此迁移不会删除字段
|
|
因为字段是初始结构的一部分
|
|
"""
|
|
if column_exists("t_dm_annotation_results", "file_version"):
|
|
op.drop_column("t_dm_annotation_results", "file_version")
|
|
print("成功删除 file_version 字段")
|