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
| - 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: | |
| 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: |
| """ | """ | ||||
| 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') |