Skip to content
Merged
Next Next commit
feat(migrations): shouldRun method on migration classes.
  • Loading branch information
danmatthews committed Mar 13, 2025
commit 58af42806ae153bba60a28c4471b5d637f1303e0
10 changes: 10 additions & 0 deletions src/Illuminate/Database/Migrations/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@ public function getConnection()
{
return $this->connection;
}

/**
* Should this migration run or not.
*
* @return bool
*/
public function shouldRun(): bool
{
return true;
}
}
13 changes: 13 additions & 0 deletions src/Illuminate/Database/Migrations/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ public function run($paths = [], array $options = [])
return $migrations;
}

/**
* Determine if the migration should be ran.
*
* @param string $file
* @return bool
*/
public function shouldMigrationBeRan($file)
{
$instance = $this->resolvePath($file);
return $instance instanceof Migration ? $instance->shouldRun() : false;
}

/**
* Get the migration files that have not yet run.
*
Expand All @@ -155,6 +167,7 @@ protected function pendingMigrations($files, $ran)
->reject(fn ($file) => in_array($migrationName = $this->getMigrationName($file), $ran) ||
in_array($migrationName, $migrationsToSkip)
)
->filter(fn ($file) => $this->shouldMigrationBeRan($file))
->values()
->all();
}
Expand Down
1 change: 1 addition & 0 deletions tests/Database/DatabaseMigrationMigrateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function testBasicMigrationsCallMigratorWithProperArguments()
return $callback();
});
$migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
$migrator->shouldReceive('shouldMigrationBeRan')->andReturn(true);
$migrator->shouldReceive('run')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => false, 'step' => false]);
$migrator->shouldReceive('getNotes')->andReturn([]);
$migrator->shouldReceive('repositoryExists')->once()->andReturn(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateFlightsTable extends Migration
{

public function shouldRun(): bool
{
return false;
}

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('flights', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('flights');
}
}
Loading