DbMigrationHook: Adjust regex pattern & add missing argument docs

This commit is contained in:
Yonas Habteab 2023-09-15 11:27:43 +02:00 committed by Johannes Meyer
parent 96a6321569
commit dc738ec4ce

View File

@ -14,16 +14,13 @@ use Icinga\Application\Icinga;
use Icinga\Application\Logger; use Icinga\Application\Logger;
use Icinga\Application\Modules\Module; use Icinga\Application\Modules\Module;
use Icinga\Model\Schema; use Icinga\Model\Schema;
use Icinga\Module\Setup\Utils\DbTool;
use Icinga\Web\Session; use Icinga\Web\Session;
use ipl\I18n\Translation; use ipl\I18n\Translation;
use ipl\Orm\Query; use ipl\Orm\Query;
use ipl\Sql\Adapter\Pgsql; use ipl\Sql\Adapter\Pgsql;
use ipl\Sql\Connection; use ipl\Sql\Connection;
use ipl\Stdlib\Filter; use ipl\Stdlib\Filter;
use ipl\Stdlib\Str;
use PDO; use PDO;
use PDOException;
use SplFileInfo; use SplFileInfo;
use stdClass; use stdClass;
@ -174,6 +171,9 @@ abstract class DbMigrationHook implements Countable
/** /**
* Apply all pending migrations of this hook * Apply all pending migrations of this hook
* *
* @param ?Connection $conn Use the provided database connection to apply the migrations.
* Is only used to elevate database users with insufficient privileges.
*
* @return bool Whether the migration(s) have been successfully applied * @return bool Whether the migration(s) have been successfully applied
*/ */
final public function run(Connection $conn = null): bool final public function run(Connection $conn = null): bool
@ -205,12 +205,8 @@ abstract class DbMigrationHook implements Countable
); );
Logger::debug($e->getTraceAsString()); Logger::debug($e->getTraceAsString());
$schemaQuery = $this->getSchemaQuery()
->filter(Filter::equal('version', $migration->getVersion()));
static::insertFailedEntry( static::insertFailedEntry(
$conn, $conn,
$schemaQuery,
$migration->getVersion(), $migration->getVersion(),
$e->getMessage() . PHP_EOL . $e->getTraceAsString() $e->getMessage() . PHP_EOL . $e->getTraceAsString()
); );
@ -280,18 +276,19 @@ abstract class DbMigrationHook implements Countable
$version = $this->getVersion(); $version = $this->getVersion();
/** @var SplFileInfo $file */ /** @var SplFileInfo $file */
foreach (new DirectoryIterator($path . DIRECTORY_SEPARATOR . $upgradeDir) as $file) { foreach (new DirectoryIterator($path . DIRECTORY_SEPARATOR . $upgradeDir) as $file) {
if (preg_match('/^(?:r|v)?((?:\d+\.){0,2}\d+)(?:_([\w+]+))?\.sql$/', $file->getFilename(), $m)) { if (preg_match('/^(v)?([^_]+)(?:_(\w+))?\.sql$/', $file->getFilename(), $m, PREG_UNMATCHED_AS_NULL)) {
if (version_compare($m[1], $version, '>')) { [$_, $_, $migrateVersion, $description] = $m;
$migration = new DbMigrationStep($m[1], $file->getRealPath()); if ($migrateVersion && version_compare($migrateVersion, $version, '>')) {
if (isset($descriptions[$migration->getVersion()])) { $migration = new DbMigrationStep($migrateVersion, $file->getRealPath());
$migration->setDescription($descriptions[$migration->getVersion()]); if (isset($descriptions[$migrateVersion])) {
} elseif (isset($m[2])) { $migration->setDescription($descriptions[$migrateVersion]);
$migration->setDescription(str_replace('_', ' ', $m[2])); } elseif ($description) {
$migration->setDescription(str_replace('_', ' ', $description));
} }
$migration->setLastState($this->loadLastState($migration->getVersion())); $migration->setLastState($this->loadLastState($migrateVersion));
$this->migrations[$m[1]] = $migration; $this->migrations[$migrateVersion] = $migration;
} }
} }
} }
@ -308,14 +305,16 @@ abstract class DbMigrationHook implements Countable
* Insert failed migration entry into the database or to the session * Insert failed migration entry into the database or to the session
* *
* @param Connection $conn * @param Connection $conn
* @param Query $schemaQuery
* @param string $version * @param string $version
* @param string $reason * @param string $reason
* *
* @return $this * @return $this
*/ */
protected function insertFailedEntry(Connection $conn, Query $schemaQuery, string $version, string $reason): self protected function insertFailedEntry(Connection $conn, string $version, string $reason): self
{ {
$schemaQuery = $this->getSchemaQuery()
->filter(Filter::equal('version', $version));
if (! static::getColumnType($conn, $schemaQuery->getModel()->getTableName(), 'success')) { if (! static::getColumnType($conn, $schemaQuery->getModel()->getTableName(), 'success')) {
$this->storeState($version, $reason); $this->storeState($version, $reason);
} else { } else {