From 490d9fa42b095cedb17abee17a063ee108992959 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 18 Feb 2021 15:31:42 -0500 Subject: [PATCH] Use SQLAlchemy _copy() when available 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 | 15 +++++++++------ alembic/operations/toimpl.py | 3 ++- alembic/util/sqla_compat.py | 7 +++++++ docs/build/unreleased/copy_change.rst | 5 +++++ 4 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 docs/build/unreleased/copy_change.rst diff --git a/alembic/operations/batch.py b/alembic/operations/batch.py index f0291936..990b3a88 100644 --- a/alembic/operations/batch.py +++ b/alembic/operations/batch.py @@ -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): diff --git a/alembic/operations/toimpl.py b/alembic/operations/toimpl.py index c6dbafbf..10a41e48 100644 --- a/alembic/operations/toimpl.py +++ b/alembic/operations/toimpl.py @@ -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) diff --git a/alembic/util/sqla_compat.py b/alembic/util/sqla_compat.py index 7cb3fbd8..91e22d38 100644 --- a/alembic/util/sqla_compat.py +++ b/alembic/util/sqla_compat.py @@ -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 index 00000000..0cdb8558 --- /dev/null +++ b/docs/build/unreleased/copy_change.rst @@ -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. -- 2.47.2