On 03/23/2016 09:32 AM, Nicolas Cadou wrote:
Hello,

I've been using the "Don’t Generate Empty Migrations with Autogenerate"
cookbook snippet:

http://alembic.readthedocs.org/en/latest/cookbook.html#don-t-generate-empty-migrations-with-autogenerate

I'd now like to also use a rewriter:

writer1 = rewriter.Rewriter()

@writer1.rewrites(ops.CreateTableOp)
def create_table(context, revision, op):
     ...

I couldn't find a way to combine the two, though. I thought something
like this could work, but it didn't:

writer2 = rewriter.Rewriter()
writer = writer1.chain(writer2)

@writer2.rewrites(ops.MigrationScript)
def process_revision_directives(context, revision, directives):
     ...

Didn't work with ops.MigrateOperation either. Both receive a
MigrationScript object as third argument instead of the list that can be
cleared that the cookbook function receives. Plus,
process_revision_directives gets called 5 times with ops.MigrateOperations.

What am I missing? What's the proper way to both use a rewriter and
prevent empty migrations from being generated?

Well the easy way, Rewriter is a callable object just like your plain process_revision_directives function. So in your process_revision_directives, you can do the thing with empty migrations, then call into your rewriter - my_rewriter(context, revision, directives), and then not worry about it.

If you wanted to use Rewriter objects for both, since the "don't generate empty migration" means that the MigrationScript directive itself must be suppressed, you'd need to subclass Rewriter, it looks like the process_revision_directives() method at the moment is where that would happen. I'd accept a PR with complete tests to allow "modification of the whole list" to be part of Rewriter's decorated cases.




Thanks,

Nicolas

--
You received this message because you are subscribed to the Google
Groups "sqlalchemy-alembic" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to [email protected]
<mailto:[email protected]>.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy-alembic" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to