Use triggers to maintain a chronicle table of updated/deleted timestamps in SQLite
Project description
sqlite-chronicle
Use triggers to track when rows in a SQLite table were updated or deleted
Installation
pip install sqlite-chronicle
enable_chronicle(conn, table_name)
This module provides a single function: sqlite_chronicle.enable_chronicle(conn, table_name)
, which does the following:
- Checks if a
_chronicle_{table_name}
table exists alreday. If so, it does nothing. Otherwise... - Creates that table, with the same primary key columns as the original table plus integer columns
updated_ms
anddeleted
- Creates a new row in the chronicle table corresponding to every row in the original table, setting
updated_ms
to the current timestamp in milliseconds - Sets up three triggers on the table:
- An after insert trigger, which creates a new row in the chronicle table and sets
updated_ms
to the current time - An after update trigger, which updates that timestamp and also updates any primary keys if they have changed (likely extremely rare)
- An after delete trigger, which updates that timestamp and places a
1
in thedeleted
column
The function will raise a sqlite_chronicle.ChronicleError
exception if the table does not have a single or compound primary key.
The end result is a chronicle table that looks something like this:
id | updated_ms | deleted |
---|---|---|
47 | 1694408890954 | 0 |
48 | 1694408874863 | 1 |
1 | 1694408825192 | 0 |
2 | 1694408825192 | 0 |
3 | 1694408825192 | 0 |
Applications
Chronicle tables can be used to efficiently answer the question "what rows have been inserted, updated or deleted since I last checked".
This has numerous potential applications, including:
- Synchronization and replication: other databases can "subscribe" to tables, keeping track of when they last refreshed their copy and requesting just rows that changed since the last time - and deleting rows that have been marked as deleted.
- Indexing: if you need to update an Elasticsearch index or a vector database embeddings index or similar you can run against just the records that changed since your last run - see also The denormalized query engine design pattern
- Enrichments: datasette-enrichments needs to to persist something that says "every address column should be geocoded" - then have an enrichment that runs every X seconds and looks for newly inserted or updated rows and enriches just those.
- Showing people what has changed since their last visit - "52 rows have been updated and 16 deleted since yesterday" kind of thing.
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
Hashes for sqlite_chronicle-0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | efe5a1ba510eadc10c08884fb037de5e32b7c0019f6288a21f5a6c27d07e989f |
|
MD5 | 99098845d6c527b73af1e567040aebad |
|
BLAKE2b-256 | d7f96b8a3c80ef7120c9b70855555fe236b8099ed64c27b3db5e64f0fdbde9b4 |