operations, sqltext, execution_options=execution_options
)
+ def to_diff_tuple(self) -> Tuple[str, Union[Executable, str]]:
+ return ("execute", self.sqltext)
+
class OpContainer(MigrateOperation):
"""Represent a sequence of operations operation."""
--- /dev/null
+.. change::
+ :tags: bug, operations
+ :tickets: 1335
+
+ Repaired :class:`.ExecuteSQLOp` so that it can participate in "diff"
+ operations; while this object is typically not present in a reflected
+ operation stream, custom hooks may be adding this construct where it needs
+ to have the correct ``to_diff_tuple()`` method. Pull request courtesy
+ Sebastian Bayer.
],
)
+ def test_add_execute_sql_op(self):
+ uo = ops.UpgradeOps(ops=[])
+ autogenerate._produce_net_changes(self.autogen_context, uo)
+
+ uo.ops.append(ops.ExecuteSQLOp("STATEMENT"))
+
+ diffs = uo.as_diffs()
+
+ eq_(diffs[-1][0], "execute")
+ eq_(diffs[-1][1], "STATEMENT")
+
class AutogenerateDiffTestWSchema(ModelOne, AutogenTest, TestBase):
__only_on__ = "postgresql"
" # ### end Alembic commands ###",
)
+ def test_add_execute_sql(self):
+ writer = autogenerate.Rewriter()
+
+ @writer.rewrites(ops.CreateTableOp)
+ def rewriter_execute_sql(context, revision, op):
+ execute_op = ops.ExecuteSQLOp(sqltext="STATEMENT")
+ return [op, execute_op]
+
+ directives = [
+ ops.MigrationScript(
+ util.rev_id(),
+ ops.UpgradeOps(
+ ops=[
+ ops.CreateTableOp(
+ "test_table",
+ [sa.Column("id", sa.Integer(), primary_key=True)],
+ )
+ ]
+ ),
+ ops.DowngradeOps(ops=[]),
+ )
+ ]
+
+ ctx, rev = mock.Mock(), mock.Mock()
+ writer(ctx, rev, directives)
+
+ eq_(
+ autogenerate.render_python_code(directives[0].upgrade_ops_list[0]),
+ "# ### commands auto generated by Alembic - please adjust! ###\n"
+ " op.create_table('test_table',\n"
+ " sa.Column('id', sa.Integer(), nullable=False),\n"
+ " sa.PrimaryKeyConstraint('id')\n"
+ " )\n"
+ " op.execute('STATEMENT')\n"
+ " # ### end Alembic commands ###",
+ )
+
+ diffs = directives[0].upgrade_ops_list[0].as_diffs()
+
+ eq_(diffs[0][0], "add_table")
+
+ eq_(diffs[1][0], "execute")
+ eq_(diffs[1][1], "STATEMENT")
+
class MultiDirRevisionCommandTest(TestBase):
def setUp(self):