fix: enable batch operations in alembic for sqlite unique constraints

This commit is contained in:
mwiegand 2026-05-06 20:59:18 +02:00
parent 7e5a8f89b5
commit c01359002a
No known key found for this signature in database
2 changed files with 10 additions and 3 deletions

View file

@ -25,6 +25,7 @@ def run_migrations_offline() -> None:
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
render_as_batch=True
)
with context.begin_transaction():
@ -41,7 +42,11 @@ def run_migrations_online() -> None:
)
with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata)
context.configure(
connection=connection,
target_metadata=target_metadata,
render_as_batch=True
)
with context.begin_transaction():
context.run_migrations()

View file

@ -21,12 +21,14 @@ depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.create_unique_constraint(None, 'servers', ['port'])
with op.batch_alter_table('servers', schema=None) as batch_op:
batch_op.create_unique_constraint(batch_op.f('uq_servers_port'), ['port'])
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, 'servers', type_='unique')
with op.batch_alter_table('servers', schema=None) as batch_op:
batch_op.drop_constraint(batch_op.f('uq_servers_port'), type_='unique')
# ### end Alembic commands ###