1
# Copyright 2010-2011 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
5
Added support for inserting test runs backwards in time.
9
"""DROP FUNCTION next_distroseries_sequence(INTEGER)""",
10
"""DROP FUNCTION next_projectseries_sequence(INTEGER)""",
11
"""DROP TRIGGER testresult_delete_after_trigger ON testresult""",
12
"""DROP FUNCTION testresult_delete_after()""",
14
CREATE OR REPLACE FUNCTION testrun_insert_before() RETURNS TRIGGER
18
IF NEW.distro_series IS NOT NULL THEN
19
UPDATE distroseriestwin SET counter = counter + 1
20
WHERE id = NEW.distro_series RETURNING counter INTO NEW.sequence;
21
ELSIF NEW.project_series IS NOT NULL THEN
22
UPDATE projectseriestwin SET counter = counter + 1
23
WHERE id = NEW.project_series RETURNING counter INTO NEW.sequence;
31
CREATE OR REPLACE FUNCTION testrun_insert_after() RETURNS TRIGGER
35
INSERT INTO testresult (
45
MIN(testrun.date_created),
46
testresult.date_deleted
49
ON testrun.date_created > NEW.date_created
50
AND testrun.date_created < testresult.date_deleted
51
WHERE testresult.date_created < NEW.date_created
52
AND CASE WHEN NEW.distro_series IS NOT NULL THEN
53
testrun.distro_series = NEW.distro_series
55
testrun.project_series = NEW.project_series
61
testresult.date_deleted;
64
SET date_deleted = NEW.date_created
66
WHERE testresult.date_created < NEW.date_created
67
AND testresult.date_deleted > NEW.date_created
68
AND testresult.test_case = testcase.id
69
AND CASE WHEN NEW.distro_series IS NOT NULL THEN
70
testcase.distro_series = NEW.distro_series
72
testcase.project_series = NEW.project_series
80
DROP TRIGGER testrun_insert_after_trigger
84
CREATE TRIGGER testrun_insert_after_trigger
85
AFTER INSERT ON testrun
87
EXECUTE PROCEDURE testrun_insert_after()
90
CREATE FUNCTION testrun_update_before() RETURNS TRIGGER
94
IF OLD.distro_series <> NEW.distro_series THEN
95
RAISE EXCEPTION 'Cannot update distro_series';
96
ELSIF OLD.project_series <> NEW.project_series THEN
97
RAISE EXCEPTION 'Cannot update project_series';
98
ELSIF OLD.date_created <> NEW.date_created THEN
99
RAISE EXCEPTION 'Cannot update project_series';
108
ON FUNCTION testrun_update_before()
112
CREATE TRIGGER testrun_update_before_trigger
113
BEFORE UPDATE ON testrun
115
EXECUTE PROCEDURE testrun_update_before()
118
CREATE FUNCTION testrun_delete_after() RETURNS TRIGGER
128
WHERE testrun.date_created > OLD.date_created
129
AND CASE WHEN OLD.distro_series IS NOT NULL THEN
130
testrun.distro_series = OLD.distro_series
132
testrun.project_series = OLD.project_series
134
ORDER BY testrun.date_created ASC LIMIT 1;
137
IF next_testrun.date_created IS NOT NULL THEN
138
UPDATE testresult tr1
139
SET date_deleted = tr2.date_deleted
140
FROM testresult tr2, testcase tc
141
WHERE tr1.test_case = tr2.test_case
142
AND tr1.status = tr2.status
143
AND decode(md5(tr1.output), 'hex') = decode(md5(tr2.output), 'hex')
144
AND tr1.date_deleted = OLD.date_created
145
AND tr2.date_created = next_testrun.date_created
146
AND tr1.test_case = tc.id
147
AND CASE WHEN OLD.distro_series IS NOT NULL THEN
148
tc.distro_series = OLD.distro_series
150
tc.project_series = OLD.project_series
153
DELETE FROM testresult tr2
154
USING testresult tr1, testcase tc
156
tr1.test_case = tr2.test_case
157
AND tr1.status = tr2.status
158
AND decode(md5(tr1.output), 'hex')
159
= decode(md5(tr2.output), 'hex')
160
AND tr2.date_created = next_testrun.date_created)
162
tr2.date_created = OLD.date_created
163
AND tr2.date_deleted = next_testrun.date_created)
164
AND tr1.test_case = tc.id
165
AND CASE WHEN OLD.distro_series IS NOT NULL THEN
166
tc.distro_series = OLD.distro_series
168
tc.project_series = OLD.project_series
171
DELETE FROM testresult tr
173
WHERE tr.date_created = OLD.date_created
174
AND tr.date_deleted = next_testrun.date_created
175
AND tr.test_case = tc.id
176
AND CASE WHEN OLD.distro_series IS NOT NULL THEN
177
tc.distro_series = OLD.distro_series
179
tc.project_series = OLD.project_series
185
SET date_created = next_testrun.date_created
187
WHERE tr.date_created = OLD.date_created
188
AND tr.test_case = testcase.id
189
AND CASE WHEN OLD.distro_series IS NOT NULL THEN
190
testcase.distro_series = OLD.distro_series
192
testcase.project_series = OLD.project_series
195
next_testrun.date_created = '3000-01-01';
196
DELETE FROM testresult tr
198
WHERE tr.date_created = OLD.date_created
199
AND tr.test_case = tc.id
200
AND CASE WHEN OLD.distro_series IS NOT NULL THEN
201
tc.distro_series = OLD.distro_series
203
tc.project_series = OLD.project_series
209
SET date_deleted = next_testrun.date_created
211
WHERE tr.date_deleted = OLD.date_created
212
AND tr.test_case = tc.id
213
AND CASE WHEN OLD.distro_series IS NOT NULL THEN
214
tc.distro_series = OLD.distro_series
216
tc.project_series = OLD.project_series
225
ON FUNCTION testrun_delete_after()
229
CREATE TRIGGER testrun_delete_after_trigger
230
AFTER DELETE ON testrun
232
EXECUTE PROCEDURE testrun_delete_after()
235
CREATE OR REPLACE FUNCTION testresult_insert_before() RETURNS TRIGGER
239
next_date_created DATE;
241
previous_date_deleted DATE;
243
IF NEW.date_deleted < '3000-01-01' THEN
247
SELECT INTO next_testrun
248
CASE WHEN MIN(date_created) > NEW.date_created
249
THEN MIN(date_created)
250
ELSE MIN(date_deleted)
254
ON tr.test_case = tc1.id
256
ON tc1.distro_series IS NOT DISTINCT FROM tc2.distro_series
257
AND tc1.project_series IS NOT DISTINCT FROM tc2.project_series
258
WHERE date_deleted > NEW.date_created
259
AND tc2.id = NEW.test_case;
260
IF next_testrun < '3000-01-01' THEN
261
-- Set date created of next test result.
263
SET date_created = CASE
264
WHEN testresult.status = NEW.status
265
AND testresult.output = NEW.output
266
AND testresult.date_created <> NEW.date_created
267
THEN NEW.date_created
270
WHERE testresult.test_case = NEW.test_case
271
AND testresult.date_created <= next_testrun
272
AND testresult.date_created >= NEW.date_created
273
RETURNING date_created INTO next_date_created;
274
IF next_date_created != NEW.date_created THEN
275
next_date_created := next_testrun;
276
NEW.date_deleted := next_testrun;
279
next_date_created := '3000-01-01';
282
-- Set date deleted of previous test result.
284
SET date_deleted = CASE
285
WHEN testresult.status = NEW.status
286
AND testresult.output = NEW.output
287
THEN next_date_created
288
ELSE NEW.date_created
290
WHERE testresult.test_case = NEW.test_case
291
AND testresult.date_created <= NEW.date_created
292
AND testresult.date_deleted >= NEW.date_created
293
RETURNING date_deleted INTO previous_date_deleted;
294
IF previous_date_deleted = next_date_created THEN
295
IF next_date_created = NEW.date_created THEN
296
DELETE FROM testresult
297
WHERE test_case = NEW.test_case
298
AND testresult.date_created = next_testrun;
301
ELSIF next_date_created = NEW.date_created
302
AND previous_date_deleted IS NOT NULL THEN
311
CREATE OR REPLACE FUNCTION testresult_update_before() RETURNS TRIGGER
315
IF OLD.test_case <> NEW.test_case THEN
316
RAISE EXCEPTION 'Cannot update test_case';
317
ELSIF OLD.status <> NEW.status THEN
318
RAISE EXCEPTION 'Cannot update status';
319
ELSIF OLD.output <> NEW.output THEN
320
RAISE EXCEPTION 'Cannot update output';
321
ELSIF NEW.date_deleted = NEW.date_created THEN
322
DELETE FROM testresult
323
WHERE test_case = OLD.test_case
324
AND date_created = OLD.date_created;
337
for statement in statements:
338
store.execute(statement)