From b015a6645602cc7111a3a0d191c5dd9f57636fba Mon Sep 17 00:00:00 2001 From: Clark Winkelmann Date: Mon, 21 Nov 2022 14:27:44 +0100 Subject: [PATCH] Enable conditional migrations through `when` callback --- framework/core/src/Database/Migrator.php | 47 ++++++++++++------------ 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/framework/core/src/Database/Migrator.php b/framework/core/src/Database/Migrator.php index 58d25288d..a7b44ce4c 100644 --- a/framework/core/src/Database/Migrator.php +++ b/framework/core/src/Database/Migrator.php @@ -124,7 +124,22 @@ class Migrator */ protected function runUp($path, $file, Extension $extension = null) { - $this->resolveAndRunClosureMigration($path, $file); + $migration = $this->resolve($path, $file); + + // If a "when" callback in defined in a migration array, a falsy return value will cause the migration to skip + if (is_array($migration) && array_key_exists('when', $migration)) { + if (!call_user_func($migration['when'], $this->connection->getSchemaBuilder())) { + $this->note("Skipped: $file"); + + return; + } + } + + try { + $this->runClosureMigration($migration); + } catch (MigrationKeyMissing $exception) { + throw $exception->withFile("$path/$file.php"); + } // Once we have run a migrations class, we will log that it was run in this // repository so that we don't try to run it next time we do a migration @@ -165,13 +180,18 @@ class Migrator * * @param string $path * @param string $file - * @param string $path - * @param Extension $extension + * @param Extension|null $extension * @return void */ protected function runDown($path, $file, Extension $extension = null) { - $this->resolveAndRunClosureMigration($path, $file, 'down'); + $migration = $this->resolve($path, $file); + + try { + $this->runClosureMigration($migration, 'down'); + } catch (MigrationKeyMissing $exception) { + throw $exception->withFile("$path/$file.php"); + } // Once we have successfully run the migration "down" we will remove it from // the migration repository so it will be considered to have not been run @@ -197,25 +217,6 @@ class Migrator } } - /** - * Resolves and run a migration and assign the filename to the exception if needed. - * - * @param string $path - * @param string $file - * @param string $direction - * @throws MigrationKeyMissing - */ - protected function resolveAndRunClosureMigration(string $path, string $file, string $direction = 'up') - { - $migration = $this->resolve($path, $file); - - try { - $this->runClosureMigration($migration, $direction); - } catch (MigrationKeyMissing $exception) { - throw $exception->withFile("$path/$file.php"); - } - } - /** * Get all of the migration files in a given path. *