]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Preserve comment and info in Drop/CreateTableOp
authorNicolas CANIART <nicolas@caniart.net>
Tue, 10 Aug 2021 14:36:55 +0000 (10:36 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 10 Aug 2021 16:05:07 +0000 (12:05 -0400)
Fixed regression due to :ticket:`803` where the ``.info`` and ``.comment``
attributes of ``Table`` would be lost inside of the :class:`.DropTableOp`
class, which when "reversed" into a :class:`.CreateTableOp` would then have
lost these elements. Pull request courtesy Nicolas CANIART.

Fixes: #879
Closes: #881
Pull-request: https://github.com/sqlalchemy/alembic/pull/881
Pull-request-sha: e509b9e8180085998e32746721eeb9266b9f2145

Change-Id: I84a0680266fcf80f3dc922f3cbc76dabd74eedd3

alembic/operations/ops.py
docs/build/unreleased/879.rst [new file with mode: 0644]
tests/test_op.py

index 87ad552d80ec15fdde8733887fd9d87c3ed1292e..89793554f407119986ef9e3667b7652c9145716f 100644 (file)
@@ -198,7 +198,7 @@ class CreatePrimaryKeyOp(AddConstraintOp):
             constraint_table.name,
             constraint.columns.keys(),
             schema=constraint_table.schema,
-            **constraint.dialect_kwargs
+            **constraint.dialect_kwargs,
         )
 
     def to_constraint(self, migration_context=None):
@@ -208,7 +208,7 @@ class CreatePrimaryKeyOp(AddConstraintOp):
             self.table_name,
             self.columns,
             schema=self.schema,
-            **self.kw
+            **self.kw,
         )
 
     @classmethod
@@ -310,7 +310,7 @@ class CreateUniqueConstraintOp(AddConstraintOp):
             constraint_table.name,
             [c.name for c in constraint.columns],
             schema=constraint_table.schema,
-            **kw
+            **kw,
         )
 
     def to_constraint(self, migration_context=None):
@@ -320,7 +320,7 @@ class CreateUniqueConstraintOp(AddConstraintOp):
             self.table_name,
             self.columns,
             schema=self.schema,
-            **self.kw
+            **self.kw,
         )
 
     @classmethod
@@ -459,7 +459,7 @@ class CreateForeignKeyOp(AddConstraintOp):
             target_table,
             source_columns,
             target_columns,
-            **kw
+            **kw,
         )
 
     def to_constraint(self, migration_context=None):
@@ -470,7 +470,7 @@ class CreateForeignKeyOp(AddConstraintOp):
             self.referent_table,
             self.local_cols,
             self.remote_cols,
-            **self.kw
+            **self.kw,
         )
 
     @classmethod
@@ -549,7 +549,7 @@ class CreateForeignKeyOp(AddConstraintOp):
             referent_schema=referent_schema,
             initially=initially,
             match=match,
-            **dialect_kw
+            **dialect_kw,
         )
         return operations.invoke(op)
 
@@ -600,7 +600,7 @@ class CreateForeignKeyOp(AddConstraintOp):
             referent_schema=referent_schema,
             initially=initially,
             match=match,
-            **dialect_kw
+            **dialect_kw,
         )
         return operations.invoke(op)
 
@@ -635,7 +635,7 @@ class CreateCheckConstraintOp(AddConstraintOp):
             constraint_table.name,
             constraint.sqltext,
             schema=constraint_table.schema,
-            **constraint.dialect_kwargs
+            **constraint.dialect_kwargs,
         )
 
     def to_constraint(self, migration_context=None):
@@ -645,7 +645,7 @@ class CreateCheckConstraintOp(AddConstraintOp):
             self.table_name,
             self.condition,
             schema=self.schema,
-            **self.kw
+            **self.kw,
         )
 
     @classmethod
