From e784c145de42b41e847bbf827e2f22c471460f9f Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Mon, 10 Jul 2023 15:31:41 +0200 Subject: [PATCH] Provide `Migration` hook --- library/Reporting/ProvidedHook/Migration.php | 64 ++++++++++++++++++++ run.php | 2 + 2 files changed, 66 insertions(+) create mode 100644 library/Reporting/ProvidedHook/Migration.php diff --git a/library/Reporting/ProvidedHook/Migration.php b/library/Reporting/ProvidedHook/Migration.php new file mode 100644 index 00000000..5cc62962 --- /dev/null +++ b/library/Reporting/ProvidedHook/Migration.php @@ -0,0 +1,64 @@ +registerCallback('1.0.0', [$this, 'migrateReportSchedules']); + } + + public function getDescription(): string + { + return $this->translate('We have deprecated the start and frequency column of the schedule table.'); + } + + public function migrateReportSchedules(): void + { + $conn = $this->getDB(); + $schedules = Model\Schedule::on($conn) + ->withColumns([ + 'frequency' => new Expression('frequency'), + 'start' => new Expression('start') + ]); + + $conn->transaction(function (Connection $conn) use ($schedules) { + foreach ($schedules as $schedule) { + $config = Json::decode($schedule->config, true); + if (isset($config['frequencyType'])) { + // It's already migrated + continue; + } + + $frequency = new Cron('@' . $schedule->frequency); + $frequency->startAt((new MillisecondTimestamp([]))->fromDb($schedule->start, 'start', null)); + + $config['frequencyType'] = get_php_type($frequency); + $config['frequency'] = Json::encode($frequency); + + $conn->update('schedule', ['config' => Json::encode($config)], ['id = ?' => $schedule->id]); + } + }); + } +} diff --git a/run.php b/run.php index ab6c4778..a93f2efa 100644 --- a/run.php +++ b/run.php @@ -8,6 +8,8 @@ /** @var \Icinga\Application\Modules\Module $this */ + $this->provideHook('migration', 'Migration'); + $this->provideHook('reporting/Report', '\\Icinga\\Module\\Reporting\\Reports\\SystemReport'); $this->provideHook('reporting/Action', '\\Icinga\\Module\\Reporting\\Actions\\SendMail');