✨ Add audit logs system for tracking data changes#1671
Draft
douglasduteil wants to merge 2 commits into
Draft
Conversation
**Problem** No way to track historical changes to users, organizations, and memberships. **Proposal** Add audit_logs table with triggers on users, organizations, and users_organizations tables.
**Problem** Drizzle schema is out of sync after adding audit_logs table. **Proposal** Update Drizzle schema and relations to include the new audit_logs table.
0a46691 to
5462da0
Compare
Contributor
Author
Audit Logs Query ExamplesBasic QueriesView all audit logs for a specific userSELECT id, table_name, action, changed_fields, actor_email, created_at
FROM audit_logs
WHERE record_id = :user_id AND table_name = 'users'
ORDER BY created_at DESC;View recent changes by actorSELECT id, table_name, record_id, action, created_at
FROM audit_logs
WHERE actor_email = :actor_email
ORDER BY created_at DESC
LIMIT 100;View all changes from a specific migrationSELECT id, table_name, record_id, action, changed_fields
FROM audit_logs
WHERE migration_name = :migration_name
ORDER BY id;Point-in-Time ReconstructionReconstruct user state at a specific timestamp-- Get the most recent audit entry for a user before the target time
SELECT
CASE
WHEN action = 'DELETE' THEN old_values
ELSE new_values
END as state_at_time
FROM audit_logs
WHERE table_name = 'users'
AND record_id = :user_id
AND created_at <= :target_timestamp
ORDER BY created_at DESC
LIMIT 1;Full history reconstruction for a user-- Returns all states a user has been in, chronologically
SELECT
created_at,
action,
CASE
WHEN action = 'INSERT' THEN new_values
WHEN action = 'UPDATE' THEN new_values
WHEN action = 'DELETE' THEN old_values
END as state,
changed_fields,
actor_email
FROM audit_logs
WHERE table_name = 'users'
AND record_id = :user_id
ORDER BY created_at;Moderation Context QueriesGet user state at the time of moderation-- Given a moderation created_at timestamp, find the user's state at that moment
SELECT
m.id as moderation_id,
m.type as moderation_type,
m.created_at as moderation_time,
al.new_values as user_state_at_moderation
FROM moderations m
CROSS JOIN LATERAL (
SELECT new_values
FROM audit_logs
WHERE table_name = 'users'
AND record_id = m.user_id
AND created_at <= m.created_at
ORDER BY created_at DESC
LIMIT 1
) al
WHERE m.id = :moderation_id;Get organization state at moderation timeSELECT
m.id as moderation_id,
m.type as moderation_type,
m.created_at as moderation_time,
al.new_values as org_state_at_moderation
FROM moderations m
CROSS JOIN LATERAL (
SELECT new_values
FROM audit_logs
WHERE table_name = 'organizations'
AND record_id = m.organization_id
AND created_at <= m.created_at
ORDER BY created_at DESC
LIMIT 1
) al
WHERE m.id = :moderation_id;Get user-organization link state at moderation timeSELECT
m.id as moderation_id,
m.created_at as moderation_time,
al.new_values as link_state_at_moderation
FROM moderations m
CROSS JOIN LATERAL (
SELECT new_values
FROM audit_logs
WHERE table_name = 'users_organizations'
AND record_id = m.user_id
AND (new_values->>'organization_id')::int = m.organization_id
AND created_at <= m.created_at
ORDER BY created_at DESC
LIMIT 1
) al
WHERE m.id = :moderation_id;Change Analysis QueriesFind all changes to a specific fieldSELECT id, record_id, created_at, actor_email,
old_values->>'email' as old_email,
new_values->>'email' as new_email
FROM audit_logs
WHERE table_name = 'users'
AND action = 'UPDATE'
AND 'email' = ANY(changed_fields)
ORDER BY created_at DESC;Count changes by table and actionSELECT table_name, action, COUNT(*) as change_count
FROM audit_logs
GROUP BY table_name, action
ORDER BY table_name, action;Find suspicious activity (many changes in short time)SELECT
actor_email,
COUNT(*) as changes,
MIN(created_at) as first_change,
MAX(created_at) as last_change
FROM audit_logs
WHERE created_at > NOW() - INTERVAL '1 hour'
GROUP BY actor_email
HAVING COUNT(*) > 10
ORDER BY changes DESC;Setting Context VariablesIn application code (before making changes)-- Set actor context for tracking who made the change
SET LOCAL app.actor_email = 'user@example.com';
SET LOCAL app.actor_type = 'user'; -- or 'moderator', 'system', 'migration'
-- Then perform your INSERT/UPDATE/DELETE
UPDATE users SET email = 'new@example.com' WHERE id = 123;In migrations-- Set migration context
SET LOCAL app.current_migration = '1768213263725_apply-audit-triggers';
-- Perform migration operations
UPDATE organizations SET cached_libelle = 'New Name' WHERE id = 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
No way to track historical changes to users, organizations, and memberships.
Proposal
Add audit_logs table with triggers on users, organizations, and users_organizations tables.