Enterprise audit and migration
Prerequisite: node-data-partitioning-and-retention. Objective: Design audit and migration plan. Each example is independent. Tested with Node.js 22.16.0 on Linux. Save as main.mjs in a fresh disposable directory and run node main.mjs. These are static code blocks; external system behavior is not established by a local model.
How it works
Record actor ID, tenant, action, resource ID, decision and correlation ID, using synthetic identifiers in training. Do not log tokens or entire request bodies. Restrict audit access and define retention and integrity controls. Expand-and-contract migrations first add a compatible field, then backfill and verify, switch readers/writers, and remove old fields only after rollback compatibility is resolved. Batch by tenant and record progress; use idempotent backfill conditions.
Build an allowlisted audit event
import assert from 'node:assert/strict';const request={actor:'u1',tenant:'t1',resource:'b1',token:'synthetic-secret'};const event={actor:request.actor,tenant:request.tenant,resource:request.resource,action:'read',decision:'allow'};assert.equal(Object.hasOwn(event,'token'),false);console.log(JSON.stringify(event));Expected output:
{"actor":"u1","tenant":"t1","resource":"b1","action":"read","decision":"allow"}Only approved fields enter the model event.
Run an idempotent SQLite backfill
import {DatabaseSync} from 'node:sqlite';import assert from 'node:assert/strict';const db=new DatabaseSync(':memory:');try{db.exec("CREATE TABLE books(tenant TEXT,title TEXT); INSERT INTO books VALUES('a','River'),('b','Forest'); ALTER TABLE books ADD COLUMN display TEXT");const update=db.prepare('UPDATE books SET display=title WHERE tenant=? AND display IS NULL');console.log(update.run('a').changes,update.run('a').changes);assert.equal(db.prepare("SELECT display FROM books WHERE tenant='b'").get().display,null);}finally{db.close();}Expected output:
1 0The second run has nothing to change, and the other tenant stays untouched. The fixture disables the experimental warning for Node 22 SQLite; the API is experimental on this baseline.
Common mistake
A successful schema change does not prove old application versions still work. An audit object alone is not tamper-proof storage.
Practice
Add a new display field without removing the old title and run the backfill twice.
Reference candidate: node-enterprise-audit-and-migration-lookup (route unverified). Documentation anchors: https://www.postgresql.org/docs/18/ddl-rowsecurity.html. External versions and full integration are pending.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Enterprise audit and migration — knowledge check
Apply the lesson to a fresh scenario and explain the evidence boundary.
Which audit field is normally inappropriate?
Why add a column before removing the old one?
What does zero changes on a second backfill indicate?