]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Fix column existence check in batch operation
authorCaselIT <cfederico87@gmail.com>
Fri, 4 Sep 2020 19:18:27 +0000 (21:18 +0200)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 10 Sep 2020 21:11:27 +0000 (17:11 -0400)
Existence of a column check used ``ColumnCollection.contains_column`` with a
the name of the column instead of the column instance.

Change-Id: I41d9f6b6ed9e44eeb9ced78b039da6261491eeee

.gitignore
alembic/operations/batch.py
docs/build/unreleased/add_column.rst [new file with mode: 0644]
tests/test_batch.py

index 4f13bf55e2e6d91d6e94b2a06eb0ccd5744b1693..b2a27bd30910e72d1da3be147a540a58723369d8 100644 (file)
@@ -16,4 +16,5 @@ coverage.xml
 /test_schema.db
 .idea/
 .vscode/
-.pytest_cache/
\ No newline at end of file
+.pytest_cache/
+/docs/build/_build/
index eb0214c8da6673ebc2a484dcfe5dbe8b748df94c..b0b04157c22b32c5453b544a72e0d9af10767f6c 100644 (file)
@@ -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 (file)
index 0000000..1e3c8a6
--- /dev/null
@@ -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
index 90d2a4ffd22ed3cc5caa2268cc30c8e55c6f71a6..faff4f58c479a14bead228572819475b6763c23d 100644 (file)
@@ -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"))