-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't create versions that would be identical to previous version (#35)
- Loading branch information
1 parent
4894907
commit 9255241
Showing
6 changed files
with
131 additions
and
11 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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
CREATE TABLE versioning (a bigint, b bigint, sys_period tstzrange); | ||
-- Insert some data before versioning is enabled. | ||
INSERT INTO versioning (a, b, sys_period) VALUES (2, 2, tstzrange('2000-01-01', NULL)); | ||
CREATE TABLE versioning_history (b bigint, 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, true); | ||
-- Update with no changes that would affect history | ||
BEGIN; | ||
UPDATE versioning SET a = 3; | ||
SELECT a, b FROM versioning ORDER BY a, sys_period; | ||
a | b | ||
---+--- | ||
3 | 2 | ||
(1 row) | ||
|
||
SELECT b, upper(sys_period) = CURRENT_TIMESTAMP FROM versioning_history ORDER BY b, sys_period; | ||
b | ?column? | ||
---+---------- | ||
(0 rows) | ||
|
||
SELECT a, b FROM versioning WHERE lower(sys_period) = CURRENT_TIMESTAMP ORDER BY a, sys_period; | ||
a | b | ||
---+--- | ||
(0 rows) | ||
|
||
COMMIT; | ||
-- Make sure that the next transaction's CURRENT_TIMESTAMP is different. | ||
SELECT pg_sleep(0.1); | ||
pg_sleep | ||
---------- | ||
|
||
(1 row) | ||
|
||
-- Update with changes that would affect history. | ||
BEGIN; | ||
UPDATE versioning SET b = 3; | ||
SELECT a, b, lower(sys_period) = CURRENT_TIMESTAMP FROM versioning ORDER BY a, sys_period; | ||
a | b | ?column? | ||
---+---+---------- | ||
3 | 3 | t | ||
(1 row) | ||
|
||
SELECT b, upper(sys_period) = CURRENT_TIMESTAMP FROM versioning_history ORDER BY b, sys_period; | ||
b | ?column? | ||
---+---------- | ||
2 | t | ||
(1 row) | ||
|
||
SELECT a, b FROM versioning WHERE lower(sys_period) = CURRENT_TIMESTAMP ORDER BY a, sys_period; | ||
a | b | ||
---+--- | ||
3 | 3 | ||
(1 row) | ||
|
||
COMMIT; | ||
DROP TABLE versioning; | ||
DROP TABLE versioning_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,42 @@ | ||
CREATE TABLE versioning (a bigint, b bigint, sys_period tstzrange); | ||
|
||
-- Insert some data before versioning is enabled. | ||
INSERT INTO versioning (a, b, sys_period) VALUES (2, 2, tstzrange('2000-01-01', NULL)); | ||
|
||
CREATE TABLE versioning_history (b bigint, 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, true); | ||
|
||
-- Update with no changes that would affect history | ||
BEGIN; | ||
|
||
UPDATE versioning SET a = 3; | ||
|
||
SELECT a, b FROM versioning ORDER BY a, sys_period; | ||
|
||
SELECT b, upper(sys_period) = CURRENT_TIMESTAMP FROM versioning_history ORDER BY b, sys_period; | ||
|
||
SELECT a, b FROM versioning WHERE lower(sys_period) = CURRENT_TIMESTAMP ORDER BY a, sys_period; | ||
|
||
COMMIT; | ||
|
||
-- Make sure that the next transaction's CURRENT_TIMESTAMP is different. | ||
SELECT pg_sleep(0.1); | ||
|
||
-- Update with changes that would affect history. | ||
BEGIN; | ||
|
||
UPDATE versioning SET b = 3; | ||
|
||
SELECT a, b, lower(sys_period) = CURRENT_TIMESTAMP FROM versioning ORDER BY a, sys_period; | ||
|
||
SELECT b, upper(sys_period) = CURRENT_TIMESTAMP FROM versioning_history ORDER BY b, sys_period; | ||
|
||
SELECT a, b FROM versioning WHERE lower(sys_period) = CURRENT_TIMESTAMP ORDER BY a, sys_period; | ||
|
||
COMMIT; | ||
|
||
DROP TABLE versioning; | ||
DROP TABLE versioning_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