Skip to content

Commit

Permalink
Provide Migration hook
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Aug 21, 2023
1 parent 6ab233d commit 0957c8a
Show file tree
Hide file tree
Showing 10 changed files with 165 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
35 changes: 35 additions & 0 deletions library/Reporting/Model/Schema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

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

namespace Icinga\Module\Reporting\Model;

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

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

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

public function getColumns()
{
return [
'version',
'timestamp',
];
}

public function createBehaviors(Behaviors $behaviors)
{
$behaviors->add(new MillisecondTimestamp(['timestamp']));
}
}
56 changes: 56 additions & 0 deletions library/Reporting/ProvidedHook/DBMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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;

class DBMigration extends DBMigrationHook
{
use Database;

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

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

public function getVersion(): string
{
$conn = $this->getDb();
$schema = Schema::on($conn)->columns('version');
if (static::tableExists($conn, $schema->getModel()->getTableName())) {
$schema = $schema->first();
if ($schema) {
return $schema->version;
}

// Schema version table exist, but the user has probably deleted the entry!
return '1.0.0';
}

if ($this->getBackendType() !== static::PGSQL_BACKEND) {
if (! static::tableExists($conn, 'template')) {
if (static::getColumnType($conn, 'timeframe', 'name') !== 'varchar(128)') {
// Use the initial version as the last migrated schema version!
return '0.9.0';
}

// 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.
return '0.9.1';
}
}

// We have added Postgres support and the template table with 0.10.0.
// So, use this as the last (migrated) version.
return '0.10.0';
}
}
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', '\\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
11 changes: 11 additions & 0 deletions schema/mysql-upgrades/0.10.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,14 @@ CREATE TABLE template (

ALTER TABLE report ADD COLUMN template_id int(10) unsigned NULL DEFAULT NULL AFTER timeframe_id;
ALTER TABLE report ADD CONSTRAINT report_template FOREIGN KEY (template_id) REFERENCES template (id);

CREATE TABLE IF NOT EXISTS reporting_schema (
id int unsigned NOT NULL AUTO_INCREMENT,
version varchar(64) NOT NULL,
timestamp int unsigned NOT NULL,

PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=DYNAMIC;

INSERT INTO reporting_schema (id, version, timestamp)
VALUES (1, '0.10.0', UNIX_TIMESTAMP()) ON DUPLICATE KEY UPDATE version = VALUES(version), timestamp = VALUES(timestamp);
11 changes: 11 additions & 0 deletions schema/mysql-upgrades/0.9.1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,14 @@ ALTER TABLE timeframe ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin

ALTER TABLE report MODIFY COLUMN name varchar(128) NOT NULL COLLATE utf8mb4_unicode_ci;
ALTER TABLE report ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=default;

CREATE TABLE IF NOT EXISTS reporting_schema (
id int unsigned NOT NULL AUTO_INCREMENT,
version varchar(64) NOT NULL,
timestamp int unsigned NOT NULL,

PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=DYNAMIC;

INSERT INTO reporting_schema (id, version, timestamp)
VALUES (1, '0.9.1', UNIX_TIMESTAMP());
14 changes: 14 additions & 0 deletions schema/mysql-upgrades/1.0.0.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ALTER TABLE schedule
DROP COLUMN IF EXISTS start,
DROP COLUMN IF EXISTS frequency;

CREATE TABLE IF NOT EXISTS reporting_schema (
id int unsigned NOT NULL AUTO_INCREMENT,
version varchar(64) NOT NULL,
timestamp int unsigned NOT NULL,

PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=DYNAMIC;

INSERT INTO reporting_schema (id, version, timestamp)
VALUES (1, '1.0.0', UNIX_TIMESTAMP()) ON DUPLICATE KEY UPDATE version = VALUES(version), timestamp = VALUES(timestamp);
13 changes: 11 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,17 @@ 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 IF NOT EXISTS reporting_schema (
id int unsigned NOT NULL AUTO_INCREMENT,
version varchar(64) NOT NULL,
timestamp int unsigned NOT NULL,

PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=DYNAMIC;

INSERT INTO reporting_schema (id, version, timestamp)
VALUES (1, '1.0.0', UNIX_TIMESTAMP()) ON DUPLICATE KEY UPDATE version = VALUES(version), timestamp = VALUES(timestamp);

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

CREATE TABLE reporting_schema (
id serial,
version varchar(64) NOT NULL,
timestamp int NOT NULL,

CONSTRAINT pk_reporting_schema PRIMARY KEY (id)
);

INSERT INTO reporting_schema (version, timestamp)
VALUES ('1.0.0', UNIX_TIMESTAMP());
15 changes: 11 additions & 4 deletions schema/pgsql.schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ CREATE OR REPLACE FUNCTION unix_timestamp(timestamp with time zone DEFAULT NOW()
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 +71,20 @@ 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 int NOT NULL,

CONSTRAINT pk_reporting_schema PRIMARY KEY (id)
);

INSERT INTO reporting_schema (version, timestamp)
VALUES ('1.0.0', UNIX_TIMESTAMP());

0 comments on commit 0957c8a

Please sign in to comment.