SQLAlchemy extension that provides an easy way to track changes to your database models.
Project description
SQLAudit
SQLAudit is a SQLAlchemy extension that provides an easy way to track changes to your database models. It automatically creates an audit trail of changes made to your models, including the user who made the change, the timestamp of the change.
It is designed to work with SQLAlchemy's ORM and provides a simple way to track changes to your models without having to write custom code for each model. SQLAudit only requires you to decorate your models with the @track_table decorator, and it will automatically track changes to the specified fields.
@track_table(tracked_fields=["name", "email", "user_id"])
class Customer(Base):
__tablename__ = "customers"
customer_id: Mapped[int] = mapped_column(
Integer, primary_key=True, autoincrement=True
)
name: Mapped[str] = mapped_column(String)
email: Mapped[str] = mapped_column(String)
created_by: Mapped[str] = mapped_column(ForeignKey("users.user_id"))
Notice
This project is in its early stages and is not yet ready for production use.
Quick start
To get started with SQLAudit, you need to install it using pip:
pip install sqlaudit
Follow the steps below to set up and use SQLAudit in your SQLAlchemy application.
This short guide demonstrates how to setup and use SQLAudit.
Step 1: Define your Base and User model
SQLAudit requires access to a SQAlchemy Base class. We will also define a User class which will be used to identify who made which change, and a session factory to create sessions.
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, DeclarativeBase
# Create an in-memory SQLite database
DATABASE_URL = "sqlite:///:memory:"
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def get_db():
"""Yield a database session for an in-memory SQLite DB."""
db = SessionLocal()
try:
yield db
finally:
db.close()
class Base(DeclarativeBase):
pass
class User(Base):
__tablename__ = "users"
user_id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
name: Mapped[string] = mapped_column()
Step 2: Configure SQLAudit
Set the global configuration using set_audit_config().
from sqlaudit import set_audit_config
from utils.db import engine, get_db # your db utils
set_audit_config(
engine=engine, # SQLAlchemy engine
Base=Base, # SQLAlchemy Base class defined in Step 1
session_factory=get_db, # function to get a SQLAlchemy session
default_user_id_field="user_id", # field in User model to identify user
user_model=User, # User model defined in Step 1
user_id_field="user_id", # field in User model to identify user
)
Step 3: Add SQLAudit to your models
To enable auditing for your models use the @track_table decorator. This decorator will automatically track changes to the model and store them in the audit table. There are various options you can pass to the decorator to customize the behavior. The most basic usage only requires a list of strings representing the columns you want to track.
from sqlaudit import track_table
from sqlalchemy import Integer, String
@track_table(tracked_fields=["name", "email", "user_id"])
class Customer(Base):
__tablename__ = "customers"
customer_id: Mapped[int] = mapped_column(
Integer, primary_key=True, autoincrement=True
)
name: Mapped[str] = mapped_column(String)
email: Mapped[str] = mapped_column(String)
user_id: Mapped[str] = mapped_column(String, nullable=False)
Step 4: Register the SQLAudit hooks
SQLAudit uses two SQLAlchemy events to track changes: before_flush and after_flush. You need to register these hooks to enable auditing.
We will be doing this in a startup function that will be called when the application starts.
if __name__ == "__main__":
Base.metadata.create_all(engine)
register_audit_hooks()
Step 5: Use the session to make changes
Now you can use the session to make changes to your models. SQLAudit will automatically track these changes and store them in the audit table.
with next(get_db()) as session:
user = User()
session.add(user)
session.commit()
new_customer = Customer(
name="John Doe", email="jdoe@example.com", user_id=user.user_id
)
session.add(new_customer)
session.commit()
print(
f"Customer {new_customer.customer_id} added with name {new_customer.name} and email {new_customer.email}."
)
# We check if the customer is in the database
customer = (
session.query(Customer)
.filter_by(customer_id=new_customer.customer_id)
.first()
)
new_customer2 = Customer(
name="Jane Doe", email="jane@example.com", user_id=user.user_id
)
session.add(new_customer2)
session.commit()
print(
f"Customer {new_customer2.customer_id} added with name {new_customer2.name} and email {new_customer2.email}."
)
session.refresh(new_customer2)
new_customer2.name = "Jane Smith"
session.commit()
changes = get_resource_changes(
model_class=Customer,
session=session,
filter_resource_ids=["1,", "2"],
filter_user_ids=str(user.user_id),
)
for change in changes:
print(change)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sqlaudit-0.1.3.tar.gz.
File metadata
- Download URL: sqlaudit-0.1.3.tar.gz
- Upload date:
- Size: 12.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4b10f360983dcb1192b9544e7dd538768cdb69a5a5144932ec7d50f949be24ee
|
|
| MD5 |
ef99a4d59fce91e452a9917a0f0c75c7
|
|
| BLAKE2b-256 |
428df278b71fc4c08475b431e221766bb0ae2b977e7651b10c7cdaf6b7728f27
|
Provenance
The following attestation bundles were made for sqlaudit-0.1.3.tar.gz:
Publisher:
publish.yml on SanderJBouwman/sqlaudit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sqlaudit-0.1.3.tar.gz -
Subject digest:
4b10f360983dcb1192b9544e7dd538768cdb69a5a5144932ec7d50f949be24ee - Sigstore transparency entry: 220045473
- Sigstore integration time:
-
Permalink:
SanderJBouwman/sqlaudit@161d359802ab34f5cbb31f37f2de285815c0c41d -
Branch / Tag:
refs/tags/0.1.3 - Owner: https://github.com/SanderJBouwman
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@161d359802ab34f5cbb31f37f2de285815c0c41d -
Trigger Event:
release
-
Statement type:
File details
Details for the file sqlaudit-0.1.3-py3-none-any.whl.
File metadata
- Download URL: sqlaudit-0.1.3-py3-none-any.whl
- Upload date:
- Size: 15.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f8e97bf96de68b2d8c1947dfa9d69ec77845bb9935feb469015c8f46d2221b98
|
|
| MD5 |
7a6bf93abd48a3d7b29fd68e5a66b81d
|
|
| BLAKE2b-256 |
aeb7447210af7e5a4ace7067412e904f77dc77c40360330e2d220aee69652166
|
Provenance
The following attestation bundles were made for sqlaudit-0.1.3-py3-none-any.whl:
Publisher:
publish.yml on SanderJBouwman/sqlaudit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sqlaudit-0.1.3-py3-none-any.whl -
Subject digest:
f8e97bf96de68b2d8c1947dfa9d69ec77845bb9935feb469015c8f46d2221b98 - Sigstore transparency entry: 220045477
- Sigstore integration time:
-
Permalink:
SanderJBouwman/sqlaudit@161d359802ab34f5cbb31f37f2de285815c0c41d -
Branch / Tag:
refs/tags/0.1.3 - Owner: https://github.com/SanderJBouwman
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@161d359802ab34f5cbb31f37f2de285815c0c41d -
Trigger Event:
release
-
Statement type: