24
24
id INTEGER PRIMARY KEY)
28
id SERIAL NOT NULL PRIMARY KEY,
29
project_id INTEGER NOT NULL REFERENCES project_twin ON DELETE CASCADE,
34
CREATE TABLE test_result (
35
test_id INTEGER NOT NULL REFERENCES test ON DELETE CASCADE,
37
date_created TIMESTAMP WITHOUT TIME ZONE NOT NULL,
38
date_deleted TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT 'infinity')
41
27
CREATE TABLE test_run (
42
28
id SERIAL NOT NULL PRIMARY KEY,
43
29
person_id INTEGER NOT NULL REFERENCES person_twin ON DELETE CASCADE,
44
project_id INTEGER NOT NULL REFERENCES project_twin ON DELETE CASCADE,
45
date_created TIMESTAMP WITHOUT TIME ZONE NOT NULL
46
DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC'))
49
CREATE INDEX test_run_project_id_idx ON test_run (project_id)
30
project_id INTEGER REFERENCES project_twin ON DELETE CASCADE,
31
date_created TIMESTAMP WITHOUT TIME ZONE NOT NULL
32
DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC'),
33
CONSTRAINT valid_target CHECK (project_id IS NOT NULL))
36
CREATE INDEX test_run__person_id__idx ON test_run (person_id)
39
CREATE UNIQUE INDEX test_run__project_id__date_created__key ON test_run (project_id, date_created) WHERE project_id IS NOT NULL
42
CREATE TABLE test_case (
43
id SERIAL NOT NULL PRIMARY KEY,
44
project_id INTEGER REFERENCES project_twin ON DELETE CASCADE,
46
CONSTRAINT valid_target CHECK (project_id IS NOT NULL))
49
CREATE UNIQUE INDEX test_case__project_id__name__key ON test_case (project_id, name) WHERE project_id IS NOT NULL
52
CREATE TABLE test_result (
53
test_case_id INTEGER NOT NULL REFERENCES test_case ON DELETE CASCADE,
54
status INTEGER NOT NULL DEFAULT 2, -- UNINITIATED
56
date_created TIMESTAMP WITHOUT TIME ZONE NOT NULL
57
DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC'),
58
date_deleted TIMESTAMP WITHOUT TIME ZONE NOT NULL
60
CONSTRAINT valid_dates CHECK (date_created < date_deleted))
63
CREATE UNIQUE INDEX test_result__test_id__date_created__key ON test_result (test_case_id, date_created)
66
CREATE INDEX test_result__test_id__idx ON test_result (test_case_id)
69
CREATE INDEX test_result__date_created__date_deleted__idx ON test_result (date_created, date_deleted)
72
CREATE INDEX test_result__test_id__status__output__date_created__date_deleted__idx ON test_result (test_case_id, status, decode(md5(output), 'hex'), date_created, date_deleted)
75
CREATE LANGUAGE plpgsql
78
CREATE FUNCTION test_result_delete() RETURNS TRIGGER
82
test_result_record RECORD;
84
-- Check if there is a test_result between two equivalent dimensions
85
SELECT INTO test_result_record r2.date_deleted FROM test_result r1
87
ON r1.test_case_id = r2.test_case_id
88
AND r1.status = r2.status
89
AND decode(md5(r1.output), 'hex') = decode(md5(r2.output), 'hex')
90
AND r1.date_deleted = OLD.date_created
91
AND r2.date_created = OLD.date_deleted;
93
DELETE FROM test_result
94
WHERE test_result.test_case_id = OLD.test_case_id
95
AND test_result.date_created = OLD.date_deleted;
96
UPDATE test_result SET date_deleted = test_result_record.date_deleted
97
WHERE test_result.test_case_id = OLD.test_case_id
98
AND test_result.date_deleted = OLD.date_created;
100
-- Extend forward dimension
101
UPDATE test_result SET date_deleted = OLD.date_deleted
102
WHERE test_result.test_case_id = OLD.test_case_id
103
AND test_result.date_deleted = OLD.date_created;
105
-- Reduce backward dimension
106
UPDATE test_result set date_created = OLD.date_created
107
WHERE test_result.test_case_id = OLD.test_case_id
108
AND test_result.date_created = OLD.date_deleted;
117
CREATE TRIGGER test_result_delete_trigger
118
AFTER DELETE ON test_result
120
EXECUTE PROCEDURE test_result_delete()
123
CREATE OR REPLACE FUNCTION test_result_update() RETURNS TRIGGER
127
IF OLD.test_case_id != NEW.test_case_id
128
OR OLD.status != NEW.status
129
OR OLD.output != NEW.output THEN
130
DELETE FROM test_result
131
WHERE test_case_id = OLD.test_case_id
132
AND date_created = OLD.date_created;
133
INSERT INTO test_result (
153
CREATE TRIGGER test_result_update_trigger
154
BEFORE UPDATE ON test_result
156
EXECUTE PROCEDURE test_result_update()
159
CREATE OR REPLACE FUNCTION test_result_insert() RETURNS TRIGGER
163
test_result_record RECORD;
165
-- First, check for the most common case when
166
-- there is a test result in a backward dimension.
167
SELECT INTO test_result_record test_result.status, test_result.output
169
WHERE test_result.test_case_id = NEW.test_case_id
170
AND test_result.date_created <= NEW.date_created
171
AND test_result.date_deleted > NEW.date_created;
173
IF NEW.status = test_result_record.status
174
AND NEW.output = test_result_record.output THEN
175
-- No need to extend forward dimension
178
-- Reducing backward dimension
179
UPDATE test_result SET date_deleted = NEW.date_created
180
WHERE test_result.test_case_id = NEW.test_case_id
181
AND test_result.date_created <= NEW.date_created
182
AND test_result.date_deleted > NEW.date_created;
186
-- Then, check when there is a test result in a forward dimension.
187
SELECT INTO test_result_record test_result.status, test_result.output, test_result.date_created
189
WHERE test_result.test_case_id = NEW.test_case_id
190
AND test_result.date_created > NEW.date_created
191
ORDER BY date_created LIMIT 1;
193
IF NEW.status = test_result_record.status
194
AND NEW.output = test_result_record.output THEN
195
-- Extending forward dimension
196
UPDATE test_result SET date_created = NEW.date_created
197
WHERE test_result.test_case_id = NEW.test_case_id
198
AND test_result.date_created = test_result_record.date_created;
201
-- Reducing forward dimension
202
NEW.date_deleted := test_result_record.date_created;
211
CREATE TRIGGER test_result_insert_trigger
212
BEFORE INSERT ON test_result
214
EXECUTE PROCEDURE test_result_insert()
217
CREATE FUNCTION get_test_case_id(
218
test_project_id INTEGER, test_name TEXT) RETURNS INTEGER
222
test_case_id INTEGER;
225
SELECT test_case.id INTO test_case_id FROM test_case
226
WHERE test_case.project_id = test_project_id
227
AND test_case.name = test_name;
234
INSERT INTO test_case (
240
RETURNING id INTO test_case_id;
243
WHEN unique_violation THEN
251
CREATE OR REPLACE FUNCTION add_test_result(
252
test_result_test_case_id INTEGER, test_result_status INTEGER,
253
test_result_output TEXT, test_result_date_created TIMESTAMP)
260
INSERT INTO test_result (
266
test_result_test_case_id,
269
test_result_date_created);
272
WHEN unique_violation THEN
282
"DROP FUNCTION add_test_result(INTEGER, TEXT, TIMESTAMP)",
283
"DROP FUNCTION get_test_case_id(INTEGER, TEXT, TEXT)",
284
"DROP FUNCTION test_result_insert()",
285
"DROP FUNCTION test_result_update()",
286
"DROP FUNCTION test_result_delete()",
287
"DROP LANGUAGE plpgsql",
288
"DROP TABLE test_result",
289
"DROP TABLE test_case",
54
290
"DROP TABLE test_run",
55
"DROP TABLE test_result",
57
291
"DROP TABLE project_twin",
58
292
"DROP TABLE person_twin",
296
"DELETE FROM test_result",
297
"DELETE FROM test_case",
62
298
"DELETE FROM test_run",
63
"DELETE FROM test_result",
65
299
"DELETE FROM project_twin",
66
300
"DELETE FROM person_twin",