Skip to content

feat(migrations): partition audit_logs and pii_access_audit_logs by month - #1875

Merged
sublime247 merged 1 commit into
sublime247:mainfrom
Gabbydunkk:partition-audit-logs-1858
Aug 28, 2026
Merged

feat(migrations): partition audit_logs and pii_access_audit_logs by month#1875
sublime247 merged 1 commit into
sublime247:mainfrom
Gabbydunkk:partition-audit-logs-1858

Conversation

@Gabbydunkk

Copy link
Copy Markdown
Contributor

Summary

Partitions the two audit tables so they scale with the transaction_logs approach already in the codebase:

  • audit_logs — RANGE-partitioned on created_at
  • pii_access_audit_logs — RANGE-partitioned on accessed_at

Monthly partitions follow the same zero-downtime pattern established by 20260723_partition_logs: the existing table is renamed to *_legacy and re-attached as the DEFAULT partition (no data copy), monthly partitions are pre-created (past, current, next 3 months), and a BEFORE INSERT route trigger creates the partition for a row's timestamp on demand so an audit write can never fail for want of a partition. The append-only immutability triggers are recreated on the partitioned parents, where PostgreSQL ≥ 14 fires them for every current and future partition.

Verification (Postgres 16)

Run against a real Postgres 16 container:

  • Up: all 85 migrations apply cleanly on a fresh DB; both tables are partitioned with *_legacy + monthly partitions; inserts route into the correct monthly partition; UPDATE/DELETE are blocked (Audit logs are immutable and cannot be modified or deleted).
  • Down: monthly partitions are drained into the legacy table before the parent is dropped — rows written after the up migration survive the rollback — and the original primary keys, indexes, and immutability triggers are restored exactly.
  • Re-up: idempotent; existing data is preserved through the cycle.

Changes

  • migrations/20260826_partition_audit_logs.sql — up migration (both tables, helpers, route + immutability triggers, indexes)
  • migrations/20260826_partition_audit_logs.down.sql — rollback (drain partitions, restore original schema, data-safe)

Closes #1858

…onth

Redesigns the audit schema so audit_logs (RANGE on created_at) and
pii_access_audit_logs (RANGE on accessed_at) are monthly-partitioned,
following the zero-downtime pattern established by 20260723_partition_logs:
rename original -> *_legacy (attached as DEFAULT), pre-create monthly
partitions, and route inserts on demand so a late timestamp never fails an
audit write. Recreates the append-only immutability triggers on the
partitioned parents, where PostgreSQL fires them for every partition.

The down migration drains every monthly partition into the legacy table
before dropping the parent, so rows written after the up migration survive
a rollback, then restores the original PK, indexes and triggers.

Closes sublime247#1858

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
@drips-wave

drips-wave Bot commented Aug 26, 2026

Copy link
Copy Markdown

@Gabbydunkk Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@sublime247
sublime247 merged commit b9a95c3 into sublime247:main Aug 28, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Refactor] Redesign database schema to support partitioned audits

2 participants