Skip to content

Commit

Permalink
feat: update_config_change func
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Sep 6, 2024
1 parent c444740 commit 6ab2b8b
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions views/030_config_changes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
CREATE OR REPLACE FUNCTION update_config_change(
increment_count integer,
p_id uuid,
p_change_type TEXT,
p_count INTEGER,
p_created_by uuid,
p_details jsonb,
p_diff TEXT,
p_external_change_id TEXT,
p_external_created_by TEXT,
p_patches jsonb,
p_severity TEXT,
p_source TEXT,
p_summary TEXT
)
RETURNS void
AS $$
DECLARE current_details jsonb;
BEGIN
SELECT details INTO current_details FROM config_changes WHERE id = p_id;

UPDATE config_changes
SET
change_type = p_change_type,
count = CASE
WHEN current_details IS DISTINCT FROM p_details THEN count + increment_count
ELSE count
END,
created_at = NOW(),
created_by = p_created_by,
details = p_details,
diff = p_diff,
external_change_id = p_external_change_id,
external_created_by = p_external_created_by,
patches = p_patches,
severity = p_severity,
source = p_source,
summary = p_summary
WHERE
id = p_id;
EXCEPTION
WHEN unique_violation THEN
IF sqlerrm LIKE '%config_changes_config_id_external_change_id_key%' THEN
UPDATE config_changes
SET
change_type = p_change_type,
count = CASE
WHEN current_details IS DISTINCT FROM p_details THEN count + increment_count
ELSE count
END,
created_at = NOW(),
created_by = p_created_by,
details = p_details,
diff = p_diff,
external_created_by = p_external_created_by,
patches = p_patches,
severity = p_severity,
source = p_source,
summary = p_summary
WHERE
external_change_id = p_external_change_id;
ELSE
RAISE;
END IF;
WHEN OTHERS THEN
RAISE;
END;
$$ LANGUAGE plpgsql;

0 comments on commit 6ab2b8b

Please sign in to comment.