Skip to content

Commit 58cdf34

Browse files
committed
migration: Update rows that conflict with new constraints
1 parent 99b468f commit 58cdf34

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

pgcommitfest/commitfest/migrations/0012_add_status_related_constraints.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ class Migration(migrations.Migration):
1818
),
1919
migrations.RunSQL(
2020
"""
21+
WITH cte AS (
22+
SELECT * FROM (
23+
SELECT
24+
id,
25+
patch_id,
26+
status,
27+
ROW_NUMBER() OVER (PARTITION BY patch_id ORDER BY commitfest_id DESC) AS rn
28+
FROM
29+
commitfest_patchoncommitfest
30+
WHERE
31+
status NOT IN (5)
32+
) q
33+
WHERE rn > 1
34+
)
35+
UPDATE commitfest_patchoncommitfest
36+
SET status = 5
37+
WHERE id IN (
38+
SELECT id
39+
FROM cte
40+
);
2141
CREATE UNIQUE INDEX poc_enforce_maxoneoutcome_idx
2242
ON commitfest_patchoncommitfest (patch_id)
2343
WHERE status not in (5);
@@ -28,6 +48,15 @@ class Migration(migrations.Migration):
2848
),
2949
migrations.RunSQL(
3050
"""
51+
UPDATE commitfest_patchoncommitfest
52+
SET leavedate =
53+
CASE
54+
WHEN status IN (4,5,6,7,8) THEN NOW()
55+
ELSE NULL
56+
END
57+
WHERE
58+
(status IN (4,5,6,7,8) AND leavedate IS NULL)
59+
OR (status NOT IN (4,5,6,7,8) AND leavedate IS NOT NULL);
3160
ALTER TABLE commitfest_patchoncommitfest
3261
ADD CONSTRAINT status_and_leavedate_correlation
3362
CHECK ((status IN (4,5,6,7,8)) = (leavedate IS NOT NULL));

0 commit comments

Comments
 (0)