]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Use SQLAlchemy _copy() when available
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 18 Feb 2021 20:31:42 +0000 (15:31 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 18 Feb 2021 20:32:09 +0000 (15:32 -0500)
Adjusted the use of SQLAlchemy's ".copy()" internals to use "._copy()"
for version 1.4.0, as this method is being renamed.

Change-Id: I1e69a1628e408f06b43efbc0cc52fc0ad1e8cbc4

alembic/operations/batch.py
alembic/operations/toimpl.py
alembic/util/sqla_compat.py
docs/build/unreleased/copy_change.rst [new file with mode: 0644]

index f0291936e390fb79138ec79ea7cce6a46bd0cf18..990b3a887e1b6d67a83681f274a53fcb4889c251 100644 (file)
@@ -13,6 +13,7 @@ from sqlalchemy.util import topological
 
 from ..util import exc
 from ..util.sqla_compat import _columns_for_constraint
+from ..util.sqla_compat import _copy
 from ..util.sqla_compat import _ensure_scope_for_ddl
 from ..util.sqla_compat import _fk_is_self_referential
 from ..util.sqla_compat import _insert_inline
@@ -194,7 +195,7 @@ class ApplyBatchImpl(object):
         schema = self.table.schema
         self.columns = OrderedDict()
         for c in self.table.c:
-            c_copy = c.copy(schema=schema)
+            c_copy = _copy(c, schema=schema)
             c_copy.unique = c_copy.index = False
             # ensure that the type object was copied,
             # as we may need to modify it in-place
@@ -297,16 +298,18 @@ class ApplyBatchImpl(object):
                     # FK constraints from other tables; we assume SQLite
                     # no foreign keys just keeps the names unchanged, so
                     # when we rename back, they match again.
-                    const_copy = const.copy(
-                        schema=schema, target_table=self.table
+                    const_copy = _copy(
+                        const, schema=schema, target_table=self.table
                     )
                 else:
                     # "target_table" for ForeignKeyConstraint.copy() is
                     # only used if the FK is detected as being
                     # self-referential, which we are handling above.
-                    const_copy = const.copy(schema=schema)
+                    const_copy = _copy(const, schema=schema)
             else:
-                const_copy = const.copy(schema=schema, target_table=new_table)
+                const_copy = _copy(
+                    const, schema=schema, target_table=new_table
+                )
             if isinstance(const, ForeignKeyConstraint):
                 self._setup_referent(m, const)
             new_table.append_constraint(const_copy)
@@ -503,7 +506,7 @@ class ApplyBatchImpl(object):
         )
         # we copy the column because operations.add_column()
         # gives us a Column that is part of a Table already.
-        self.columns[column.name] = column.copy(schema=self.table.schema)
+        self.columns[column.name] = _copy(column, schema=self.table.schema)
         self.column_transfers[column.name] = {}
 
     def drop_column(self, table_name, column, **kw):
index c6dbafbf1a0fad88a281f0c171fa60f5dc14355e..10a41e48c1016d3850997e25598d65ce376b3c71 100644 (file)
@@ -2,6 +2,7 @@ from sqlalchemy import schema as sa_schema
 
 from . import ops
 from .base import Operations
+from ..util.sqla_compat import _copy
 
 
 @Operations.implementation_for(ops.AlterColumnOp)
@@ -128,7 +129,7 @@ def add_column(operations, operation):
     kw = operation.kw
 
     if column.table is not None:
-        column = column.copy()
+        column = _copy(column)
 
     t = operations.schema_obj.table(table_name, column, schema=schema)
     operations.impl.add_column(table_name, column, schema=schema, **kw)
index 7cb3fbd813e7e98decd50e8cd9a50d1a44f601dd..91e22d38455847d2ed78161a2b9b78b3464920cf 100644 (file)
@@ -98,6 +98,13 @@ def _get_connection_in_transaction(connection):
         return in_transaction()
 
 
+def _copy(schema_item, **kw):
+    if hasattr(schema_item, "_copy"):
+        return schema_item._copy(**kw)
+    else:
+        return schema_item.copy(**kw)
+
+
 def _get_connection_transaction(connection):
     if sqla_14:
         return connection.get_transaction()
diff --git a/docs/build/unreleased/copy_change.rst b/docs/build/unreleased/copy_change.rst
new file mode 100644 (file)
index 0000000..0cdb855
--- /dev/null
@@ -0,0 +1,5 @@
+.. change::
+    :tags: bug
+
+    Adjusted the use of SQLAlchemy's ".copy()" internals to use "._copy()"
+    for version 1.4.0, as this method is being renamed.