From 94becb35c687fc8d9a25cc75b601ec3228214540 Mon Sep 17 00:00:00 2001 From: Ymage Date: Sat, 24 Nov 2018 12:04:23 +0100 Subject: [PATCH] Support SQLite ALTER with batch feature during alembic migrate Fix #404 --- migrations/env.py | 1 + .../1274ed462010_change_setting_value_data_type.py | 10 ++++++---- .../1274ed462010_remove_all_settings_in_the_db.py | 8 +++++--- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/migrations/env.py b/migrations/env.py index 23663ff..6a10e6d 100755 --- a/migrations/env.py +++ b/migrations/env.py @@ -73,6 +73,7 @@ def run_migrations_online(): context.configure(connection=connection, target_metadata=target_metadata, process_revision_directives=process_revision_directives, + render_as_batch=config.get_main_option('sqlalchemy.url').startswith('sqlite:'), **current_app.extensions['migrate'].configure_args) try: diff --git a/migrations/versions/1274ed462010_change_setting_value_data_type.py b/migrations/versions/1274ed462010_change_setting_value_data_type.py index 91600ca..b68df36 100644 --- a/migrations/versions/1274ed462010_change_setting_value_data_type.py +++ b/migrations/versions/1274ed462010_change_setting_value_data_type.py @@ -33,8 +33,9 @@ def update_data(): ) def upgrade(): - # change column data type - op.alter_column('setting', 'value', existing_type=sa.String(256), type_=sa.Text()) + with op.batch_alter_table('setting') as batch_op: + # change column data type + batch_op.alter_column('value', existing_type=sa.String(256), type_=sa.Text()) # update data for new schema update_data() @@ -42,5 +43,6 @@ def upgrade(): def downgrade(): # delete added records in previous version op.execute("DELETE FROM setting WHERE id > 41") - # change column data type - op.alter_column('setting', 'value', existing_type=sa.Text(), type_=sa.String(256)) + with op.batch_alter_table('setting') as batch_op: + # change column data type + batch_op.alter_column('value', existing_type=sa.Text(), type_=sa.String(256)) diff --git a/migrations/versions/1274ed462010_remove_all_settings_in_the_db.py b/migrations/versions/1274ed462010_remove_all_settings_in_the_db.py index f1f54e4..e01768e 100644 --- a/migrations/versions/1274ed462010_remove_all_settings_in_the_db.py +++ b/migrations/versions/1274ed462010_remove_all_settings_in_the_db.py @@ -23,8 +23,10 @@ def upgrade(): # written to the DB. op.execute("DELETE FROM setting") - # drop view column since we don't need it - op.drop_column('setting', 'view') + with op.batch_alter_table('setting') as batch_op: + # drop view column since we don't need it + batch_op.drop_column('view') def downgrade(): - op.add_column('setting', sa.Column('view', sa.String(length=64), nullable=True)) + with op.batch_alter_table('setting') as batch_op: + batch_op.add_column(sa.Column('view', sa.String(length=64), nullable=True))