Skip to content

Commit

Permalink
Separately MERGE ungrouped/grouped and dynamic grouped predefined labels
Browse files Browse the repository at this point in the history
Because the MERGE only inspected the label name (which is null for
predefined grouped labels), we were incorrectly falling into the not
matched condition and inserting duplicate rows.

Closes sundeck-io#400
  • Loading branch information
joshelser committed Oct 3, 2023
1 parent 9d9a9f0 commit 650ad35
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion bootstrap/004_labels.sql
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,9 @@ BEGIN
RETURN FALSE;
END IF;

-- Merge in ungrouped and grouped predefined labels
MERGE INTO internal.labels t
USING internal.predefined_labels s
USING (select * from internal.predefined_labels where name is not null) s
ON t.name = s.name
WHEN MATCHED THEN
UPDATE
Expand All @@ -349,6 +350,17 @@ BEGIN
("NAME", "GROUP_NAME", "GROUP_RANK", "LABEL_CREATED_AT", "CONDITION", "LABEL_MODIFIED_AT", "IS_DYNAMIC", "ENABLED")
VALUES (s.name, s.GROUP_NAME, s.GROUP_RANK, S.LABEL_CREATED_AT, s.condition, S.LABEL_CREATED_AT, S.IS_DYNAMIC, S.ENABLED);

-- Merge in dynamic grouped predefined labels
MERGE INTO internal.labels t
USING (select * from internal.predefined_labels where name is null) s
ON t.group_name = s.group_name and t.name is null
WHEN MATCHED THEN
UPDATE
SET t.NAME = s.NAME, t.GROUP_RANK = s.GROUP_RANK, t.CONDITION = s.condition, t.LABEL_MODIFIED_AT = s.LABEL_CREATED_AT, t.IS_DYNAMIC = s.IS_DYNAMIC, t.ENABLED = s.ENABLED
WHEN NOT MATCHED THEN
INSERT
("NAME", "GROUP_RANK", "LABEL_CREATED_AT", "CONDITION", "LABEL_MODIFIED_AT", "IS_DYNAMIC", "ENABLED")
VALUES (s.name, s.GROUP_RANK, S.LABEL_CREATED_AT, s.condition, S.LABEL_CREATED_AT, S.IS_DYNAMIC, S.ENABLED);
return TRUE;
END;
$$;
Expand Down

0 comments on commit 650ad35

Please sign in to comment.