@@ -721,7 +721,7 @@ class CreateCheckConstraintOp(AddConstraintOp):
             operations.impl.table_name,
             condition,
             schema=operations.impl.schema,
-            **kw
+            **kw,
         )
         return operations.invoke(op)
 
@@ -755,7 +755,7 @@ class CreateIndexOp(MigrateOperation):
             sqla_compat._get_index_expressions(index),
             schema=index.table.schema,
             unique=index.unique,
-            **index.kwargs
+            **index.kwargs,
         )
 
     def to_index(self, migration_context=None):
@@ -767,7 +767,7 @@ class CreateIndexOp(MigrateOperation):
             self.columns,
             schema=self.schema,
             unique=self.unique,
-            **self.kw
+            **self.kw,
         )
         return idx
 
@@ -844,7 +844,7 @@ class CreateIndexOp(MigrateOperation):
             operations.impl.table_name,
             columns,
             schema=operations.impl.schema,
-            **kw
+            **kw,
         )
         return operations.invoke(op)
 
@@ -876,7 +876,7 @@ class DropIndexOp(MigrateOperation):
             index.table.name,
             schema=index.table.schema,
             _reverse=CreateIndexOp.from_index(index),
-            **index.kwargs
+            **index.kwargs,
         )
 
     def to_index(self, migration_context=None):
@@ -889,7 +889,7 @@ class DropIndexOp(MigrateOperation):
             self.table_name,
             self._reverse.columns if self._reverse else ["x"],
             schema=self.schema,
-            **self.kw
+            **self.kw,
         )
 
     @classmethod
@@ -935,7 +935,7 @@ class DropIndexOp(MigrateOperation):
             index_name,
             table_name=operations.impl.table_name,
             schema=operations.impl.schema,
-            **kw
+            **kw,
         )
         return operations.invoke(op)
 
@@ -956,6 +956,7 @@ class CreateTableOp(MigrateOperation):
         self.table_name = table_name
         self.columns = columns
         self.schema = schema
+        self.info = kw.pop("info", {})
         self.comment = kw.pop("comment", None)
         self.prefixes = kw.pop("prefixes", None)
         self.kw = kw
@@ -988,8 +989,9 @@ class CreateTableOp(MigrateOperation):
             # not doubled up. see #844 #848
             _constraints_included=True,
             comment=table.comment,
-            prefixes=table._prefixes,
-            **table.kwargs
+            info=table.info.copy(),
+            prefixes=list(table._prefixes),
+            **table.kwargs,
         )
 
     def to_table(self, migration_context=None):
@@ -999,10 +1001,11 @@ class CreateTableOp(MigrateOperation):
             self.table_name,
             *self.columns,
             schema=self.schema,
-            prefixes=self.prefixes,
+            prefixes=list(self.prefixes) if self.prefixes else [],
             comment=self.comment,
+            info=self.info.copy() if self.info else {},
             _constraints_included=self._constraints_included,
-            **self.kw
+            **self.kw,
         )
 
     @classmethod
@@ -1095,6 +1098,9 @@ class DropTableOp(MigrateOperation):
         self.table_name = table_name
         self.schema = schema
         self.table_kw = table_kw or {}
+        self.comment = self.table_kw.pop("comment", None)
+        self.info = self.table_kw.pop("info", None)
+        self.prefixes = self.table_kw.pop("prefixes", None)
         self._reverse = _reverse
 
     def to_diff_tuple(self):
