-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathstructure.sql
100 lines (59 loc) · 2.28 KB
/
structure.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
CREATE TABLE structure (a bigint, "b b" date, d text, sys_period tstzrange);
CREATE TABLE structure_history (like structure);
CREATE TRIGGER versioning_trigger
BEFORE INSERT OR UPDATE OR DELETE ON structure
FOR EACH ROW EXECUTE PROCEDURE versioning('sys_period', 'structure_history', false);
-- Insert.
BEGIN;
INSERT INTO structure (a, "b b", d) VALUES (1, '2000-01-01', 'test');
SELECT a, "b b", d FROM structure ORDER BY a, sys_period;
SELECT * FROM structure_history ORDER BY a, sys_period;
COMMIT;
-- Make sure that the next transaction's CURRENT_TIMESTAMP is different.
SELECT pg_sleep(0.1);
-- Update.
BEGIN;
UPDATE structure SET d = 'blah' WHERE a = 1;
SELECT a, "b b", d FROM structure ORDER BY a, sys_period;
SELECT a, "b b", d FROM structure_history ORDER BY a, sys_period;
COMMIT;
-- Make sure that the next transaction's CURRENT_TIMESTAMP is different.
SELECT pg_sleep(0.1);
-- Drop column in the versioned table.
ALTER TABLE structure DROP COLUMN d;
-- Update.
BEGIN;
UPDATE structure SET "b b" = '2001-01-01' WHERE a = 1;
SELECT a, "b b" FROM structure ORDER BY a, sys_period;
SELECT a, "b b", d FROM structure_history ORDER BY a, sys_period;
COMMIT;
-- Make sure that the next transaction's CURRENT_TIMESTAMP is different.
SELECT pg_sleep(0.1);
-- Add column to the versioned table.
ALTER TABLE structure ADD COLUMN e text;
-- Update.
BEGIN;
UPDATE structure SET e = 'test' WHERE a = 1;
SELECT a, "b b", e FROM structure ORDER BY a, sys_period;
SELECT a, "b b", d FROM structure_history ORDER BY a, sys_period;
COMMIT;
-- Make sure that the next transaction's CURRENT_TIMESTAMP is different.
SELECT pg_sleep(0.1);
-- Add column in the history table.
ALTER TABLE structure_history ADD COLUMN e text;
-- Update.
BEGIN;
UPDATE structure SET e = 'test2' WHERE a = 1;
SELECT a, "b b", e FROM structure ORDER BY a, sys_period;
SELECT a, "b b", d, e FROM structure_history ORDER BY a, sys_period;
COMMIT;
-- Make sure that the next transaction's CURRENT_TIMESTAMP is different.
SELECT pg_sleep(0.1);
-- Drop column in the history table.
ALTER TABLE structure_history DROP COLUMN "b b";
-- Update.
BEGIN;
UPDATE structure SET "b b" = '2012-01-01' WHERE a = 1;
SELECT a, "b b", e FROM structure ORDER BY a, sys_period;
SELECT a, d, e FROM structure_history ORDER BY a, sys_period;
COMMIT;