Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create audit_table trigger in alembic migration #7

Open
jacobsvante opened this issue Oct 29, 2015 · 3 comments
Open

Create audit_table trigger in alembic migration #7

jacobsvante opened this issue Oct 29, 2015 · 3 comments

Comments

@jacobsvante
Copy link
Contributor

Why isn't the audit_table() trigger added to new tables that are created with alembic revision --autogenerate? Am I correct in suspecting that Alembic doesn't support this?

If so, perhaps we could add a section in the documentation about using alembic's rewriter functionality to achieve this instead. This is how I'm currently solving it in my env.py:

from alembic import context
from alembic.autogenerate import rewriter
from alembic.operations import ops

writer = rewriter.Rewriter()


@writer.rewrites(ops.CreateTableOp)
def create_table(context, revision, op):
    return [
        op,
        ops.ExecuteSQLOp("SELECT audit_table('{}');".format(op.table_name)),
    ]


def run_migrations_online():
    context.configure(
        process_revision_directives=writer,
...

It should be noted that the __versioned__ attribute isn't respected when doing it this way. AFAIK the model isn't available in this context, but perhaps __versioned__ could be passed to the table. Maybe you have some idea on how to achieve this, @kvesteri?

@kvesteri
Copy link
Owner

This sounds really good! I'm fine with passing __versioned__ to the table or passing it to the info attribute of the Table object. I haven't worked with alembic rewriters so my guess is as good as yours.

@jacobsvante
Copy link
Contributor Author

Sweet. Let's just wait for zzzeek/alembic#20 to be merged before actually writing the documentation 😉.

@jacobsvante
Copy link
Contributor Author

Just a heads up that the @zzzeek merged the PR 24 days ago.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants