olirice / alembic_utils

An alembic/sqlalchemy extension for migrating sql views, functions, triggers, and policies
MIT License
193 stars 42 forks source link

[Question] Using Alembic Utils without SQL Alchemy models #100

Closed JPalm1 closed 1 year ago

JPalm1 commented 1 year ago

I'm looking to use Alembic Utils on my existing project, as we have written quite a few Postgres triggers we would like to be able to track nicely, and your package seems like the perfect fit.

We use alembic, but we chose against using SQLAlchemy, so currently we manually write the migration scripts ourselves.

In an ideal world, I'd love to be able to use alembic autogeneration only for objects provided by Alembic Utils. I've followed the steps on your quickstart page: setting up a trigger object and registering those entities in env.py.

When I run autogenerate, I get error:

env.py does not provide a MetaData object or sequence of objects to the context.

I figure this is because I have not set target_metadata, but (as far as I'm aware) I do not need to set this as I do not have SQLAlchemy models I want Alembic to migrate.

So my question is: is it possible to autogenerate migrations only for alembic utils objects?

I have also tried writing an include_object method to only return true for PGFunction and PGTrigger objects, to no avail.

Any help would be more than welcome! Thanks in advance.

JPalm1 commented 1 year ago

I worked out the issue now, was premature with this question. Thanks for the great library!

olirice commented 1 year ago

ok great

In the alembic_utils test suite we do it by providing a dummy metadata object that will aways produce noop, but that does still require sqlalchemy to be installed

https://github.com/olirice/alembic_utils/blob/4e6008fe1b355a9d2b5967d01a5272f6b1aa7bb6/src/test/alembic_config/env.py#L20

what did you come up with?

JPalm1 commented 1 year ago

Thanks for the info, I found something similar, by just setting

target_metadata = declarative_base().metadata

and it worked nicely!