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

[11.x] Add the ability to skip migrations within tests #54441

Open
wants to merge 9 commits into
base: 11.x
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion src/Illuminate/Database/Events/NoPendingMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Illuminate\Database\Events;

class NoPendingMigrations
use Illuminate\Contracts\Database\Events\MigrationEvent;

class NoPendingMigrations implements MigrationEvent
{
/**
* The migration method that was called.
Expand Down
34 changes: 33 additions & 1 deletion src/Illuminate/Database/Migrations/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ class Migrator
*/
protected static $connectionResolverCallback;

/**
* The pending migrations to skip.
*
* @var list<string>
*/
protected static $withoutMigrations = [];

/**
* The name of the default connection.
*
Expand Down Expand Up @@ -142,12 +149,26 @@ public function run($paths = [], array $options = [])
*/
protected function pendingMigrations($files, $ran)
{
$migrationsToSkip = $this->migrationsToSkip();

return (new Collection($files))
->reject(fn ($file) => in_array($this->getMigrationName($file), $ran))
->reject(fn ($file) => in_array($migrationName = $this->getMigrationName($file), $ran) || in_array($migrationName, $migrationsToSkip))
->values()
->all();
}

/**
* Get list of pending migrations to skip.
*
* @return list<string>
*/
protected function migrationsToSkip()
{
return (new Collection(self::$withoutMigrations))
->map($this->getMigrationName(...))
->all();
}

/**
* Run an array of migrations.
*
Expand Down Expand Up @@ -671,6 +692,17 @@ public static function resolveConnectionsUsing(Closure $callback)
static::$connectionResolverCallback = $callback;
}

/**
* Set the pending migrations to skip.
*
* @param list<string> $migrations
* @return void
*/
public static function withoutMigrations($migrations)
{
static::$withoutMigrations = $migrations;
}

/**
* Get the schema grammar out of a migration connection.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Console\Application as Artisan;
use Illuminate\Cookie\Middleware\EncryptCookies;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Foundation\Bootstrap\HandleExceptions;
use Illuminate\Foundation\Bootstrap\RegisterProviders;
use Illuminate\Foundation\Console\AboutCommand;
Expand Down Expand Up @@ -170,6 +171,7 @@ protected function tearDownTheTestEnvironment(): void
ConvertEmptyStringsToNull::flushState();
EncryptCookies::flushState();
HandleExceptions::flushState();
Migrator::withoutMigrations([]);
Once::flush();
PreventRequestsDuringMaintenance::flushState();
Queue::createPayloadUsing(null);
Expand Down
25 changes: 25 additions & 0 deletions tests/Integration/Migration/MigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Integration\Migration;

use Illuminate\Database\Migrations\Migrator;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
Expand Down Expand Up @@ -29,6 +30,12 @@ protected function setUp(): void
$this->subject->getRepository()->createRepository();
}

protected function tearDown(): void
{
parent::tearDown();
Migrator::withoutMigrations([]);
}

public function testMigrate()
{
$this->expectInfo('Running migrations.');
Expand Down Expand Up @@ -58,6 +65,24 @@ public function testMigrateWithoutOutput()
$this->assertTrue(DB::getSchemaBuilder()->hasColumn('people', 'last_name'));
}

public function testWithSkippedMigrations()
{
$this->app->forgetInstance('migrator');
$this->subject = $this->app->make('migrator');

Migrator::withoutMigrations(['2015_10_04_000000_modify_people_table.php', '2016_10_04_000000_modify_people_table']);

$this->subject->run([__DIR__.'/fixtures']);
$this->assertTrue(DB::getSchemaBuilder()->hasTable('people'));
$this->assertFalse(DB::getSchemaBuilder()->hasColumn('people', 'first_name'));
$this->assertFalse(DB::getSchemaBuilder()->hasColumn('people', 'last_name'));

Migrator::withoutMigrations([]);
$this->subject->run([__DIR__.'/fixtures']);
$this->assertTrue(DB::getSchemaBuilder()->hasColumn('people', 'first_name'));
$this->assertTrue(DB::getSchemaBuilder()->hasColumn('people', 'last_name'));
}

public function testRollback()
{
$this->getConnection()->statement('CREATE TABLE people(id INT, first_name VARCHAR, last_name VARCHAR);');
Expand Down