diff --git a/app/DataMigration.php b/app/DataMigration.php index 07178c1..96e8390 100644 --- a/app/DataMigration.php +++ b/app/DataMigration.php @@ -4,5 +4,15 @@ namespace App; 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); } \ No newline at end of file diff --git a/app/Http/Middleware/AppInstallation.php b/app/Http/Middleware/AppInstallation.php index cffb8df..fba5ab0 100644 --- a/app/Http/Middleware/AppInstallation.php +++ b/app/Http/Middleware/AppInstallation.php @@ -2,7 +2,6 @@ namespace App\Http\Middleware; -use App\Configuration; use App\DataMigration; use App\Facade\UserConfig; use App\Helpers\MiscHelper; @@ -99,16 +98,39 @@ class AppInstallation Artisan::call('migrate', ['--force' => true]); Artisan::call('db:seed', ['--force' => true]); - $className = sprintf('DataMigrationV%s', str_replace(['.', '-'], '_', $appVersionNumber)); - if (class_exists($className)) + // Run any data migrations relevant to the new version + $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 */ $upgradeClass = new $className; + if (version_compare($versionNumber->value, $upgradeClass->getVersion()) < 0) + { + $updateClasses[] = $upgradeClass; + } + } + + // Now run the migrations + foreach ($updateClasses as $upgradeClass) + { $upgradeClass->run($versionNumber); } - } - $versionNumber->value = $appVersionNumber; - $versionNumber->save(); + // Save the new version number + $versionNumber->value = $appVersionNumber; + $versionNumber->save(); + } } } \ No newline at end of file diff --git a/config/app.php b/config/app.php index ea40861..f9a3938 100644 --- a/config/app.php +++ b/config/app.php @@ -2,7 +2,7 @@ return [ // Version number of Blue Twilight - 'version' => '2.0.0-alpha.2', + 'version' => '2.0.0-beta.1', /* |-------------------------------------------------------------------------- diff --git a/database/data_migrations/DataMigrationV2_0_0_alpha_1.php b/database/data_migrations/DataMigrationV2_0_0_alpha_1.php index b8ab079..6458c44 100644 --- a/database/data_migrations/DataMigrationV2_0_0_alpha_1.php +++ b/database/data_migrations/DataMigrationV2_0_0_alpha_1.php @@ -5,6 +5,11 @@ use App\DataMigration; class DataMigrationV2_0_0_alpha_1 extends DataMigration { + public function getVersion() + { + return '2.0.0-alpha.1'; + } + public function run($currentVersion) { $this->storeAlbumUrlPaths();