Skip to content

Commit 2793416

Browse files
committed
check all directives in batch block until recreate selected
Fixed regression in batch mode due to 🎫`883` where the "auto" mode of batch would fail to accommodate any additional migration directives beyond encountering an ``add_column()`` directive, due to a mis-application of the conditional logic that was added as part of this change, leading to "recreate" mode not being used in cases where it is required for SQLite such as for unique constraints. Change-Id: I6315569caff5f3b33d152974ebecc8b18d9cc523 Fixes: #896
1 parent ff8fd16 commit 2793416

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

alembic/ddl/sqlite.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ def requires_recreate_in_batch(
5656
and col.server_default.persisted
5757
):
5858
return True
59-
return False
6059
elif op[0] not in ("create_index", "drop_index"):
6160
return True
6261
else:

docs/build/unreleased/896.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.. change::
2+
:tags: bug, regression, batch
3+
:tickets: 896
4+
5+
Fixed regression in batch mode due to :ticket:`883` where the "auto" mode
6+
of batch would fail to accommodate any additional migration directives
7+
beyond encountering an ``add_column()`` directive, due to a mis-application
8+
of the conditional logic that was added as part of this change, leading to
9+
"recreate" mode not being used in cases where it is required for SQLite
10+
such as for unique constraints.

tests/test_batch.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,19 +1661,21 @@ def test_drop_pk_col_readd_col_also_pk_const(self):
16611661
pk_const = inspect(self.conn).get_pk_constraint("foo")
16621662
eq_(pk_const["constrained_columns"], ["id"])
16631663

1664-
def test_add_pk_constraint(self):
1664+
@testing.combinations(("always",), ("auto",), argnames="recreate")
1665+
def test_add_pk_constraint(self, recreate):
16651666
self._no_pk_fixture()
1666-
with self.op.batch_alter_table("nopk", recreate="always") as batch_op:
1667+
with self.op.batch_alter_table("nopk", recreate=recreate) as batch_op:
16671668
batch_op.create_primary_key("newpk", ["a", "b"])
16681669

16691670
pk_const = inspect(self.conn).get_pk_constraint("nopk")
16701671
with config.requirements.reflects_pk_names.fail_if():
16711672
eq_(pk_const["name"], "newpk")
16721673
eq_(pk_const["constrained_columns"], ["a", "b"])
16731674

1675+
@testing.combinations(("always",), ("auto",), argnames="recreate")
16741676
@config.requirements.check_constraint_reflection
1675-
def test_add_ck_constraint(self):
1676-
with self.op.batch_alter_table("foo", recreate="always") as batch_op:
1677+
def test_add_ck_constraint(self, recreate):
1678+
with self.op.batch_alter_table("foo", recreate=recreate) as batch_op:
16771679
batch_op.create_check_constraint("newck", text("x > 0"))
16781680

16791681
ck_consts = inspect(self.conn).get_check_constraints("foo")
@@ -1682,12 +1684,13 @@ def test_add_ck_constraint(self):
16821684
)
16831685
eq_(ck_consts, [{"sqltext": "x > 0", "name": "newck"}])
16841686

1687+
@testing.combinations(("always",), ("auto",), argnames="recreate")
16851688
@config.requirements.check_constraint_reflection
1686-
def test_drop_ck_constraint(self):
1689+
def test_drop_ck_constraint(self, recreate):
16871690
self._ck_constraint_fixture()
16881691

16891692
with self.op.batch_alter_table(
1690-
"ck_table", recreate="always"
1693+
"ck_table", recreate=recreate
16911694
) as batch_op:
16921695
batch_op.drop_constraint("ck", "check")
16931696

@@ -1736,6 +1739,36 @@ def _assert_table_comment(self, tname, comment):
17361739
tcomment = insp.get_table_comment(tname)
17371740
eq_(tcomment, {"text": comment})
17381741

1742+
@testing.combinations(("always",), ("auto",), argnames="recreate")
1743+
def test_add_uq(self, recreate):
1744+
with self.op.batch_alter_table("foo", recreate=recreate) as batch_op:
1745+
batch_op.create_unique_constraint("newuk", ["x"])
1746+
1747+
uq_consts = inspect(self.conn).get_unique_constraints("foo")
1748+
eq_(
1749+
[
1750+
{"name": uc["name"], "column_names": uc["column_names"]}
1751+
for uc in uq_consts
1752+
],
1753+
[{"name": "newuk", "column_names": ["x"]}],
1754+
)
1755+
1756+
@testing.combinations(("always",), ("auto",), argnames="recreate")
1757+
def test_add_uq_plus_col(self, recreate):
1758+
with self.op.batch_alter_table("foo", recreate=recreate) as batch_op:
1759+
batch_op.add_column(Column("y", Integer))
1760+
batch_op.create_unique_constraint("newuk", ["x", "y"])
1761+
1762+
uq_consts = inspect(self.conn).get_unique_constraints("foo")
1763+
1764+
eq_(
1765+
[
1766+
{"name": uc["name"], "column_names": uc["column_names"]}
1767+
for uc in uq_consts
1768+
],
1769+
[{"name": "newuk", "column_names": ["x", "y"]}],
1770+
)
1771+
17391772
@config.requirements.comments
17401773
def test_add_table_comment(self):
17411774
with self.op.batch_alter_table("foo") as batch_op:

0 commit comments

Comments
 (0)