style: adjust the style of migration SQL (#18475)

Signed-off-by: chlins <chenyuzh@vmware.com>
This commit is contained in:
Chlins Zhang 2023-04-04 17:37:27 +08:00 committed by GitHub
parent 62bb56daf6
commit 72a6ef2514
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 24 deletions

View File

@ -91,41 +91,29 @@ SET enabled = false,
description = 'Chartmuseum is deprecated in Harbor v2.8.0, because this notification policy only has event type about Chartmuseum, so please update or delete this notification policy.' description = 'Chartmuseum is deprecated in Harbor v2.8.0, because this notification policy only has event type about Chartmuseum, so please update or delete this notification policy.'
WHERE event_types = '[]'; WHERE event_types = '[]';
/* insert the default payload_format for http type webhook target */ /* insert the default payload_format for http type webhook target
1. separate the original targets(text) to json array elements(targets_expanded)
2. update the old target to set the payload format if type is 'http' into the targets_updated
3. finally update back to the original table notification_policy
*/
WITH targets_expanded AS ( WITH targets_expanded AS (
-- Expand the JSON array of targets into separate rows SELECT id, jsonb_array_elements(targets::jsonb) AS target
SELECT FROM notification_policy
id,
jsonb_array_elements(targets::jsonb) AS target
FROM
notification_policy
), ),
targets_updated AS ( targets_updated AS (
-- Update targets based on the specified conditions SELECT id,
SELECT
id,
jsonb_agg( jsonb_agg(
CASE CASE
-- If target is HTTP and has no payload format, add "Default"
WHEN target->>'type' = 'http' AND NOT target ? 'payload_format' WHEN target->>'type' = 'http' AND NOT target ? 'payload_format'
THEN target || '{"payload_format":"Default"}'::jsonb THEN target || '{"payload_format":"Default"}'::jsonb
ELSE target ELSE target
END END
) AS targets ) AS targets
FROM FROM targets_expanded GROUP BY id
targets_expanded
GROUP BY
id
) )
-- Update the original table with the updated targets UPDATE notification_policy
UPDATE SET targets = targets_updated.targets
notification_policy FROM targets_updated WHERE notification_policy.id = targets_updated.id;
SET
targets = targets_updated.targets
FROM
targets_updated
WHERE
notification_policy.id = targets_updated.id;
/* migrate the webhook job to execution and task as the webhook refactor since v2.8 */ /* migrate the webhook job to execution and task as the webhook refactor since v2.8 */
DO $$ DO $$