Skip to content

consider rationale for ModifyTableOps emitting "pass" for no operations #550

@RazerM

Description

@RazerM

I was using alembic's rewrite feature to ignore comment changes (temporarily, not important for the issue):

@writer.rewrites(ops.AlterColumnOp)
def rewrite_alter_column(context, revision, op):
    op.modify_comment = False
    if not op.has_changes():
        return []
    return op

the resulting migration file has many pass lines:

def upgrade():
    pass
    pass
    pass
    ...

It would be nice if alembic wouldn't add unnecessary pass lines.

I found the cause:

return ["pass"]


I found a workaround by rewriting ModifyTableOps after the first rewriter has finished:

writer1 = Rewriter()
writer2 = Rewriter()

@writer1.rewrites(ops.AlterColumnOp)
def rewrite_alter_column(context, revision, op):
    op.modify_comment = False
    if not op.has_changes():
        return []
    return op

@writer2.rewrites(ops.ModifyTableOps)
def rewrite_modify_table_ops(context, revision, op):
    if op.is_empty():
        return []
    return op

writer = writer1.chain(writer2)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions