Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ExecuteSQLOp.to_diff_tuple, fixes #1335 #1336

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 6 additions & 0 deletions docs/build/unreleased/1335.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. change::
:tags: usecase
:tickets: 1335

The check command now works when there are custom SQL statements added to the
diff stream via :class:`.ExecuteSQLOp`.
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
Loading