From 2944ceaa52e3a05d8ff070fa4defcb400429b3fd Mon Sep 17 00:00:00 2001
From: Yonas Habteab <yonas.habteab@icinga.com>
Date: Tue, 12 Sep 2023 16:57:53 +0200
Subject: [PATCH] Rename `getSchemaQueryFor()` & drop `$version` param

---
 .../Icinga/Application/Hook/MigrationHook.php | 19 +++++++++++--------
 .../Application/ProvidedHook/DbMigration.php  |  8 +++-----
 2 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/library/Icinga/Application/Hook/MigrationHook.php b/library/Icinga/Application/Hook/MigrationHook.php
index bbbc529cc..9e31f3421 100644
--- a/library/Icinga/Application/Hook/MigrationHook.php
+++ b/library/Icinga/Application/Hook/MigrationHook.php
@@ -191,9 +191,12 @@ abstract class MigrationHook implements Countable
                 );
                 Logger::debug($e->getTraceAsString());
 
+                $schemaQuery = $this->getSchemaQuery()
+                    ->filter(Filter::equal('version', $migration->getVersion()));
+
                 static::insertFailedEntry(
                     $conn,
-                    $this->getSchemaQueryFor($migration->getVersion()),
+                    $schemaQuery,
                     $migration->getVersion(),
                     $e->getMessage() . PHP_EOL . $e->getTraceAsString()
                 );
@@ -247,13 +250,11 @@ abstract class MigrationHook implements Countable
     abstract protected function getDb(): Connection;
 
     /**
-     * Get a schema version query filtered by the given $version
-     *
-     * @param string $version
+     * Get a schema version query
      *
      * @return Query
      */
-    abstract protected function getSchemaQueryFor(string $version): Query;
+    abstract protected function getSchemaQuery(): Query;
 
     protected function load(): void
     {
@@ -361,12 +362,14 @@ abstract class MigrationHook implements Countable
         /** @var array<string, string> $states */
         $states = $session->get($this->getModuleName(), []);
         if (! isset($states[$version])) {
-            $schemaQuery = $this->getSchemaQueryFor($version);
-            $schemaQuery->setFilter(Filter::all(Filter::equal('success', 'n')));
+            $schemaQuery = $this->getSchemaQuery()
+                ->filter(Filter::equal('version', $version))
+                ->filter(Filter::all(Filter::equal('success', 'n')));
+
             if (static::getColumnType($this->getDb(), $schemaQuery->getModel()->getTableName(), 'reason')) {
                 /** @var Schema $schema */
                 $schema = $schemaQuery->first();
-                if ($schema && version_compare($schema->version, $version, '==')) {
+                if ($schema) {
                     return $schema->reason;
                 }
             }
diff --git a/library/Icinga/Application/ProvidedHook/DbMigration.php b/library/Icinga/Application/ProvidedHook/DbMigration.php
index 20054b790..527b910c1 100644
--- a/library/Icinga/Application/ProvidedHook/DbMigration.php
+++ b/library/Icinga/Application/ProvidedHook/DbMigration.php
@@ -8,7 +8,6 @@ use Icinga\Application\Hook\MigrationHook;
 use Icinga\Common\Database;
 use Icinga\Model\Schema;
 use ipl\Orm\Query;
-use ipl\Stdlib\Filter;
 
 class DbMigration extends MigrationHook
 {
@@ -28,7 +27,7 @@ class DbMigration extends MigrationHook
     {
         if ($this->version === null) {
             $conn = $this->getDb();
-            $schemaQuery = Schema::on($conn)
+            $schemaQuery = $this->getSchemaQuery()
                 ->orderBy('id', SORT_DESC)
                 ->limit(2);
 
@@ -65,9 +64,8 @@ class DbMigration extends MigrationHook
         return $this->version;
     }
 
-    protected function getSchemaQueryFor(string $version): Query
+    protected function getSchemaQuery(): Query
     {
-        return Schema::on($this->getDb())
-            ->filter(Filter::equal('version', $version));
+        return Schema::on($this->getDb());
     }
 }