olirice / alembic_utils

An alembic/sqlalchemy extension for migrating sql views, functions, triggers, and policies
https://olirice.github.io/alembic_utils
MIT License
211 stars 43 forks source link

alembic check broken - diff cannot be rendered #114

Closed eliasmistler closed 1 year ago

eliasmistler commented 1 year ago

alembic check crashes in the presence of a diff (e.g. a view created):

...
  File ".../alembic/command.py", line 297, in check
    diffs = migration_script.upgrade_ops.as_diffs()
  File ".../alembic/operations/ops.py", line 2547, in as_diffs
    return list(OpContainer._ops_as_diffs(self))
  File ".../alembic/operations/ops.py", line 2557, in _ops_as_diffs
    yield op.to_diff_tuple()
  File "...alembic/operations/ops.py", line 87, in to_diff_tuple
    raise NotImplementedError
NotImplementedError

The problem being that ReversibleOp doesn't currently implement MigrateOperation's abstract method to_diff_tuple, which is used to render a diff to the console.

Env:

python: 3.10.6
alembic: 1.11.1
alembic_utils:  0.8.1

I've patched over it for now with the following added method:

def reversible_op_to_diff_tuple(self: ReversibleOp):
    """missing method in alembic_utils"""
    op_name = type(self).__name__
    if op_name.endswith("Op"):
        op_name = op_name[:-2]
    op_name = inflection.underscore(op_name)
    return f"{op_name}_{self.target.type_}", self.target.schema, self.target.signature

ReversibleOp.to_diff_tuple = reversible_op_to_diff_tuple

And it works fine:

alembic.util.exc.AutogenerateDiffsDetected: New upgrade operations detected: [('create_view', 'my_schema', 'my_view')]

Feel free to use the code provided here in alembic_utils if applicable.

olirice commented 1 year ago

thanks, I'd be interested in a PR for this if anyone would like to help out