From: CaselIT Date: Fri, 4 Sep 2020 19:18:27 +0000 (+0200) Subject: Fix column existence check in batch operation X-Git-Tag: rel_1_4_3~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1de1070972f35978be99631392168d991b1c96b2;p=thirdparty%2Fsqlalchemy%2Falembic.git Fix column existence check in batch operation Existence of a column check used ``ColumnCollection.contains_column`` with a the name of the column instead of the column instance. Change-Id: I41d9f6b6ed9e44eeb9ced78b039da6261491eeee --- diff --git a/.gitignore b/.gitignore index 4f13bf55..b2a27bd3 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,5 @@ coverage.xml /test_schema.db .idea/ .vscode/ -.pytest_cache/ \ No newline at end of file +.pytest_cache/ +/docs/build/_build/ diff --git a/alembic/operations/batch.py b/alembic/operations/batch.py index eb0214c8..b0b04157 100644 --- a/alembic/operations/batch.py +++ b/alembic/operations/batch.py @@ -335,7 +335,7 @@ class ApplyBatchImpl(object): t = metadata.tables[key] for elem in constraint.elements: colname = elem._get_colspec().split(".")[-1] - if not t.c.contains_column(colname): + if colname not in t.c: t.append_column(Column(colname, sqltypes.NULLTYPE)) else: Table( diff --git a/docs/build/unreleased/add_column.rst b/docs/build/unreleased/add_column.rst new file mode 100644 index 00000000..1e3c8a69 --- /dev/null +++ b/docs/build/unreleased/add_column.rst @@ -0,0 +1,9 @@ +.. change:: + :tags: bug, batch + + Fixed issue where columns in a foreign-key referenced table would be + replaced with null-type columns during a batch operation; while this did + not generally have any side effects, it could theoretically impact a batch + operation that also targets that table directly and also would interfere + with future changes to the ``.append_column()`` method to disallow implicit + replacement of columns. \ No newline at end of file diff --git a/tests/test_batch.py b/tests/test_batch.py index 90d2a4ff..faff4f58 100644 --- a/tests/test_batch.py +++ b/tests/test_batch.py @@ -35,6 +35,7 @@ from alembic.testing import assert_raises_message from alembic.testing import config from alembic.testing import eq_ from alembic.testing import exclusions +from alembic.testing import is_ from alembic.testing import mock from alembic.testing import TestBase from alembic.testing.fixtures import op_fixture @@ -660,6 +661,21 @@ class BatchApplyTest(TestBase): schema="foo_schema", ) + def test_do_not_add_existing_columns_columns(self): + impl = self._multi_fk_fixture() + meta = impl.table.metadata + + cid = Column("id", Integer()) + user = Table("user", meta, cid) + + fk = [ + c + for c in impl.unnamed_constraints + if isinstance(c, ForeignKeyConstraint) + ] + impl._setup_referent(meta, fk[0]) + is_(user.c.id, cid) + def test_drop_col(self): impl = self._simple_fixture() impl.drop_column("tname", column("x"))