Skip to content

Instantly share code, notes, and snippets.

@miadz
Forked from rohitkhatri/MigrateSpecificCommand.php
Created October 27, 2017 14:24
Show Gist options
  • Save miadz/4a75ea38239cde5de3dc165beb056824 to your computer and use it in GitHub Desktop.
Save miadz/4a75ea38239cde5de3dc165beb056824 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);
}
}
@Faks
Copy link

Faks commented Sep 22, 2018

thanks it's awesome i tested it on Laravel 5.5 works just like it should 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment