aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElmar Ludwig <elmar.ludwig@uni-osnabrueck.de>2023-08-18 09:59:48 +0000
committerJan-Hendrik Willms <tleilax+studip@gmail.com>2023-08-18 09:59:48 +0000
commit75cedc6df4797f2e2f90f06222ec5e5018fc0826 (patch)
tree4384ee0e9b0fc7a66f7feae544a472cb607971cc
parent3bee81d9cb87eda39843435a8ceea40454154731 (diff)
add a new API to count pending migrations, fixes #3069
Closes #3069 and #3022 Merge request studip/studip!2046
-rw-r--r--cli/Commands/Plugins/PluginInfo.php2
-rw-r--r--lib/classes/PluginAdministration.php2
-rw-r--r--lib/migrations/Migrator.php19
-rw-r--r--lib/seminar_open.php4
-rw-r--r--tests/unit/lib/classes/MigrationTest.php4
5 files changed, 24 insertions, 7 deletions
diff --git a/cli/Commands/Plugins/PluginInfo.php b/cli/Commands/Plugins/PluginInfo.php
index e1b00f4..9809b13 100644
--- a/cli/Commands/Plugins/PluginInfo.php
+++ b/cli/Commands/Plugins/PluginInfo.php
@@ -43,7 +43,7 @@ class PluginInfo extends AbstractPluginCommand
if (is_dir($plugindir . '/migrations')) {
$schemaVersion = new \DBSchemaVersion($plugin['name']);
$migrator = new \Migrator($plugindir . '/migrations', $schemaVersion);
- $plugin['pending_migrations'] = count($migrator->relevantMigrations(null));
+ $plugin['pending_migrations'] = $migrator->pendingMigrations();
$plugin['schema_version'] = $schemaVersion->get();
}
diff --git a/lib/classes/PluginAdministration.php b/lib/classes/PluginAdministration.php
index 763f67d..2732f58 100644
--- a/lib/classes/PluginAdministration.php
+++ b/lib/classes/PluginAdministration.php
@@ -364,7 +364,7 @@ class PluginAdministration
if (is_dir($plugindir . '/migrations')) {
$schema_version = new DBSchemaVersion($plugin['name']);
$migrator = new Migrator($plugindir . '/migrations', $schema_version);
- $info[$id]['pending_migrations'] = count($migrator->relevantMigrations(null));
+ $info[$id]['pending_migrations'] = $migrator->pendingMigrations();
$info[$id]['schema_version'] = $schema_version->get();
}
}
diff --git a/lib/migrations/Migrator.php b/lib/migrations/Migrator.php
index 07caa90..50c8f3f 100644
--- a/lib/migrations/Migrator.php
+++ b/lib/migrations/Migrator.php
@@ -470,6 +470,25 @@ class Migrator
}
/**
+ * Returns the number of available, but not yet applied migrations.
+ *
+ * @return int migration count
+ */
+ public function pendingMigrations()
+ {
+ $pending = 0;
+
+ foreach (array_keys($this->migrationClasses()) as $version) {
+ list($branch, $version) = $this->migrationBranchAndVersion($version);
+ if ($this->schema_version->get($branch) < $version) {
+ ++$pending;
+ }
+ }
+
+ return $pending;
+ }
+
+ /**
* Overridable method used to return a textual representation of what's going
* on in me. You can use me as you would use printf.
*
diff --git a/lib/seminar_open.php b/lib/seminar_open.php
index 33436d4..e79d39c 100644
--- a/lib/seminar_open.php
+++ b/lib/seminar_open.php
@@ -161,12 +161,10 @@ if (!Request::isXhr() && $perm->have_perm('root')) {
new DBSchemaVersion('studip')
);
- $migrations = $migrator->relevantMigrations(null);
-
$_SESSION['migration-check'] = [
'disabled' => $_SESSION['migration-check']['disabled'] ?? false,
'timestamp' => time(),
- 'count' => count($migrations),
+ 'count' => $migrator->pendingMigrations()
];
}
diff --git a/tests/unit/lib/classes/MigrationTest.php b/tests/unit/lib/classes/MigrationTest.php
index 0aabbcd..dd78ac7 100644
--- a/tests/unit/lib/classes/MigrationTest.php
+++ b/tests/unit/lib/classes/MigrationTest.php
@@ -95,7 +95,7 @@ class MigrationTest extends \Codeception\Test\Unit
$migrator = $this->getMigrator($schema_version);
$migrator->migrateTo(null);
$this->assertSame(10, $schema_version->get());
- $this->assertSame(0, count($migrator->relevantMigrations(null)));
+ $this->assertSame(0, $migrator->pendingMigrations());
return $schema_version;
}
@@ -108,7 +108,7 @@ class MigrationTest extends \Codeception\Test\Unit
$migrator = $this->getMigrator($schema_version);
$migrator->migrateTo(0);
$this->assertSame(0, $schema_version->get());
- $this->assertSame(4, count($migrator->relevantMigrations(null)));
+ $this->assertSame(4, $migrator->pendingMigrations());
}
public function testGaps()