Skip to content

Commit

Permalink
change snapshot_of for postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Stoerkel committed May 20, 2024
1 parent cd12499 commit 739b3f3
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions history/backends/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
_ctid integer := TG_ARGV[0]::integer;
_pk_name text := TG_ARGV[1];
_record_snap boolean := TG_ARGV[2]::boolean;
_fields text[] := TG_ARGV[3:];
_snap_of text := TG_ARGV[3];
_fields text[] := TG_ARGV[4:];
_old jsonb := to_jsonb(OLD);
_new jsonb := to_jsonb(NEW);
_snapshot jsonb;
Expand All @@ -22,9 +23,15 @@
END IF;
IF _record_snap THEN
SELECT jsonb_object_agg(key, value) INTO _snapshot
FROM jsonb_each(_new)
WHERE key = ANY(_fields);
IF _snap_of == 'OLD' THEN
SELECT jsonb_object_agg(key, value) INTO _snapshot
FROM jsonb_each(_old)
WHERE key = ANY(_fields);
ELSEIF _snap_of == 'NEW' THEN
SELECT jsonb_object_agg(key, value) INTO _snapshot
FROM jsonb_each(_new)
WHERE key = ANY(_fields);
END IF;
END IF;
IF (TG_OP = 'UPDATE') THEN
Expand Down Expand Up @@ -133,17 +140,18 @@ def create_trigger(self, model, trigger_type):
return tr_name, []
self.execute(
"""
CREATE TRIGGER {tr_name} AFTER {trans_type} ON {table}
FOR EACH ROW EXECUTE PROCEDURE
history_record({ctid}, '{pk_col}', {snapshots}{field_list});
CREATE TRIGGER {tr_name} AFTER {trans_type} ON {table}
FOR EACH ROW EXECUTE PROCEDURE
history_record({ctid}, '{pk_col}', {snapshots}, '{snap_of}', {field_list});
""".format(
tr_name=tr_name,
trans_type=trigger_type.name.upper(),
table=model._meta.db_table,
ctid=ct.pk,
pk_col=model._meta.pk.column,
snapshots=int(conf.SNAPSHOTS),
field_list=", '" + "', '".join(field_names) + "'",
snap_of=trigger_type.snapshot_of,
field_list="'" + "', '".join(field_names) + "'",
)
)
return tr_name, field_names
Expand Down

0 comments on commit 739b3f3

Please sign in to comment.