@@ -1108,7 +1114,12 @@ class DropTableOp(MigrateOperation):
         return cls(
             table.name,
             schema=table.schema,
-            table_kw=table.kwargs,
+            table_kw={
+                "comment": table.comment,
+                "info": table.info.copy(),
+                "prefixes": list(table._prefixes),
+                **table.kwargs,
+            },
             _reverse=CreateTableOp.from_table(
                 table, _namespace_metadata=_namespace_metadata
             ),
@@ -1124,10 +1135,13 @@ class DropTableOp(MigrateOperation):
         t = schema_obj.table(
             self.table_name,
             *cols_and_constraints,
+            comment=self.comment,
+            info=self.info.copy() if self.info else {},
+            prefixes=list(self.prefixes) if self.prefixes else [],
             schema=self.schema,
             _constraints_included=bool(self._reverse)
             and self._reverse._constraints_included,
-            **self.table_kw
+            **self.table_kw,
         )
         return t
 
@@ -1649,7 +1663,7 @@ class AlterColumnOp(AlterTableOp):
             modify_server_default=server_default,
             modify_nullable=nullable,
             modify_comment=comment,
-            **kw
+            **kw,
         )
 
         return operations.invoke(alt)
@@ -1711,7 +1725,7 @@ class AlterColumnOp(AlterTableOp):
             modify_server_default=server_default,
             modify_nullable=nullable,
             modify_comment=comment,
-            **kw
+            **kw,
         )
 
         return operations.invoke(alt)
@@ -1823,7 +1837,7 @@ class AddColumnOp(AlterTableOp):
             operations.impl.table_name,
             column,
             schema=operations.impl.schema,
-            **kw
+            **kw,
         )
         return operations.invoke(op)
 
@@ -1933,7 +1947,7 @@ class DropColumnOp(AlterTableOp):
             operations.impl.table_name,
             column_name,
             schema=operations.impl.schema,
-            **kw
+            **kw,
         )
         return operations.invoke(op)
 
diff --git a/docs/build/unreleased/879.rst b/docs/build/unreleased/879.rst
new file mode 100644 (file)
index 0000000..07e50ed
--- /dev/null
@@ -0,0 +1,9 @@
+.. change::
+    :tags: bug, operations
+    :tickets: 879
+
+    Fixed regression due to :ticket:`803` where the ``.info`` and ``.comment``
+    attributes of ``Table`` would be lost inside of the :class:`.DropTableOp`
+    class, which when "reversed" into a :class:`.CreateTableOp` would then have
+    lost these elements. Pull request courtesy Nicolas CANIART.
+
index 3841f25aa19e31927e3b8be921e1ad4d7149d1ca..57da362d0d9909cf344b9a39e7f681869903c7be 100644 (file)
@@ -1152,9 +1152,19 @@ class ObjectFromToTest(TestBase):
 
     def test_drop_table(self):
         schema_obj = schemaobj.SchemaObjects()
-        table = schema_obj.table("x", Column("q", Integer))
+        table = schema_obj.table(
+            "x",
+            Column("q", Integer),
+            info={"custom": "value"},
+            prefixes=["FOREIGN"],
+            postgresql_partition_by="x",
+            comment="some comment",
+        )
         op = ops.DropTableOp.from_table(table)
         is_not_(op.to_table(), table)
+        eq_(op.to_table().comment, table.comment)
+        eq_(op.to_table().info, table.info)
+        eq_(op.to_table()._prefixes, table._prefixes)
 
     def test_drop_table_add_kw(self):
         schema_obj = schemaobj.SchemaObjects()
@@ -1171,9 +1181,19 @@ class ObjectFromToTest(TestBase):
 
     def test_create_table(self):
         schema_obj = schemaobj.SchemaObjects()
-        table = schema_obj.table("x", Column("q", Integer))
+        table = schema_obj.table(
+            "x",
+            Column("q", Integer),
+            postgresql_partition_by="x",
+            prefixes=["FOREIGN"],
+            info={"custom": "value"},
+            comment="some comment",
+        )
         op = ops.CreateTableOp.from_table(table)
         is_not_(op.to_table(), table)
+        eq_(op.to_table().comment, table.comment)
+        eq_(op.to_table().info, table.info)
+        eq_(op.to_table()._prefixes, table._prefixes)
 
     def test_create_table_add_kw(self):
         schema_obj = schemaobj.SchemaObjects()