Skip to content

Commit

Permalink
new repo audit: ID is always generated and MUST NOT be provided by app
Browse files Browse the repository at this point in the history
  • Loading branch information
virgo47 committed Dec 6, 2021
1 parent 3a8baaf commit 2e99dc7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion config/sql/native-new/postgres-new-audit.sql
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ CREATE TABLE IF NOT EXISTS m_global_metadata (

-- region AUDIT
CREATE TABLE ma_audit_event (
id BIGSERIAL NOT NULL,
id BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY, -- ID must NOT be provided by the application
timestamp TIMESTAMPTZ NOT NULL,
eventIdentifier TEXT,
eventType AuditEventTypeType,
Expand Down
11 changes: 11 additions & 0 deletions config/sql/native-new/postgres-new-upgrade-audit.sql
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,14 @@ BEGIN
END loop;
END $$;
$aac$, false);

-- enforcing DB sequence IDs, ignoring provided ones
call apply_audit_change(2, $aac$
ALTER TABLE ma_audit_event ALTER COLUMN id DROP DEFAULT;
-- We don't want just to fill it in, we want to enforce it for uniqueness reasons.
ALTER TABLE ma_audit_event ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY;

-- false flag means that first value returned by nextval() will be the set one, not +1
SELECT setval(pg_get_serial_sequence('ma_audit_event', 'id'),
coalesce((SELECT MAX(id) + 1 FROM ma_audit_event), 1), false);
$aac$, false);

0 comments on commit 2e99dc7

Please sign in to comment.