#25: Updated the data migration framework to run all intermediate versions

This commit is contained in:
Andy Heathershaw 2017-04-19 09:12:36 +01:00
parent 6d11f1c0f0
commit 22d446d246
4 changed files with 44 additions and 7 deletions

View File

@ -4,5 +4,15 @@ namespace App;
abstract class DataMigration abstract class DataMigration
{ {
/**
* Gets the version number this data migration applies to.
* @return string
*/
abstract function getVersion();
/**
* Runs this data migration.
* @param string $currentVersion Current version being upgraded from.
*/
abstract function run($currentVersion); abstract function run($currentVersion);
} }

View File

@ -2,7 +2,6 @@
namespace App\Http\Middleware; namespace App\Http\Middleware;
use App\Configuration;
use App\DataMigration; use App\DataMigration;
use App\Facade\UserConfig; use App\Facade\UserConfig;
use App\Helpers\MiscHelper; use App\Helpers\MiscHelper;
@ -99,16 +98,39 @@ class AppInstallation
Artisan::call('migrate', ['--force' => true]); Artisan::call('migrate', ['--force' => true]);
Artisan::call('db:seed', ['--force' => true]); Artisan::call('db:seed', ['--force' => true]);
$className = sprintf('DataMigrationV%s', str_replace(['.', '-'], '_', $appVersionNumber)); // Run any data migrations relevant to the new version
if (class_exists($className)) $dataMigrationsFolder = join(DIRECTORY_SEPARATOR, [dirname(dirname(dirname(__DIR__))), 'database', 'data_migrations']);
$di = new \RecursiveDirectoryIterator($dataMigrationsFolder, \RecursiveDirectoryIterator::SKIP_DOTS);
$updateClasses = [];
// First load all data migration classes and examine the version number to decide if we need it or not
/** @var \SplFileInfo $fileInfo */
foreach ($di as $fileInfo)
{ {
if (!$di->isFile() || $di->getExtension() != 'php')
{
continue;
}
$className = substr($fileInfo->getFilename(), 0, strlen($fileInfo->getFilename()) - 4);
/** @var DataMigration $upgradeClass */ /** @var DataMigration $upgradeClass */
$upgradeClass = new $className; $upgradeClass = new $className;
if (version_compare($versionNumber->value, $upgradeClass->getVersion()) < 0)
{
$updateClasses[] = $upgradeClass;
}
}
// Now run the migrations
foreach ($updateClasses as $upgradeClass)
{
$upgradeClass->run($versionNumber); $upgradeClass->run($versionNumber);
} }
}
$versionNumber->value = $appVersionNumber; // Save the new version number
$versionNumber->save(); $versionNumber->value = $appVersionNumber;
$versionNumber->save();
}
} }
} }

View File

@ -2,7 +2,7 @@
return [ return [
// Version number of Blue Twilight // Version number of Blue Twilight
'version' => '2.0.0-alpha.2', 'version' => '2.0.0-beta.1',
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------

View File

@ -5,6 +5,11 @@ use App\DataMigration;
class DataMigrationV2_0_0_alpha_1 extends DataMigration class DataMigrationV2_0_0_alpha_1 extends DataMigration
{ {
public function getVersion()
{
return '2.0.0-alpha.1';
}
public function run($currentVersion) public function run($currentVersion)
{ {
$this->storeAlbumUrlPaths(); $this->storeAlbumUrlPaths();