remove column tx_contextid and use txId as primary key

This commit is contained in:
Michael Hoennig 2024-08-27 08:48:41 +02:00
parent 36c21505a3
commit f973868bf9

View File

@ -23,8 +23,10 @@ do $$
*/ */
create table tx_context create table tx_context
( (
contextId bigint primary key not null, -- FIXME: what whas the purpose of such a hash(task+txid)?
txId bigint not null, -- contextId bigint primary key not null,
-- txId bigint not null,
txId bigint primary key not null,
txTimestamp timestamp not null, txTimestamp timestamp not null,
currentUser varchar(63) not null, -- not the uuid, because users can be deleted currentUser varchar(63) not null, -- not the uuid, because users can be deleted
assumedRoles varchar(1023) not null, -- not the uuids, because roles can be deleted assumedRoles varchar(1023) not null, -- not the uuids, because roles can be deleted
@ -43,7 +45,8 @@ create index on tx_context using brin (txTimestamp);
*/ */
create table tx_journal create table tx_journal
( (
contextId bigint not null references tx_context (contextId), -- contextId bigint not null references tx_context (contextId), -- FIXME: this ...
txId bigint not null references tx_context (txId), -- FIXME: ... or that?
targetTable text not null, targetTable text not null,
targetUuid uuid not null, -- Assumes that all audited tables have a uuid column. targetUuid uuid not null, -- Assumes that all audited tables have a uuid column.
targetOp operation not null, targetOp operation not null,
@ -62,7 +65,7 @@ create index on tx_journal (targetTable, targetUuid);
create view tx_journal_v as create view tx_journal_v as
select txc.*, txj.targettable, txj.targetop, txj.targetuuid, txj.targetdelta select txc.*, txj.targettable, txj.targetop, txj.targetuuid, txj.targetdelta
from tx_journal txj from tx_journal txj
left join tx_context txc using (contextid) left join tx_context txc using (txId) -- FIXME: or contextId?
order by txc.txtimestamp; order by txc.txtimestamp;
--// --//
@ -77,31 +80,37 @@ create or replace function tx_journal_trigger()
language plpgsql as $$ language plpgsql as $$
declare declare
curTask text; curTask text;
curContextId bigint; -- curContextId bigint; FIXME: needed?
curTxId bigint;
begin begin
curTask := currentTask(); curTask := currentTask();
curContextId := txid_current()+bigIntHash(curTask); -- curContextId := txid_current()+bigIntHash(curTask); FIXME: needed?
curTxId := txid_current();
insert insert
into tx_context (contextId, txId, txTimestamp, currentUser, assumedRoles, currentTask, currentRequest) -- FIXME
values (curContextId, txid_current(), now(), -- into tx_context (contextId, txId, txTimestamp, currentUser, assumedRoles, currentTask, currentRequest)
-- values (curContextId, txid_current(), now(),
-- currentUser(), assumedRoles(), curTask, currentRequest())
into tx_context (txId, txTimestamp, currentUser, assumedRoles, currentTask, currentRequest)
values ( curTxId, now(),
currentUser(), assumedRoles(), curTask, currentRequest()) currentUser(), assumedRoles(), curTask, currentRequest())
on conflict do nothing; on conflict do nothing;
case tg_op case tg_op
when 'INSERT' then insert when 'INSERT' then insert
into tx_journal into tx_journal
values (curContextId, values (curTxId, -- FIXME: curContextId?
tg_table_name, new.uuid, tg_op::operation, tg_table_name, new.uuid, tg_op::operation,
to_jsonb(new)); to_jsonb(new));
when 'UPDATE' then insert when 'UPDATE' then insert
into tx_journal into tx_journal
values (curContextId, values (curTxId, -- FIXME: curContextId?
tg_table_name, old.uuid, tg_op::operation, tg_table_name, old.uuid, tg_op::operation,
jsonb_changes_delta(to_jsonb(old), to_jsonb(new))); jsonb_changes_delta(to_jsonb(old), to_jsonb(new)));
when 'DELETE' then insert when 'DELETE' then insert
into tx_journal into tx_journal
values (curContextId, values (curTxId, -- FIXME: curContextId?
tg_table_name, old.uuid, 'DELETE'::operation, tg_table_name, old.uuid, 'DELETE'::operation,
null::jsonb); null::jsonb);
else raise exception 'Trigger op % not supported for %.', tg_op, tg_table_name; else raise exception 'Trigger op % not supported for %.', tg_op, tg_table_name;