Parcourir la source

chore(api): fix Alembic offline migration compatibility (#24795)

This PR fixes Alembic offline mode (`--sql` flag) by ensuring data migration functions only execute in online mode. When running in offline mode, these functions now skip data operations and output informational comments to the generated SQL.
tags/1.8.1
QuantumGhost il y a 2 mois
Parent
révision
8d60e5c342
Aucun compte lié à l'adresse e-mail de l'auteur

+ 6
- 0
.github/workflows/db-migration-test.yml Voir le fichier



- name: Install dependencies - name: Install dependencies
run: uv sync --project api run: uv sync --project api
- name: Ensure Offline migration are supported
run: |
# upgrade
uv run --directory api flask db upgrade 'base:head' --sql
# downgrade
uv run --directory api flask db downgrade 'head:base' --sql


- name: Prepare middleware env - name: Prepare middleware env
run: | run: |

+ 20
- 3
api/migrations/versions/2025_08_09_1553-e8446f481c1e_add_provider_credential_pool_support.py Voir le fichier

Create Date: 2025-08-09 15:53:54.341341 Create Date: 2025-08-09 15:53:54.341341


""" """
from alembic import op
from alembic import op, context
from libs.uuid_utils import uuidv7 from libs.uuid_utils import uuidv7
import models as models import models as models
import sqlalchemy as sa import sqlalchemy as sa
with op.batch_alter_table('load_balancing_model_configs', schema=None) as batch_op: with op.batch_alter_table('load_balancing_model_configs', schema=None) as batch_op:
batch_op.add_column(sa.Column('credential_id', models.types.StringUUID(), nullable=True)) batch_op.add_column(sa.Column('credential_id', models.types.StringUUID(), nullable=True))


migrate_existing_providers_data()
if not context.is_offline_mode():
migrate_existing_providers_data()
else:
op.execute(
'-- [IMPORTANT] Data migration skipped!!!\n'
"-- You should manually run data migration function `migrate_existing_providers_data`\n"
f"-- inside file {__file__}\n"
"-- Please review the migration script carefully!"
)


# Remove encrypted_config column from providers table after migration # Remove encrypted_config column from providers table after migration
with op.batch_alter_table('providers', schema=None) as batch_op: with op.batch_alter_table('providers', schema=None) as batch_op:
batch_op.add_column(sa.Column('encrypted_config', sa.Text(), nullable=True)) batch_op.add_column(sa.Column('encrypted_config', sa.Text(), nullable=True))


# Migrate data back from provider_credentials to providers # Migrate data back from provider_credentials to providers
migrate_data_back_to_providers()

if not context.is_offline_mode():
migrate_data_back_to_providers()
else:
op.execute(
'-- [IMPORTANT] Data migration skipped!!!\n'
"-- You should manually run data migration function `migrate_data_back_to_providers`\n"
f"-- inside file {__file__}\n"
"-- Please review the migration script carefully!"
)


# Remove credential_id columns # Remove credential_id columns
with op.batch_alter_table('load_balancing_model_configs', schema=None) as batch_op: with op.batch_alter_table('load_balancing_model_configs', schema=None) as batch_op:

+ 21
- 5
api/migrations/versions/2025_08_13_1605-0e154742a5fa_add_provider_model_multi_credential.py Voir le fichier



""" """


from alembic import op
from alembic import op, context
from libs.uuid_utils import uuidv7 from libs.uuid_utils import uuidv7
import models as models import models as models
import sqlalchemy as sa import sqlalchemy as sa
with op.batch_alter_table('load_balancing_model_configs', schema=None) as batch_op: with op.batch_alter_table('load_balancing_model_configs', schema=None) as batch_op:
batch_op.add_column(sa.Column('credential_source_type', sa.String(length=40), nullable=True)) batch_op.add_column(sa.Column('credential_source_type', sa.String(length=40), nullable=True))


# Migrate existing provider_models data
migrate_existing_provider_models_data()
if not context.is_offline_mode():
# Migrate existing provider_models data
migrate_existing_provider_models_data()
else:
op.execute(
'-- [IMPORTANT] Data migration skipped!!!\n'
"-- You should manually run data migration function `migrate_existing_provider_models_data`\n"
f"-- inside file {__file__}\n"
"-- Please review the migration script carefully!"
)


# Remove encrypted_config column from provider_models table after migration # Remove encrypted_config column from provider_models table after migration
with op.batch_alter_table('provider_models', schema=None) as batch_op: with op.batch_alter_table('provider_models', schema=None) as batch_op:
with op.batch_alter_table('provider_models', schema=None) as batch_op: with op.batch_alter_table('provider_models', schema=None) as batch_op:
batch_op.add_column(sa.Column('encrypted_config', sa.Text(), nullable=True)) batch_op.add_column(sa.Column('encrypted_config', sa.Text(), nullable=True))


# Migrate data back from provider_model_credentials to provider_models
migrate_data_back_to_provider_models()
if not context.is_offline_mode():
# Migrate data back from provider_model_credentials to provider_models
migrate_data_back_to_provider_models()
else:
op.execute(
'-- [IMPORTANT] Data migration skipped!!!\n'
"-- You should manually run data migration function `migrate_data_back_to_provider_models`\n"
f"-- inside file {__file__}\n"
"-- Please review the migration script carefully!"
)


with op.batch_alter_table('provider_models', schema=None) as batch_op: with op.batch_alter_table('provider_models', schema=None) as batch_op:
batch_op.drop_column('credential_id') batch_op.drop_column('credential_id')

Chargement…
Annuler
Enregistrer