mysqlalchemy audit extension

lightsong發表於2024-04-28

mysqlalchemy audit extension

https://sqlalchemy-declarative-extensions.readthedocs.io/en/stable/audit_tables.html

from sqlalchemy import Column, types
from sqlalchemy.orm import declarative_base

from sqlalchemy_declarative_extensions import declarative_database
from sqlalchemy_declarative_extensions.audit import audit

Base = declarative_database(declarative_base())


@audit()
class Foo(Base):
    __tablename__ = "foo"

    id = Column(types.Integer(), primary_key=True)
    name = Column(types.Unicode())
    json = Column(JSONB())


audit_table = Foo.__audit_table__

change

foo1 = Foo(id=1, name=None, json=None)
foo2 = Foo(id=2, name='wat?', json={})
pg.add(foo1)
pg.add(foo2)
pg.commit()

foo1.name = 'wow!'
pg.delete(foo2)
pg.commit()

audit table

相關文章