-
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
11 changed files
with
322 additions
and
178 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
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
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
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,49 @@ | ||
<?php | ||
|
||
/* Icinga Reporting | (c) 2023 Icinga GmbH | GPLv2 */ | ||
|
||
namespace Icinga\Module\Reporting\Model; | ||
|
||
use DateTime; | ||
use ipl\Orm\Behavior\BoolCast; | ||
use ipl\Orm\Behavior\MillisecondTimestamp; | ||
use ipl\Orm\Behaviors; | ||
use ipl\Orm\Model; | ||
|
||
/** | ||
* A database model for reporting schema version table | ||
* | ||
* @property int $id Unique identifier of the database schema entries | ||
* @property string $version The current schema version of Icinga Web | ||
* @property DateTime $timestamp The insert/modify time of the schema entry | ||
* @property bool $success Whether the database migration of the current version was successful | ||
* @property ?string $reason The reason why the database migration has failed | ||
*/ | ||
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'])); | ||
} | ||
} |
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,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 private 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; | ||
} | ||
} |
Oops, something went wrong.