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?

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.17.0.tar.gz (28.4 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: postgresql_audit-0.17.0.tar.gz
  • Upload date:
  • Size: 28.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for postgresql_audit-0.17.0.tar.gz
Algorithm Hash digest
SHA256 f2e999df30ec04afecfb13ab25e4895f9a6bd6cf690da9a5ab4a4fdbdee1c638
MD5 7da495afc6e53d5e1f5984abbcc157a8
BLAKE2b-256 fbc86e34269443e0333b0b7ce2f220b04452f3bef586270f0b3165b3d47ae064

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for postgresql_audit-0.17.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3933ccc2a6395b5b6fb2485404ef56166590dc79be0631798023449619626075
MD5 2709caeefc0cc9b60a64e0c0238ce58c
BLAKE2b-256 1e0f9a05b0edf858c3476ee7debdecf95ff3b9506a702beed1d07a14724e1f6c

See more details on using hashes here.

Provenance

Supported by

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