Skip to content

Commit

Permalink
Add migrate_schedule_config() procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
lippserd authored and yhabteab committed Aug 30, 2023
1 parent aaa2a80 commit 304e52c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
38 changes: 38 additions & 0 deletions schema/mysql-upgrades/1.0.0.sql
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
25 changes: 25 additions & 0 deletions schema/pgsql-upgrades/1.0.0.sql
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 304e52c

Please sign in to comment.