Skip to content

Instantly share code, notes, and snippets.

@rohitkhatri
Last active December 12, 2022 23:10
Show Gist options
  • Save rohitkhatri/a3b1237925b609bf7070f924d37b9b92 to your computer and use it in GitHub Desktop.
Save rohitkhatri/a3b1237925b609bf7070f924d37b9b92 to your computer and use it in GitHub Desktop.
Migrate specific migrations with this command
<?php
/* Instructions:
* 1. Add this class file to your app/Console folder
* 2. Register command in app/Console/Kernel by adding `\App\Console\Commands\MigrateSpecificCommand::class` it to commands array
* 3. Done, now you can access it like: php artisan migrate:specific 2014_10_12_000000_create_users_table,2014_10_12_100000_create_password_resets_table
*/
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class MigrateSpecificCommand extends Command
{
protected $signature = 'migrate:specific {migration}';
protected $description = 'Migrate specific migration';
public function handle()
{
$migrations = explode(',', $this->argument('migration'));
if(!Schema::hasTable('migrations')) {
Artisan::call('migrate:install');
}
$batch = \Illuminate\Support\Facades\DB::table('migrations')->max('batch');
$batch = is_null($batch) ? 1 : $batch + 1;
$insert = [];
foreach($migrations as $migration) {
$migration_path = database_path('migrations/'.$migration.'.php');
if(file_exists($migration_path)) {
include_once $migration_path;
$class = studly_case(last(explode('_', $migration, 5)));
if(class_exists($class)) {
(new $class())->up();
$insert[] = ['migration'=>$migration,'batch'=>$batch];
}
}
}
DB::table('migrations')->insert($insert);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment