Skip to main content

Versioning and auditing extension for PostgreSQL and SQLAlchemy.

Project description

Build Status Version Status Downloads

Auditing extension for PostgreSQL. Provides additional extensions for SQLAlchemy and Flask. PostgreSQL-Audit tries to combine the best of breed from existing solutions such as SQLAlchemy-Continuum, Papertrail and especially Audit Trigger by 2nd Quadrant.

Compared to existing solutions PostgreSQL-Audit has the following charasteristics:

  • Stores all versions into single table called ‘activity’

  • Uses minimalistic trigger based approach to keep INSERTs, UPDATEs and DELETEs as fast as possible

  • Tracks actor IDs to be able to answer these questions quickly:
    • Who modified record x on day x?

    • What did person x do between y and z?

    • Can you show me the activity history of record x?

Documentation

Installation

pip install PostgreSQL-Audit

Running the tests

git clone https://github.com/kvesteri/postgresql-audit.git
cd postgresql-audit
pip install tox
createdb postgresql_audit_test
tox

Flask extension

from postgresql_audit.flask import versioning_manager

from my_app.extensions import db


versioning_manager.init(db.Model)


class Article(db.Model):
    __tablename__ = 'article'
    __versioned__ = {}  # <- IMPORTANT!
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)


article = Article(name='Some article')
db.session.add(article)
db.session.commit()

Now we can check the newly created activity.

Activity = versioning_manager.activity_cls

activity = Activity.query.first()
activity.id             # 1
activity.table_name     # 'article'
activity.verb           # 'insert'
activity.old_data       # None
activity.changed_data   # {'id': '1', 'name': 'Some article'}
article.name = 'Some other article'
db.session.commit()

activity = Activity.query.order_by(db.desc(Activity.id)).first()
activity.id             # 2
activity.table_name     # 'article'
activity.verb           # 'update'
activity.object_id      # 1
activity.old_data       # {'id': '1', 'name': 'Some article'}
activity.changed_data   # {'name': 'Some other article'}
db.session.delete(article)
db.session.commit()

activity = Activity.query.order_by(db.desc(Activity.id)).first()
activity.id             # 3
activity.table_name     # 'article'
activity.verb           # 'delete'
activity.object_id      # 1
activity.old_data       # {'id': '1', 'name': 'Some other article'}
activity.changed_data   # None

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

postgresql_audit-0.18.0.tar.gz (28.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

postgresql_audit-0.18.0-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file postgresql_audit-0.18.0.tar.gz.

File metadata

  • Download URL: postgresql_audit-0.18.0.tar.gz
  • Upload date:
  • Size: 28.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for postgresql_audit-0.18.0.tar.gz
Algorithm Hash digest
SHA256 f3725092df5b7003d3da68b21e57d37f8e15e001d9242108e8c5b8e4fe27ad93
MD5 70048a52a6686d885fec82e7451b7e02
BLAKE2b-256 c6fe9b5c05dc058e7e6d91768343973e788e4784dd1b12b8cca2c302ad8c463d

See more details on using hashes here.

File details

Details for the file postgresql_audit-0.18.0-py3-none-any.whl.

File metadata

File hashes

Hashes for postgresql_audit-0.18.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7ce76d8af1402e336112c6539333c538e25972346d04f624a60dfd5a8163b95b
MD5 0d17f8b33c0cc2e10143d10ed5afeea9
BLAKE2b-256 9776805845b993f692d6f32a8ca74b13dedfd320aa07f48e973302d0797e87d8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page