@@ -20,6 +20,50 @@ FROM test_results r
2020WHERE d .last_auto_gen_date IS NOT NULL
2121 AND test_results .id = r .id ;
2222
23+ -- Handle duplicates before creating unique indexes
24+
25+ UPDATE test_definitions
26+ SET last_auto_gen_date = NULL
27+ WHERE last_auto_gen_date IS NOT NULL
28+ AND table_name IS NULL
29+ AND column_name IS NULL
30+ AND id NOT IN (
31+ SELECT DISTINCT ON (test_suite_id, test_type, schema_name) id
32+ FROM test_definitions
33+ WHERE last_auto_gen_date IS NOT NULL
34+ AND table_name IS NULL
35+ AND column_name IS NULL
36+ ORDER BY test_suite_id, test_type, schema_name, last_auto_gen_date
37+ );
38+
39+ UPDATE test_definitions
40+ SET last_auto_gen_date = NULL
41+ WHERE last_auto_gen_date IS NOT NULL
42+ AND table_name IS NOT NULL
43+ AND column_name IS NULL
44+ AND id NOT IN (
45+ SELECT DISTINCT ON (test_suite_id, test_type, schema_name, table_name) id
46+ FROM test_definitions
47+ WHERE last_auto_gen_date IS NOT NULL
48+ AND table_name IS NOT NULL
49+ AND column_name IS NULL
50+ ORDER BY test_suite_id, test_type, schema_name, table_name, last_auto_gen_date
51+ );
52+
53+ UPDATE test_definitions
54+ SET last_auto_gen_date = NULL
55+ WHERE last_auto_gen_date IS NOT NULL
56+ AND table_name IS NOT NULL
57+ AND column_name IS NOT NULL
58+ AND id NOT IN (
59+ SELECT DISTINCT ON (test_suite_id, test_type, schema_name, table_name, column_name) id
60+ FROM test_definitions
61+ WHERE last_auto_gen_date IS NOT NULL
62+ AND table_name IS NOT NULL
63+ AND column_name IS NOT NULL
64+ ORDER BY test_suite_id, test_type, schema_name, table_name, column_name, last_auto_gen_date
65+ );
66+
2367CREATE UNIQUE INDEX uix_td_autogen_schema
2468 ON test_definitions (test_suite_id, test_type, schema_name)
2569 WHERE last_auto_gen_date IS NOT NULL
0 commit comments