"# ### commands auto generated by Alembic - please adjust! ###"
)
- if not op_container.ops:
- printer.writeline("pass")
- else:
- for op in op_container.ops:
- lines = render_op(autogen_context, op)
+ has_lines = False
+ for op in op_container.ops:
+ lines = render_op(autogen_context, op)
+ has_lines = has_lines or lines
- for line in lines:
- printer.writeline(line)
+ for line in lines:
+ printer.writeline(line)
+
+ if not has_lines:
+ printer.writeline("pass")
printer.writeline("# ### end Alembic commands ###")
return lines
else:
- return ["pass"]
+ return []
@renderers.dispatch_for(ops.CreateTableCommentOp)
--- /dev/null
+.. change::
+ :tags: bug, autogenerate
+ :tickets: 550
+
+ Improved the Python rendering of a series of migration operations such that
+ a single "pass" is rendered for a :class:`.UpgradeOps` or
+ :class:`.DowngradeOps` based on if no lines of Python code actually
+ rendered under the operation, rather than whether or not sub-directives
+ exist. Removed extra "pass" lines that would generate from the
+ :class:`.ModifyTableOps` directive so that these aren't duplicated under
+ operation rewriting scenarios.
+
" # ### end Alembic commands ###",
)
+ def test_no_needless_pass(self):
+ writer1 = autogenerate.Rewriter()
+
+ @writer1.rewrites(ops.AlterColumnOp)
+ def rewrite_alter_column(context, revision, op):
+ return []
+
+ directives = [
+ ops.MigrationScript(
+ util.rev_id(),
+ ops.UpgradeOps(
+ ops=[
+ ops.ModifyTableOps(
+ "t1",
+ ops=[
+ ops.AlterColumnOp(
+ "foo",
+ "bar",
+ modify_nullable=False,
+ existing_type=sa.Integer(),
+ ),
+ ops.AlterColumnOp(
+ "foo",
+ "bar",
+ modify_nullable=False,
+ existing_type=sa.Integer(),
+ ),
+ ],
+ ),
+ ops.ModifyTableOps(
+ "t1",
+ ops=[
+ ops.AlterColumnOp(
+ "foo",
+ "bar",
+ modify_nullable=False,
+ existing_type=sa.Integer(),
+ )
+ ],
+ ),
+ ]
+ ),
+ ops.DowngradeOps(ops=[]),
+ )
+ ]
+ ctx, rev = mock.Mock(), mock.Mock()
+ writer1(ctx, rev, directives)
+
+ eq_(
+ autogenerate.render_python_code(directives[0].upgrade_ops),
+ "# ### commands auto generated by Alembic - please adjust! ###\n"
+ " pass\n"
+ " # ### end Alembic commands ###",
+ )
+
class MultiDirRevisionCommandTest(TestBase):
def setUp(self):