Skip to content

Commit

Permalink
Merge "Implement ExecuteSQLOp.to_diff_tuple" into main
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzeek authored and Gerrit Code Review committed Oct 26, 2023
2 parents 20a04c1 + 1033b99 commit 5dcbca6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
3 changes: 3 additions & 0 deletions alembic/operations/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -2539,6 +2539,9 @@ def batch_execute(
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."""
Expand Down
9 changes: 9 additions & 0 deletions docs/build/unreleased/1335.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.. 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.
11 changes: 11 additions & 0 deletions tests/test_autogen_diffs.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,17 @@ def test_dont_barf_on_already_reflected(self):
],
)

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"
Expand Down
44 changes: 44 additions & 0 deletions tests/test_script_production.py
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,50 @@ def rewrite_alter_column(context, revision, op):
" # ### 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):
Expand Down

0 comments on commit 5dcbca6

Please sign in to comment.