|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Gilbitron\Laravel\Console\Commands; |
| 4 | + |
| 5 | +use Illuminate\Database\Console\Migrations\BaseCommand; |
| 6 | +use Illuminate\Support\Collection; |
| 7 | + |
| 8 | +class MigrateToBranch extends BaseCommand |
| 9 | +{ |
| 10 | + /** |
| 11 | + * The name and signature of the console command. |
| 12 | + * |
| 13 | + * @var string |
| 14 | + */ |
| 15 | + protected $signature = 'migrate:to-branch {branch : The name of the branch} |
| 16 | + {--database= : The database connection to use} |
| 17 | + {--path= : The path of migrations files to be executed} |
| 18 | + {--dry-run : Output the migrations that need rolled back}'; |
| 19 | + |
| 20 | + /** |
| 21 | + * The console command description. |
| 22 | + * |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + protected $description = 'Rollback migrations before switching to a given branch'; |
| 26 | + |
| 27 | + /** |
| 28 | + * The migrator instance. |
| 29 | + * |
| 30 | + * @var \Illuminate\Database\Migrations\Migrator |
| 31 | + */ |
| 32 | + protected $migrator; |
| 33 | + |
| 34 | + /** |
| 35 | + * Create a new command instance. |
| 36 | + */ |
| 37 | + public function __construct() |
| 38 | + { |
| 39 | + parent::__construct(); |
| 40 | + |
| 41 | + // For some reason dependency injecting this class doesn't work |
| 42 | + $this->migrator = app('migrator'); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Execute the console command. |
| 47 | + * |
| 48 | + * @return mixed |
| 49 | + */ |
| 50 | + public function handle() |
| 51 | + { |
| 52 | + $destBranch = $this->argument('branch'); |
| 53 | + $currentBranch = trim(shell_exec('git rev-parse --abbrev-ref HEAD')); |
| 54 | + |
| 55 | + if ($destBranch == $currentBranch) { |
| 56 | + $this->error('Already on branch ' . $destBranch); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + $rollbackMigrations = $this->getMigrationsToRollBack($currentBranch, $destBranch); |
| 61 | + $ranMigrations = $this->getRanMigrations(); |
| 62 | + |
| 63 | + $steps = $rollbackMigrations->reject(function ($migration) use ($ranMigrations) { |
| 64 | + return !in_array($migration, $ranMigrations->toArray()); |
| 65 | + }); |
| 66 | + |
| 67 | + if ($steps->count()) { |
| 68 | + if ($this->option('dry-run')) { |
| 69 | + $this->info('Rollback required! ' . $steps->count() . ' migrations need rolled back:'); |
| 70 | + |
| 71 | + $steps->each(function ($item) { |
| 72 | + $this->line(' - ' . $item); |
| 73 | + }); |
| 74 | + |
| 75 | + $this->info('Run the following command: php artisan migrate:rollback --step=' . $steps->count()); |
| 76 | + } else { |
| 77 | + $this->call('migrate:rollback', [ |
| 78 | + '--step' => $steps->count(), |
| 79 | + ]); |
| 80 | + } |
| 81 | + } else { |
| 82 | + $this->info('No migrations need rolled back'); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Get migrations to roll back |
| 88 | + * |
| 89 | + * @param string $currentBranch |
| 90 | + * @param string $destBranch |
| 91 | + * @return Collection |
| 92 | + */ |
| 93 | + protected function getMigrationsToRollBack($currentBranch, $destBranch) |
| 94 | + { |
| 95 | + $command = 'cd ' . base_path() . ' && git diff --name-status '; |
| 96 | + $command .= $currentBranch . '..' . $destBranch; |
| 97 | + $command .= ' -- database/migrations'; |
| 98 | + |
| 99 | + /* |
| 100 | + * Format: |
| 101 | + * A database/migrations/2017_06_02_105859_example1_migration.php |
| 102 | + * D database/migrations/2017_06_02_105859_example2_migration.php |
| 103 | + */ |
| 104 | + $output = trim(shell_exec($command)); |
| 105 | + $migrations = explode("\n", $output); |
| 106 | + |
| 107 | + return collect($migrations)->reject(function ($migration) { |
| 108 | + // We only need migrations that don't exist in the dest branch |
| 109 | + return !starts_with($migration, 'D'); |
| 110 | + })->map(function ($migration) { |
| 111 | + return basename($migration, '.php'); |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Get migrations that have been run |
| 117 | + * |
| 118 | + * @return Collection |
| 119 | + */ |
| 120 | + protected function getRanMigrations() |
| 121 | + { |
| 122 | + $this->migrator->setConnection($this->option('database')); |
| 123 | + |
| 124 | + if (!$this->migrator->repositoryExists()) { |
| 125 | + return collect(); |
| 126 | + } |
| 127 | + |
| 128 | + $ran = $this->migrator->getRepository()->getRan(); |
| 129 | + |
| 130 | + return collect($this->getAllMigrationFiles())->reject(function ($migration) use ($ran) { |
| 131 | + $migrationName = $this->migrator->getMigrationName($migration); |
| 132 | + |
| 133 | + return !in_array($migrationName, $ran); |
| 134 | + })->keys(); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Get an array of all of the migration files. |
| 139 | + * |
| 140 | + * @return array |
| 141 | + */ |
| 142 | + protected function getAllMigrationFiles() |
| 143 | + { |
| 144 | + return $this->migrator->getMigrationFiles($this->getMigrationPaths()); |
| 145 | + } |
| 146 | +} |
0 commit comments