Skip to content

Commit

Permalink
Use SQLAlchemy _copy() when available
Browse files Browse the repository at this point in the history
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
  • Loading branch information
zzzeek committed Feb 18, 2021
1 parent 8e5b861 commit 490d9fa
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
15 changes: 9 additions & 6 deletions alembic/operations/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

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
Expand Down Expand Up @@ -194,7 +195,7 @@ def _grab_table_elements(self):
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
Expand Down Expand Up @@ -297,16 +298,18 @@ def _transfer_elements_to_new_table(self):
# 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)
Expand Down Expand Up @@ -503,7 +506,7 @@ def add_column(
)
# 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):
Expand Down
3 changes: 2 additions & 1 deletion alembic/operations/toimpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from . import ops
from .base import Operations
from ..util.sqla_compat import _copy


@Operations.implementation_for(ops.AlterColumnOp)
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions alembic/util/sqla_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions docs/build/unreleased/copy_change.rst
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 490d9fa

Please sign in to comment.