diff --git a/schema/mysql-upgrades/1.0.0.sql b/schema/mysql-upgrades/1.0.0.sql index d790dba..e01bc1b 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 start, DROP COLUMN frequency; diff --git a/schema/pgsql-upgrades/1.0.0.sql b/schema/pgsql-upgrades/1.0.0.sql index b0f6cfc..0bf3c35 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 start, DROP COLUMN frequency;