Prefered sqlite3 settings

.headers on
.mode columns

Helpers

Sqlite funtion to convert string time stamp to unix epoch:

strftime('%s', 'time')

Ideas

When naming indexes can name them after the query they support

Examples

Query data and place in file

sqlite3 data.db <<EOF
.headers on
.mode csv
.output output.csv
SELECT profile_picture FROM users where profile_picture is not null;
.output stdout
EOF

Historical Data Tracking via Change Sets

Capabilities:

Note: would need to update these tables with transaction queries, because database triggers lack information like who changed the record.

CREATE TABLE change_sets (
    id INTEGER PRIMARY KEY,
    table_name TEXT NOT NULL,
    record_id INTEGER NOT NULL,
    status TEXT NOT NULL DEFAULT 'pending'
        CHECK (status IN ('pending', 'approved', 'rejected')),
    created_by TEXT NOT NULL,
    created_at TEXT DEFAULT CURRENT_TIMESTAMP,
    applied_at TEXT
);

CREATE TABLE field_changes (
    id INTEGER PRIMARY KEY,
    change_set_id INTEGER NOT NULL
        REFERENCES change_sets(id),
    column_name TEXT NOT NULL,
    old_value TEXT,
    new_value TEXT,
    UNIQUE (change_set_id, column_name)
);

Separate pending changes from historical changes for performance (simpler indexes) and easier managment (e.g., can ommit historical changes from replication slot feeds for example)

inventory
    |
    +-- pending_changes
    |
    +-- change_history

Archiving can be done against the change_history Put into s3 cold storage as zip files for example. Would need manifest files, but not super complicated to manage.

CREATE TABLE history_archives (
    id INTEGER PRIMARY KEY,
    object_key TEXT NOT NULL UNIQUE,
    first_change_id INTEGER NOT NULL,
    last_change_id INTEGER NOT NULL,
    record_count INTEGER NOT NULL,
    checksum TEXT NOT NULL,
    created_at TEXT DEFAULT CURRENT_TIMESTAMP
);