Added alembic migration

This commit is contained in:
Adrià Casajús 2022-03-10 16:37:21 +01:00
parent 1d15af53b7
commit bc82bab1eb
No known key found for this signature in database
GPG key ID: F0033226A5AFC9B9
2 changed files with 25 additions and 25 deletions

View file

@ -36,10 +36,6 @@ class SLModelView(sqla.ModelView):
return redirect(url_for("auth.login", next=request.url)) return redirect(url_for("auth.login", next=request.url))
def on_model_change(self, form, model, is_created): def on_model_change(self, form, model, is_created):
if is_created:
action = AdminAuditLog.ACTION_CREATE_OBJECT
else:
action = AdminAuditLog.ACTION_UPDATE_OBJECT
changes = {} changes = {}
for attr in sqlalchemy.inspect(model).attrs: for attr in sqlalchemy.inspect(model).attrs:
if attr.history.has_changes() and attr.key not in ( if attr.history.has_changes() and attr.key not in (
@ -47,14 +43,23 @@ class SLModelView(sqla.ModelView):
"updated_at", "updated_at",
): ):
value = attr.value value = attr.value
# If it's a model reference, get the source id
if issubclass(type(value), models.Base): if issubclass(type(value), models.Base):
value = value.id value = value.id
# otherwise, if its a generic object stringify it
if issubclass(type(value), object):
value = str(value)
changes[attr.key] = value changes[attr.key] = value
auditAction = (
AdminAuditLog.ACTION_CREATE_OBJECT
if is_created
else AdminAuditLog.ACTION_UPDATE_OBJECT
)
AdminAuditLog.create( AdminAuditLog.create(
admin_user_id=current_user.id, admin_user_id=current_user.id,
model=model.__class__.__name__, model=model.__class__.__name__,
model_id=model.id, model_id=model.id,
action=action, action=auditAction,
data=changes, data=changes,
) )

View file

@ -11,30 +11,25 @@ import sqlalchemy as sa
from sqlalchemy.dialects import postgresql from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision = 'b500363567e3' revision = "b500363567e3"
down_revision = '9282e982bc05' down_revision = "9282e982bc05"
branch_labels = None branch_labels = None
depends_on = None depends_on = None
def upgrade(): def upgrade():
op.create op.create_table(
# ### commands auto generated by Alembic - please adjust! ### "admin_audit_log",
op.alter_column('admin_audit_log', 'data', sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
existing_type=postgresql.JSONB(astext_type=sa.Text()), sa.Column("created_at", sqlalchemy_utils.types.arrow.ArrowType(), nullable=False),
nullable=False) sa.Column("admin_user_id", sa.Integer, nullable=False),
op.alter_column('admin_audit_log', 'model_id', sa.Column("action", sa.Integer, nullable=False),
existing_type=sa.INTEGER(), sa.Column("model", sa.String(length=256), nullable=False),
nullable=False) sa.Column("model_id", sa.Integer, nullable=False),
# ### end Alembic commands ### sa.Column("data", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
sa.ForeignKeyConstraint(['admin_user_id'], ['users.id'], ondelete='cascade'),
sa.PrimaryKeyConstraint("id"),
)
def downgrade(): def downgrade():
# ### commands auto generated by Alembic - please adjust! ### op.drop_table("admin_audit_log")
op.alter_column('admin_audit_log', 'model_id',
existing_type=sa.INTEGER(),
nullable=True)
op.alter_column('admin_audit_log', 'data',
existing_type=postgresql.JSONB(astext_type=sa.Text()),
nullable=True)
# ### end Alembic commands ###