Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable conditional migrations through when callback #3689

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions framework/core/src/Database/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,22 @@ public function runMigrationList($path, $migrations, Extension $extension = null
*/
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("<info>Skipped:</info> $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
Expand Down Expand Up @@ -165,13 +180,18 @@ public function reset($path, Extension $extension = null)
*
* @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
Expand All @@ -197,25 +217,6 @@ protected function runClosureMigration($migration, $direction = 'up')
}
}

/**
* 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.
*
Expand Down