-
Notifications
You must be signed in to change notification settings - Fork 28
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
Add support for alembic migrations #47
Comments
PR has been updated to include support for alembic autogenerated migrations. To support this, To enable alembic support in your env.py file you need to add a couple lines. import postgresql_audit.alembic
If you are using flask-migrate, def process_revision_directives(context, revision, directives):
if getattr(config.cmd_opts, "autogenerate", False):
script = directives[0]
if script.upgrade_ops.is_empty():
directives[:] = []
logger.info("No changes in schema detected.")
return
postgresql_audit.alembic.writer.process_revision_directives(context, revision, directives) |
this needs superuser privileges. [1] suggests to use the following instead: |
@wakemaster39 could you please show a complete example, I'm interested to use you PR but in my test the trigger are never added |
@wakemaster39 @kvesteri what's currently blocking the related PR from getting merged? What can I do to help get this over the line? @wakemaster39 is there any way to exclude your refactoring in |
Oh I think I see - that refactoring (43e99f4) is needed to enable the changes that followed? |
So I spent some time on trying to get the tests fixed and was able to get all but 7 working (at least in python 3.7 with postgresql 12). Notable changes were:
the two errors I haven't resolved yet are a couple instances of this error
and this one
|
I also wanted to use postgresql-audit with alembic, here is my approach: https://gist.github.com/adrianschneider94/1557e3c6052c1ecc6401cecb1a79bab4 I utilize alembic-utils' PGFunction for the needed sql-functions and PGTrigger to create the triggers on the column - not the To use this, there have to be two migrations in the beginning:
To create the first migration:
To create the second migration:
|
Scrolling through the issue as I was setting this up, I found the following that appear to be relevant. #43 #7 #20 and #39 .
Basically it comes down to the way that the triggers are added into the DB is via the
after_create
signal in SQLAlchemy. This unfortunately only triggers the first time a table is created and it only triggers when you are using the table metadata.Working with Alembic, Flask-Migrate or other wrappers around alembic this means that the postgres triggers are never properly added and as such nothing ever gets added to the activity table.
#46 is my first pass at attempting to resolve this problem. Its not perfect but it is a start.
Basically at this point all the SQL calls back been removed to external functions from the base
VersionManager
. In addition, new functions have been added to the migrations file.manually adding
init_before_create_transaction
,init_activity_table_triggers
,rollback_create_transaction
andregister_table
allow most things to function.Basically the first migration will look something like this:
All future migrations will need lines like
register_table(op, "ipam_vrfs", ["DateCreated", "LastModified"])
added for all tables that have had either their `excluded_columns modified or a new table for revision tracking is added.Excluded columns are explicitly added and not read from the models to allow full history tracking of what was ignored.
The text was updated successfully, but these errors were encountered: