-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature - Current version history (#71)
* Adds a new feature to the extension that enables the current versions of records to also be stored in the history table * adding some friendly comments * Added an additional test to check current row ranges are set correctly doing an update and removed a redundant delete test Co-authored-by: Marcos César de Oliveira <[email protected]> Co-authored-by: Levente A <[email protected]> Co-authored-by: Simone Busoli <[email protected]>
- Loading branch information
1 parent
9abf823
commit 1b512f9
Showing
13 changed files
with
525 additions
and
48 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
*.tmp | ||
.DS_Store | ||
test/result | ||
test/result | ||
.env | ||
.envrc |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
-- No-op Update Test | ||
CREATE TABLE versioning_noop (a bigint, "b b" date, sys_period tstzrange); | ||
-- Insert initial data. | ||
INSERT INTO versioning_noop (a, "b b", sys_period) VALUES (1, '2020-01-01', tstzrange('2000-01-01', NULL)); | ||
CREATE TABLE versioning_noop_history (a bigint, "b b" date, sys_period tstzrange); | ||
CREATE TRIGGER versioning_noop_trigger | ||
BEFORE INSERT OR UPDATE OR DELETE ON versioning_noop | ||
FOR EACH ROW EXECUTE PROCEDURE versioning('sys_period', 'versioning_noop_history', false, true, true); | ||
-- Test no-op update (the row is updated without any value changes). | ||
BEGIN; | ||
UPDATE versioning_noop SET a = 1, "b b" = '2020-01-01' WHERE a = 1; | ||
-- Check that no history record was created. | ||
SELECT * FROM versioning_noop_history; -- Expecting 0 rows in history. | ||
a | b b | sys_period | ||
---+-----+------------ | ||
(0 rows) | ||
|
||
COMMIT; | ||
-- Cleanup | ||
DROP TABLE versioning_noop; | ||
DROP TABLE versioning_noop_history; |
This file contains 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
160 changes: 160 additions & 0 deletions
160
test/expected/versioning_including_current_version_in_history.out
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
CREATE TABLE versioning (a bigint, "b b" date, sys_period tstzrange); | ||
-- Insert some data before versioning is enabled. | ||
INSERT INTO versioning (a, sys_period) VALUES (1, tstzrange('-infinity', NULL)); | ||
INSERT INTO versioning (a, sys_period) VALUES (2, tstzrange('2000-01-01', NULL)); | ||
CREATE TABLE versioning_history (a bigint, c date, sys_period tstzrange); | ||
CREATE TRIGGER versioning_trigger | ||
BEFORE INSERT OR UPDATE OR DELETE ON versioning | ||
FOR EACH ROW EXECUTE PROCEDURE versioning('sys_period', 'versioning_history', false, false, true); | ||
-- Insert. | ||
BEGIN; | ||
INSERT INTO versioning (a) VALUES (3); | ||
SELECT a, "b b", lower(sys_period) = CURRENT_TIMESTAMP FROM versioning ORDER BY a, sys_period; | ||
a | b b | ?column? | ||
---+-----+---------- | ||
1 | | f | ||
2 | | f | ||
3 | | t | ||
(3 rows) | ||
|
||
SELECT a, c, lower(sys_period) = CURRENT_TIMESTAMP FROM versioning_history ORDER BY a, sys_period; | ||
a | c | ?column? | ||
---+---+---------- | ||
3 | | t | ||
(1 row) | ||
|
||
COMMIT; | ||
-- Make sure that the next transaction's CURRENT_TIMESTAMP is different. | ||
SELECT pg_sleep(0.1); | ||
pg_sleep | ||
---------- | ||
|
||
(1 row) | ||
|
||
-- Update. | ||
BEGIN; | ||
UPDATE versioning SET a = 4 WHERE a = 3; | ||
SELECT a, "b b", lower(sys_period) = CURRENT_TIMESTAMP FROM versioning ORDER BY a, sys_period; | ||
a | b b | ?column? | ||
---+-----+---------- | ||
1 | | f | ||
2 | | f | ||
4 | | t | ||
(3 rows) | ||
|
||
SELECT a, c, upper(sys_period) = CURRENT_TIMESTAMP FROM versioning_history ORDER BY a, sys_period; | ||
a | c | ?column? | ||
---+---+---------- | ||
3 | | t | ||
4 | | | ||
(2 rows) | ||
|
||
SELECT a, c, lower(sys_period) = CURRENT_TIMESTAMP FROM versioning_history ORDER BY a, sys_period; | ||
a | c | ?column? | ||
---+---+---------- | ||
3 | | | ||
4 | | t | ||
(2 rows) | ||
|
||
SELECT a, "b b" FROM versioning WHERE lower(sys_period) = CURRENT_TIMESTAMP ORDER BY a, sys_period; | ||
a | b b | ||
---+----- | ||
4 | | ||
(1 row) | ||
|
||
COMMIT; | ||
-- Make sure that the next transaction's CURRENT_TIMESTAMP is different. | ||
SELECT pg_sleep(0.1); | ||
pg_sleep | ||
---------- | ||
|
||
(1 row) | ||
|
||
-- Multiple updates. | ||
BEGIN; | ||
UPDATE versioning SET a = 5 WHERE a = 4; | ||
UPDATE versioning SET "b b" = '2012-01-01' WHERE a = 5; | ||
SELECT a, "b b", lower(sys_period) = CURRENT_TIMESTAMP FROM versioning ORDER BY a, sys_period; | ||
a | b b | ?column? | ||
---+------------+---------- | ||
1 | | f | ||
2 | | f | ||
5 | 2012-01-01 | t | ||
(3 rows) | ||
|
||
SELECT a, c, upper(sys_period) = CURRENT_TIMESTAMP FROM versioning_history ORDER BY a, sys_period; | ||
a | c | ?column? | ||
---+---+---------- | ||
3 | | f | ||
4 | | t | ||
5 | | t | ||
5 | | | ||
(4 rows) | ||
|
||
SELECT a, "b b" FROM versioning WHERE lower(sys_period) = CURRENT_TIMESTAMP ORDER BY a, sys_period; | ||
a | b b | ||
---+------------ | ||
5 | 2012-01-01 | ||
(1 row) | ||
|
||
COMMIT; | ||
-- Make sure that the next transaction's CURRENT_TIMESTAMP is different. | ||
SELECT pg_sleep(0.1); | ||
pg_sleep | ||
---------- | ||
|
||
(1 row) | ||
|
||
-- Delete. | ||
BEGIN; | ||
DELETE FROM versioning WHERE a = 4; | ||
SELECT a, "b b", lower(sys_period) = CURRENT_TIMESTAMP FROM versioning ORDER BY a, sys_period; | ||
a | b b | ?column? | ||
---+------------+---------- | ||
1 | | f | ||
2 | | f | ||
5 | 2012-01-01 | f | ||
(3 rows) | ||
|
||
SELECT a, c, upper(sys_period) = CURRENT_TIMESTAMP FROM versioning_history ORDER BY a, sys_period; | ||
a | c | ?column? | ||
---+---+---------- | ||
3 | | f | ||
4 | | f | ||
5 | | f | ||
5 | | | ||
(4 rows) | ||
|
||
SELECT a, "b b" FROM versioning WHERE lower(sys_period) = CURRENT_TIMESTAMP ORDER BY a, sys_period; | ||
a | b b | ||
---+----- | ||
(0 rows) | ||
|
||
END; | ||
-- Make sure that the next transaction's CURRENT_TIMESTAMP is different. | ||
SELECT pg_sleep(0.1); | ||
pg_sleep | ||
---------- | ||
|
||
(1 row) | ||
|
||
-- Delete. | ||
BEGIN; | ||
DELETE FROM versioning; | ||
SELECT * FROM versioning; | ||
a | b b | sys_period | ||
---+-----+------------ | ||
(0 rows) | ||
|
||
SELECT a, c, upper(sys_period) = CURRENT_TIMESTAMP FROM versioning_history ORDER BY a, sys_period; | ||
a | c | ?column? | ||
---+---+---------- | ||
3 | | f | ||
4 | | f | ||
5 | | f | ||
5 | | t | ||
(4 rows) | ||
|
||
END; | ||
DROP TABLE versioning; | ||
DROP TABLE versioning_history; |
29 changes: 29 additions & 0 deletions
29
test/expected/versioning_rollback_include_current_version_in_history.out
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
-- Rollback Behavior Test | ||
CREATE TABLE versioning_rollback (a bigint, "b b" date, sys_period tstzrange); | ||
-- Insert initial data. | ||
INSERT INTO versioning_rollback (a, "b b", sys_period) VALUES (1, '2020-01-01', tstzrange('2000-01-01', NULL)); | ||
CREATE TABLE versioning_rollback_history (a bigint, "b b" date, sys_period tstzrange); | ||
CREATE TRIGGER versioning_rollback_trigger | ||
BEFORE INSERT OR UPDATE OR DELETE ON versioning_rollback | ||
FOR EACH ROW EXECUTE PROCEDURE versioning('sys_period', 'versioning_rollback_history', false, false, true); | ||
-- Test rollback during update. | ||
BEGIN; | ||
UPDATE versioning_rollback SET a = 2 WHERE a = 1; | ||
-- Rollback the transaction. | ||
ROLLBACK; | ||
-- Ensure no history record was created. | ||
SELECT * FROM versioning_rollback_history; -- Expecting 0 rows in history. | ||
a | b b | sys_period | ||
---+-----+------------ | ||
(0 rows) | ||
|
||
-- Ensure original data is unchanged. | ||
SELECT * FROM versioning_rollback; -- Expecting original value a = 1. | ||
a | b b | sys_period | ||
---+------------+----------------------------- | ||
1 | 2020-01-01 | ["2000-01-01 00:00:00+00",) | ||
(1 row) | ||
|
||
-- Cleanup | ||
DROP TABLE versioning_rollback; | ||
DROP TABLE versioning_rollback_history; |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
-- No-op Update Test | ||
CREATE TABLE versioning_noop (a bigint, "b b" date, sys_period tstzrange); | ||
|
||
-- Insert initial data. | ||
INSERT INTO versioning_noop (a, "b b", sys_period) VALUES (1, '2020-01-01', tstzrange('2000-01-01', NULL)); | ||
|
||
CREATE TABLE versioning_noop_history (a bigint, "b b" date, sys_period tstzrange); | ||
|
||
CREATE TRIGGER versioning_noop_trigger | ||
BEFORE INSERT OR UPDATE OR DELETE ON versioning_noop | ||
FOR EACH ROW EXECUTE PROCEDURE versioning('sys_period', 'versioning_noop_history', false, true, true); | ||
|
||
-- Test no-op update (the row is updated without any value changes). | ||
BEGIN; | ||
|
||
UPDATE versioning_noop SET a = 1, "b b" = '2020-01-01' WHERE a = 1; | ||
|
||
-- Check that no history record was created. | ||
SELECT * FROM versioning_noop_history; -- Expecting 0 rows in history. | ||
|
||
COMMIT; | ||
|
||
-- Cleanup | ||
DROP TABLE versioning_noop; | ||
DROP TABLE versioning_noop_history; |
Oops, something went wrong.