Skip to content
Snippets Groups Projects
Commit 75cedc6d authored by Elmar Ludwig's avatar Elmar Ludwig Committed by Jan-Hendrik Willms
Browse files

add a new API to count pending migrations, fixes #3069

Closes #3069 and #3022

Merge request studip/studip!2046
parent 3bee81d9
No related branches found
No related tags found
No related merge requests found
......@@ -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();
}
......
......@@ -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();
}
}
......
......@@ -469,6 +469,25 @@ class Migrator
return $all_branches ? $versions : $versions[$this->schema_version->getBranch()];
}
/**
* 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.
......
......@@ -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()
];
}
......
......@@ -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()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment