Skip to content

Commit

Permalink
Provide Migration hook
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Jul 11, 2023
1 parent 7ac266e commit e784c14
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
64 changes: 64 additions & 0 deletions library/Reporting/ProvidedHook/Migration.php
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]);
}
});
}
}
2 changes: 2 additions & 0 deletions run.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit e784c14

Please sign in to comment.