我有许多迁移和播种器文件要运行,虽然我需要运行所有文件,但目前我需要跳过一个迁移和播种器。
如何从 laravel 迁移和 db seeder 命令中跳过一个文件。
我不想从 migrations 或 seeds 文件夹中删除文件以跳过该文件。
最佳答案
Laravel 没有给你一个默认的方法来做这件事。但是,您可以创建自己的控制台命令和播种器来实现它。
假设您有这个默认的 DatabaseSeeder
类:
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call(ExampleTableSeeder::class);
$this->call(UserSamplesTableSeeder::class);
}
}
目标是创建一个覆盖“db:seed”的新命令,并将一个新参数“except”参数传递给 DatabaseSeeder
类。
这是我在 Laravel 5.2 实例上创建并尝试的最终代码:
命令,放入app/Console/Commands,别忘了更新你的Kernel.php:
namespace App\Console\Commands;
use Illuminate\Console\Command;
class SeedExcept extends Command
{
protected $signature = 'db:seed-except {--except=class name to jump}';
protected $description = 'Seed all except one';
public function handle()
{
$except = $this->option('except');
$seeder = new \DatabaseSeeder($except);
$seeder->run();
}
}
数据库播种器
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
protected $except;
public function __construct($except = null) {
$this->except = $except;
}
public function call($class)
{
if ($class != $this->except)
{
echo "calling $class \n";
//parent::call($class); // uncomment this to execute after tests
}
}
public function run()
{
$this->call(ExampleTableSeeder::class);
$this->call(UserSamplesTableSeeder::class);
}
}
在代码中,您会发现我注释了调用种子的行并添加了回显以用于测试目的。
执行这条命令:
php artisan db:seed-except
会给你:
calling ExampleTableSeeder
calling UserSamplesTableSeeder
但是,添加“除外”:
php artisan db:seed-except --except=ExampleTableSeeder
给你
calling UserSamplesTableSeeder
这可以覆盖 DatabaseSeeder
类的默认 call
方法,并且仅当类的名称不在 $except 变量中时才调用父类。该变量由 SeedExcept
自定义命令填充。
关于迁移,事情是相似的,但有点困难。
我现在不能给你测试过的代码,但问题是:
- 你创建一个
migrate-except
命令来覆盖MigrateCommand
类(命名空间 Illuminate\Database\Console\Migrations,位于 vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php)。 MigrateCommand
接受一个Migrator
对象(命名空间 Illuminate\Database\Migrations,路径 vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php)在构造函数中(通过 IoC 注入(inject))。Migrator
类拥有读取文件夹内所有迁移并执行它的逻辑。此逻辑在run()
方法中- 创建一个
Migrator
的子类,例如MyMigrator
,并覆盖run()
方法以跳过使用特殊选项传递的文件 - 覆盖
MigrateExceptCommand
的__construct()
方法并传递您的MyMigrator
:public function __construct(MyMigrator $migrator)
如果我有时间,我会在赏金结束前添加示例代码
编辑 正如所 promise 的那样,这里有一个迁移示例:
MyMigrator 类,扩展 Migrator 并包含跳过文件的逻辑:
namespace App\Helpers;
use Illuminate\Database\Migrations\Migrator;
class MyMigrator extends Migrator
{
public $except = null;
// run() method copied from it's superclass adding the skip logic
public function run($path, array $options = [])
{
$this->notes = [];
$files = $this->getMigrationFiles($path);
// skip logic
// remove file from array
if (isset($this->except))
{
$index = array_search($this->except,$files);
if($index !== FALSE){
unset($files[$index]);
}
}
var_dump($files); // debug
$ran = $this->repository->getRan();
$migrations = array_diff($files, $ran);
$this->requireFiles($path, $migrations);
//$this->runMigrationList($migrations, $options); // commented for debugging purposes
}
}
MigrateExcept 自定义命令
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Database\Console\Migrations\MigrateCommand;
use App\Helpers\MyMigrator;
use Illuminate\Database\Migrations\Migrator;
use Symfony\Component\Console\Input\InputOption;
class MigrateExcept extends MigrateCommand
{
protected $name = 'migrate-except';
public function __construct(MyMigrator $migrator)
{
parent::__construct($migrator);
}
public function fire()
{
// set the "except" param, containing the name of the file to skip, on our custom migrator
$this->migrator->except = $this->option('except');
parent::fire();
}
// add the 'except' option to the command
protected function getOptions()
{
return [
['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'],
['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
['path', null, InputOption::VALUE_OPTIONAL, 'The path of migrations files to be executed.'],
['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'],
['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run.'],
['step', null, InputOption::VALUE_NONE, 'Force the migrations to be run so they can be rolled back individually.'],
['except', null, InputOption::VALUE_OPTIONAL, 'Files to jump'],
];
}
}
最后,您需要将其添加到服务提供者以允许 Laravel IoC 解析依赖项
namespace App\Providers;
use App\Helpers\MyMigrator;
use App\Console\Commands\MigrateExcept;
class CustomServiceProvider extends ServiceProvider
{
public function boot()
{
parent::boot($events);
$this->app->bind('Illuminate\Database\Migrations\MigrationRepositoryInterface', 'migration.repository');
$this->app->bind('Illuminate\Database\ConnectionResolverInterface', 'Illuminate\Database\DatabaseManager');
$this->app->singleton('MyMigrator', function ($app) {
$repository = $app['migration.repository'];
return new MyMigrator($repository, $app['db'], $app['files']);
});
}
}
不要忘记在 Kernel.php 中添加 Commands\MigrateExcept::class
现在,如果你执行
php artisan migrate-except
你有:
array(70) {
[0] =>
string(43) "2014_04_24_110151_create_oauth_scopes_table"
[1] =>
string(43) "2014_04_24_110304_create_oauth_grants_table"
[2] =>
string(49) "2014_04_24_110403_create_oauth_grant_scopes_table"
...
但添加除了参数:
php artisan migrate-except --except=2014_04_24_110151_create_oauth_scopes_table
array(69) {
[1] =>
string(43) "2014_04_24_110304_create_oauth_grants_table"
[2] =>
string(49) "2014_04_24_110403_create_oauth_grant_scopes_table"
所以,回顾一下:
- 我们创建一个自定义的 migrate-except 命令,
MigrateExcept
类,扩展 MigrateCommand - 我们创建一个自定义迁移器类
MyMigrator
,扩展标准Migrator
的行为 - 当 MigrateExcept 为 fire() 时,传递文件名以跳转到我们的
MyMigrator
类 MyMigrator
覆盖Migrator
的run()
方法,跳过传递过来的迁移- 更多:因为我们需要指示 Laravel IoC 关于新创建的类,所以它可以正确地注入(inject)它们,我们创建一个服务提供者
代码已经过测试,因此它应该可以在 Laravel 5.2 上正常工作(希望剪切和粘贴工作正常 :-) ...如果有人有任何疑问,请发表评论
关于php - 除了一个之外,如何运行 laravel 迁移和 DB seeder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35191709/