Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#33: Fix trigger if table contains column with non-equality type for ignore_unchanged_values=false #34

Merged
merged 4 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ Should return something similar to:

### Ignore updates without actual change

**NOTE: This feature does not work for tables with columns with types that does not support equality operator (e.g. PostGIS types, JSON types, etc.).**

By default this extension creates a record in the history table for every update that occurs in the versioned table, regardless of any change actually happening.

We added a fourth paramater to the trigger to change this behaviour and only record updates that result in an actual change.
Expand Down
7 changes: 7 additions & 0 deletions test/expected/non_equality_types.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SET client_min_messages TO error
CREATE TABLE non_equality_types (json json, sys_period tstzrange)
CREATE TRIGGER versioning_trigger
BEFORE INSERT OR UPDATE OR DELETE ON non_equality_types
FOR EACH ROW EXECUTE PROCEDURE versioning('sys_period', 'non_equality_types', false)
INSERT INTO non_equality_types VALUES ('{"a":1}'::json)
UPDATE non_equality_types SET json = '{"a":2}'::json WHERE 1=1
9 changes: 9 additions & 0 deletions test/expected/non_equality_types_unchanged_values.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SET client_min_messages TO error
CREATE TABLE non_equality_types_unchanged_values (json json, sys_period tstzrange)
CREATE TRIGGER versioning_trigger
BEFORE INSERT OR UPDATE OR DELETE ON non_equality_types_unchanged_values
FOR EACH ROW EXECUTE PROCEDURE versioning('sys_period', 'non_equality_types_unchanged_values', false, true)
INSERT INTO non_equality_types_unchanged_values VALUES ('{"a":1}'::json)
UPDATE non_equality_types_unchanged_values SET json = '{"a":2}'::json WHERE 1=1
ERROR: could not identify an equality operator for type json
CONTEXT: PL/pgSQL function versioning() line 39 at IF
5 changes: 3 additions & 2 deletions test/runTest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ TESTS="
no_history_table no_history_system_period no_system_period
invalid_system_period_values invalid_system_period invalid_types
versioning upper_case structure combinations
different_schema unchanged_values unchanged_version_values"
different_schema unchanged_values unchanged_version_values
non_equality_types non_equality_types_unchanged_values"

for name in $TESTS; do
echo ""
Expand All @@ -20,4 +21,4 @@ for name in $TESTS; do
done


psql -q -c "drop database temporal_tables_test;"
psql -q -c "drop database temporal_tables_test;"
6 changes: 4 additions & 2 deletions test/runTestNochecks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ mkdir -p test/result

TESTS="
versioning upper_case structure combinations different_schema unchanged_values
unchanged_version_values"
non_equality_types non_equality_types_unchanged_values
unchanged_version_values
"

for name in $TESTS; do
echo ""
Expand All @@ -18,4 +20,4 @@ for name in $TESTS; do
done


psql -q -c "drop database temporal_tables_test;"
psql -q -c "drop database temporal_tables_test;"
11 changes: 11 additions & 0 deletions test/sql/non_equality_types.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SET client_min_messages TO error;

CREATE TABLE non_equality_types (json json, sys_period tstzrange);

CREATE TRIGGER versioning_trigger
BEFORE INSERT OR UPDATE OR DELETE ON non_equality_types
FOR EACH ROW EXECUTE PROCEDURE versioning('sys_period', 'non_equality_types', false);

INSERT INTO non_equality_types VALUES ('{"a":1}'::json);

UPDATE non_equality_types SET json = '{"a":2}'::json WHERE 1=1;
11 changes: 11 additions & 0 deletions test/sql/non_equality_types_unchanged_values.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SET client_min_messages TO error;

CREATE TABLE non_equality_types_unchanged_values (json json, sys_period tstzrange);

CREATE TRIGGER versioning_trigger
BEFORE INSERT OR UPDATE OR DELETE ON non_equality_types_unchanged_values
FOR EACH ROW EXECUTE PROCEDURE versioning('sys_period', 'non_equality_types_unchanged_values', false, true);

INSERT INTO non_equality_types_unchanged_values VALUES ('{"a":1}'::json);

UPDATE non_equality_types_unchanged_values SET json = '{"a":2}'::json WHERE 1=1;
6 changes: 6 additions & 0 deletions versioning_function.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ BEGIN
history_table := TG_ARGV[1];
ignore_unchanged_values := TG_ARGV[3];

IF ignore_unchanged_values AND TG_OP = 'UPDATE' THEN
IF NEW IS NOT DISTINCT FROM OLD THEN
RETURN OLD;
END IF;
END IF;

-- check if sys_period exists on original table
SELECT atttypid, attndims INTO holder FROM pg_attribute WHERE attrelid = TG_RELID AND attname = sys_period AND NOT attisdropped;
IF NOT FOUND THEN
Expand Down
7 changes: 6 additions & 1 deletion versioning_function_nochecks.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ BEGIN
history_table := TG_ARGV[1];
ignore_unchanged_values := TG_ARGV[3];

IF ignore_unchanged_values AND TG_OP = 'UPDATE' THEN
IF NEW IS NOT DISTINCT FROM OLD THEN
RETURN OLD;
END IF;
END IF;

IF TG_OP = 'UPDATE' OR TG_OP = 'DELETE' THEN
-- Ignore rows already modified in the current transaction
Expand Down Expand Up @@ -88,4 +93,4 @@ BEGIN

RETURN OLD;
END;
$$ LANGUAGE plpgsql;
$$ LANGUAGE plpgsql;
Loading