This package is deprecated in favor of https://github.com/mll-lab/laravel-utils/releases/tag/v4.0.0 and will no longer be updated.
Run migrations only if a condition is true
Based on https://github.com/onlinepets/laravel-conditional-migrations
Via composer:
composer require mll-lab/laravel-conditional-migrations
To run a migration conditionally, implement the ConditionalMigration
interface and its ->shouldRun()
method:
use MLL\ConditionalMigrations\Contracts\ConditionalMigration;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Carbon;
class DoSomethingVeryIntensive extends Migration implements ConditionalMigration
{
public function up() { ... }
public function down() { ... }
public function shouldRun(): bool
{
return (new Carbon('1 AM'))->lessThan(now())
&& (new Carbon('2 AM'))->greaterThan(now());
}
}
The code snippet above will make sure the do_something_very_intensive
migration
will be skipped unless it is executed between 1 AM and 2 AM. This can be useful
if your migration does something that should not be run during the daytime, like
adding an index to a table containing lots of data.
You can optionally publish the configuration file:
php artisan vendor:publish --tags=conditional-migrations-config
This will create the file config/conditional-migrations.php
.
The always_run
option allows you to overrule the conditions set in individual migrations.
'always_run' => env('APP_DEBUG', false),
You can also use a closure if you want to do more advanced calculations:
'always_run' => function (): bool {
// calculate if migrations should always run
},
All notable changes to this project are documented in CHANGELOG.md
.
Contributions are welcome, see CONTRIBUTING.md.
See LICENSE.md.