From 78166e8e60e495a7a64eb7a05cc9a3ca46528c25 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Tue, 8 Aug 2023 13:07:09 +0200 Subject: [PATCH] Add `migrate_schedule_config()` procedure --- schema/mysql-upgrades/1.0.0.sql | 38 +++++++++++++++++++++++++++++++++ schema/pgsql-upgrades/1.0.0.sql | 25 ++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/schema/mysql-upgrades/1.0.0.sql b/schema/mysql-upgrades/1.0.0.sql index e75156e..57aeef9 100644 --- a/schema/mysql-upgrades/1.0.0.sql +++ b/schema/mysql-upgrades/1.0.0.sql @@ -1,3 +1,41 @@ +DROP PROCEDURE IF EXISTS migrate_schedule_config; +DELIMITER // +CREATE PROCEDURE migrate_schedule_config() +BEGIN + DECLARE schedule_id int; + DECLARE schedule_start bigint; + DECLARE schedule_frequency enum('minutely', 'hourly', 'daily', 'weekly', 'monthly'); + DECLARE schedule_config text; + + DECLARE frequency_json text; + + DECLARE done int DEFAULT 0; + DECLARE schedule CURSOR FOR SELECT id, start, frequency, config FROM schedule; + DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; + + OPEN schedule; + read_loop: LOOP + FETCH schedule INTO schedule_id, schedule_start, schedule_frequency, schedule_config; + IF done THEN + LEAVE read_loop; + END IF; + IF NOT INSTR(schedule_config, 'frequencyType') THEN + SET frequency_json = CONCAT( + ',"frequencyType":"\\\\ipl\\\\Scheduler\\\\Cron","frequency":"{', + '\\"expression\\":\\"@', schedule_frequency, + '\\",\\"start\\":\\"', DATE_FORMAT(CONVERT_TZ(FROM_UNIXTIME(schedule_start / 1000), @@session.TIME_ZONE, 'UTC'), '%Y-%m-%dT%H:%i:%s.%u UTC'), + '\\"}"' + ); + UPDATE schedule SET config = INSERT(schedule_config, LENGTH(schedule_config), 0, frequency_json) WHERE id = schedule_id; + END IF; + END LOOP; + CLOSE schedule; +END // +DELIMITER ; + +CALL migrate_schedule_config(); +DROP PROCEDURE migrate_schedule_config; + ALTER TABLE schedule DROP COLUMN IF EXISTS start, DROP COLUMN IF EXISTS frequency; diff --git a/schema/pgsql-upgrades/1.0.0.sql b/schema/pgsql-upgrades/1.0.0.sql index a31ec0f..d516e9f 100644 --- a/schema/pgsql-upgrades/1.0.0.sql +++ b/schema/pgsql-upgrades/1.0.0.sql @@ -1,3 +1,28 @@ +CREATE OR REPLACE PROCEDURE migrate_schedule_config() + LANGUAGE plpgsql + AS $$ + DECLARE + row record; + frequency_json text; + BEGIN + FOR row IN (SELECT id, start, frequency, config FROM schedule) + LOOP + IF NOT CAST(POSITION('frequencyType' IN row.config) AS bool) THEN + frequency_json = CONCAT( + ',"frequencyType":"\\ipl\\Scheduler\\Cron","frequency":"{', + '\"expression\":\"@', row.frequency, + '\",\"start\":\"', TO_CHAR(TO_TIMESTAMP(row.start / 1000) AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US UTC'), + '\"}"' + ); + UPDATE schedule SET config = OVERLAY(row.config PLACING frequency_json FROM LENGTH(row.config) FOR 0) WHERE id = row.id; + END IF; + END LOOP; + END; + $$; + +CALL migrate_schedule_config(); +DROP PROCEDURE migrate_schedule_config; + ALTER TABLE schedule DROP COLUMN IF EXISTS start, DROP COLUMN IF EXISTS frequency;