-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/* Icinga Reporting | (c) 2023 Icinga GmbH | GPLv2 */ | ||
|
||
namespace Icinga\Module\Reporting\ProvidedHook; | ||
|
||
use Icinga\Application\Hook\MigrationHook; | ||
use Icinga\Module\Reporting\Database; | ||
use Icinga\Module\Reporting\Model; | ||
use Icinga\Util\Json; | ||
use ipl\Orm\Behavior\MillisecondTimestamp; | ||
use ipl\Scheduler\Cron; | ||
use ipl\Sql\Connection; | ||
use ipl\Sql\Expression; | ||
|
||
use function ipl\Stdlib\get_php_type; | ||
|
||
class Migration extends MigrationHook | ||
{ | ||
use Database; | ||
|
||
protected const INITIAL_MIGRATE_VERSION = '0.10.0'; | ||
|
||
protected function init(): void | ||
{ | ||
parent::init(); | ||
|
||
// Migrate the `start` and `frequency` column of the schedule table to the config json | ||
$this->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]); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters