Skip to content

Commit

Permalink
Provide Migration hook
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Sep 15, 2023
1 parent f9faac1 commit 3b0444b
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 9 deletions.
3 changes: 0 additions & 3 deletions library/Reporting/Model/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public function getColumns()
return [
'report_id',
'author',
'start',
'frequency',
'action',
'config',
'ctime',
Expand All @@ -38,7 +36,6 @@ public function getColumns()
public function createBehaviors(Behaviors $behaviors)
{
$behaviors->add(new MillisecondTimestamp([
'start',
'ctime',
'mtime'
]));
Expand Down
39 changes: 39 additions & 0 deletions library/Reporting/Model/Schema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/* Icinga Reporting | (c) 2023 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Reporting\Model;

use ipl\Orm\Behavior\BoolCast;
use ipl\Orm\Behavior\MillisecondTimestamp;
use ipl\Orm\Behaviors;
use ipl\Orm\Model;

class Schema extends Model
{
public function getTableName(): string
{
return 'reporting_schema';
}

public function getKeyName()
{
return 'id';
}

public function getColumns(): array
{
return [
'version',
'timestamp',
'success',
'reason'
];
}

public function createBehaviors(Behaviors $behaviors): void
{
$behaviors->add(new BoolCast(['success']));
$behaviors->add(new MillisecondTimestamp(['timestamp']));
}
}
83 changes: 83 additions & 0 deletions library/Reporting/ProvidedHook/DbMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/* Icinga Reporting | (c) 2023 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Reporting\ProvidedHook;

use Icinga\Application\Hook\DbMigrationHook;
use Icinga\Module\Reporting\Database;
use Icinga\Module\Reporting\Model\Schema;
use ipl\Orm\Query;
use ipl\Sql\Connection;

class DbMigration extends DbMigrationHook
{
use Database {
getDb as public getReportingDb;
}

public function getName(): string
{
return $this->translate('Icinga Reporting');
}

public function providedDescriptions(): array
{
return [
'0.9.1' => $this->translate(
'Modifies all columns that uses current_timestamp to unix_timestamp and alters the database'
. ' engine of some tables.'
),
'0.10.0' => $this->translate('Creates the template table and adjusts some column types'),
'1.0.0' => $this->translate('Migrates all your configured report schedules to the new config.')
];
}

protected function getSchemaQuery(): Query
{
return Schema::on($this->getDb());
}

public function getDb(): Connection
{
return $this->getReportingDb();
}

public function getVersion(): string
{
if ($this->version === null) {
$conn = $this->getDb();
$schema = $this->getSchemaQuery()
->columns(['version', 'success'])
->orderBy('id', SORT_DESC)
->limit(2);

if (static::tableExists($conn, $schema->getModel()->getTableName())) {
/** @var Schema $version */
foreach ($schema as $version) {
if ($version->success) {
$this->version = $version->version;
}
}

if (! $this->version) {
// Schema version table exist, but the user has probably deleted the entry!
$this->version = '1.0.0';
}
} elseif (static::tableExists($conn, 'template')) {
// We have added Postgres support and the template table with 0.10.0.
// So, use this as the last (migrated) version.
$this->version = '0.10.0';
} elseif (static::getColumnType($conn, 'timeframe', 'name') === 'varchar(128)') {
// Upgrade script 0.9.1 alters the timeframe.name column from `varchar(255)` -> `varchar(128)`.
// Therefore, we can safely use this as the last migrated version.
$this->version = '0.9.1';
} else {
// Use the initial version as the last migrated schema version!
$this->version = '0.9.0';
}
}

return $this->version;
}
}
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('DbMigration', '\\Icinga\\Module\\Reporting\\ProvidedHook\\DbMigration');

$this->provideHook('reporting/Report', '\\Icinga\\Module\\Reporting\\Reports\\SystemReport');

$this->provideHook('reporting/Action', '\\Icinga\\Module\\Reporting\\Actions\\SendMail');
Expand Down
17 changes: 17 additions & 0 deletions schema/mysql-upgrades/1.0.0.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
ALTER TABLE schedule
DROP COLUMN start,
DROP COLUMN frequency;

CREATE TABLE reporting_schema (
id int unsigned NOT NULL AUTO_INCREMENT,
version varchar(64) NOT NULL,
timestamp bigint NOT NULL,
success enum ('n', 'y') DEFAULT NULL,
reason text DEFAULT NULL,

PRIMARY KEY (id),
CONSTRAINT idx_reporting_schema_version UNIQUE (version)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=DYNAMIC;

INSERT INTO reporting_schema (version, timestamp, success, reason)
VALUES ('1.0.0', UNIX_TIMESTAMP() * 1000, 'y', NULL);
16 changes: 14 additions & 2 deletions schema/mysql.schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ CREATE TABLE schedule (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
report_id int(10) unsigned NOT NULL,
author varchar(255) NOT NULL COLLATE utf8mb4_unicode_ci,
start bigint(20) unsigned NOT NULL,
frequency enum('minutely', 'hourly', 'daily', 'weekly', 'monthly'),
action varchar(255) NOT NULL,
config text NULL DEFAULT NULL,
ctime bigint(20) unsigned NOT NULL,
Expand All @@ -84,6 +82,20 @@ CREATE TABLE schedule (
CONSTRAINT schedule_report FOREIGN KEY (report_id) REFERENCES report (id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

CREATE TABLE reporting_schema (
id int unsigned NOT NULL AUTO_INCREMENT,
version varchar(64) NOT NULL,
timestamp bigint NOT NULL,
success enum ('n', 'y') DEFAULT NULL,
reason text DEFAULT NULL,

PRIMARY KEY (id),
CONSTRAINT idx_reporting_schema_version UNIQUE (version)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=DYNAMIC;

INSERT INTO reporting_schema (version, timestamp, success)
VALUES ('1.0.0', UNIX_TIMESTAMP() * 1000, 'y');

-- CREATE TABLE share (
-- id int(10) unsigned NOT NULL AUTO_INCREMENT,
-- report_id int(10) unsigned NOT NULL,
Expand Down
19 changes: 19 additions & 0 deletions schema/pgsql-upgrades/1.0.0.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ALTER TABLE schedule
DROP COLUMN start,
DROP COLUMN frequency;

CREATE TYPE boolenum AS ENUM ('n', 'y');

CREATE TABLE reporting_schema (
id serial,
version varchar(64) NOT NULL,
timestamp bigint NOT NULL,
success boolenum DEFAULT NULL,
reason text DEFAULT NULL,

CONSTRAINT pk_reporting_schema PRIMARY KEY (id),
CONSTRAINT idx_reporting_schema_version UNIQUE (version)
);

INSERT INTO reporting_schema (version, timestamp, success, reason)
VALUES ('1.0.0', unix_timestamp() * 1000, 'y', NULL);
20 changes: 16 additions & 4 deletions schema/pgsql.schema.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
CREATE TYPE boolenum AS ENUM ('n', 'y');

CREATE OR REPLACE FUNCTION unix_timestamp(timestamp with time zone DEFAULT NOW()) RETURNS bigint
AS 'SELECT EXTRACT(EPOCH FROM $1)::bigint'
LANGUAGE SQL;

CREATE TYPE frequency AS ENUM ('minutely', 'hourly', 'daily', 'weekly', 'monthly');

CREATE TABLE template (
id serial PRIMARY KEY,
author varchar(255) NOT NULL,
Expand Down Expand Up @@ -73,11 +73,23 @@ CREATE TABLE schedule (
id serial PRIMARY KEY,
report_id int NOT NULL,
author varchar(255) NOT NULL,
start bigint NOT NULL,
frequency frequency,
action varchar(255) NOT NULL,
config text DEFAULT NULL,
ctime bigint NOT NULL DEFAULT unix_timestamp() * 1000,
mtime bigint NOT NULL DEFAULT unix_timestamp() * 1000,
CONSTRAINT schedule_report FOREIGN KEY (report_id) REFERENCES report (id) ON DELETE CASCADE ON UPDATE CASCADE
);

CREATE TABLE reporting_schema (
id serial,
version varchar(64) NOT NULL,
timestamp bigint NOT NULL,
success boolenum DEFAULT NULL,
reason text DEFAULT NULL,

CONSTRAINT pk_reporting_schema PRIMARY KEY (id),
CONSTRAINT idx_reporting_schema_version UNIQUE (version)
);

INSERT INTO reporting_schema (version, timestamp, success)
VALUES ('1.0.0', UNIX_TIMESTAMP() * 1000, 'y');

0 comments on commit 3b0444b

Please sign in to comment.