Skip to content

Instantly share code, notes, and snippets.

@tacone
Created July 19, 2015 13:53
Show Gist options
  • Save tacone/23be1e80dd8abf9f2401 to your computer and use it in GitHub Desktop.
Save tacone/23be1e80dd8abf9f2401 to your computer and use it in GitHub Desktop.
How to mark Laravel 4 migrations as done without executing them
<?php
app()->bindShared('command.migrate.done', function($app)
{
$packagePath = $app['path.base'].'/vendor';
return new MigrateDone($app['migrator'], $packagePath);
});
Artisan::resolve('command.migrate.done');
<?php
use Illuminate\Database\Console\Migrations\BaseCommand;
use Illuminate\Console\ConfirmableTrait;
use Illuminate\Database\Migrations\Migrator;
use Symfony\Component\Console\Input\InputOption;
class MigrateDone extends BaseCommand
{
use ConfirmableTrait;
/**
* The console command name.
*
* @var string
*/
protected $name = 'migrate:done';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Mark all migrations as done';
/**
* The migrator instance.
*
* @var \Illuminate\Database\Migrations\Migrator
*/
protected $migrator;
/**
* The path to the packages directory (vendor).
*/
protected $packagePath;
/**
* Create a new migration command instance.
*
* @param \Illuminate\Database\Migrations\Migrator $migrator
* @param string $packagePath
* @return void
*/
public function __construct(Migrator $migrator, $packagePath)
{
parent::__construct();
$this->migrator = $migrator;
$this->packagePath = $packagePath;
}
/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
if (!$this->confirmToProceed()) {
return;
}
$this->prepareDatabase();
$path = $this->getMigrationPath();
$this->notes = array();
$files = $this->migrator->getMigrationFiles($path);
// Once we grab all of the migration files for the path, we will compare them
// against the migrations that have already been run for this package then
// run each of the outstanding migrations against a database connection.
$ran = $this->migrator->getRepository()->getRan();
$migrations = array_diff($files, $ran);
$batch = $this->migrator->getRepository()->getNextBatchNumber();
foreach ($migrations as $file) {
$this->line('Marking <comment>'.$file.'</comment> as <info>done</info>');
$this->migrator->getRepository()->log($file, $batch);
}
if (!$migrations) {
$this->line('All migrations are already marked as done.');
}
}
/**
* Prepare the migration database for running.
*
* @return void
*/
protected function prepareDatabase()
{
$this->migrator->setConnection($this->input->getOption('database'));
if (!$this->migrator->repositoryExists()) {
$options = array('--database' => $this->input->getOption('database'));
$this->call('migrate:install', $options);
}
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array(
array('bench', null, InputOption::VALUE_OPTIONAL, 'The name of the workbench to migrate.', null),
array('database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'),
array('path', null, InputOption::VALUE_OPTIONAL, 'The path to migration files.', null),
array('package', null, InputOption::VALUE_OPTIONAL, 'The package to migrate.', null),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment