我要运行许多迁移和播种器文件,虽然我需要运行所有文件,但目前我需要跳过一个迁移和播种器。
我如何从laravel migration和db seeder命令中跳过一个文件。
我不想从迁移或种子文件夹中删除文件以跳过该文件。
我要运行许多迁移和播种器文件,虽然我需要运行所有文件,但目前我需要跳过一个迁移和播种器。
我如何从laravel migration和db seeder命令中跳过一个文件。
我不想从迁移或种子文件夹中删除文件以跳过该文件。
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();
    }
}
DatabaseSeeder
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);
    }
}
代码,你会发现我评论了调用种子的行,并为测试目的添加了一个echo。
执行此命令:
php artisan db:seed-except
会给你:
调用ExampleTableSeeder
调用UserSamplesTableSeeder
但是,添加“除”:
php artisan db:seed-except --except = ExampleTableSeeder
会给你
调用UserSamplesTableSeeder
这可以覆盖默认值 call 你的方法 DatabaseSeeder 只有当类的名称不在$ except变量中时才调用父类。该变量由。填充 SeedExcept 自定义命令。
关于迁移,事情类似但有点困难。
我现在不能给你测试代码,但问题是:
migrate-except 覆盖的命令 MigrateCommand class(名称空间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注入)。该 Migrator class拥有读取文件夹内所有迁移并执行它的逻辑。这个逻辑在里面 run() 方法Migrator, 例如 MyMigrator,并覆盖 run() 跳过使用特殊选项传递的文件的方法__construct() 你的方法 MigrateExceptCommand 通过你的 MyMigrator: public function __construct(MyMigrator $migrator)如果我有时间,我会在赏金结束之前添加一个例子的代码
编辑 正如所承诺的,这是迁移的一个例子:
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']);
        });
    }
}
别忘了添加 Commands\MigrateExcept::class 在Kernel.php中
现在,如果你执行
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"
所以,回顾一下:
MigrateExcept class,扩展MigrateCommandMyMigrator,扩展了标准的行为 MigratorMyMigrator 类MyMigrator 覆盖了 run() 的方法 Migrator 并跳过传递的迁移代码经过测试,因此它应该在Laravel 5.2上正常工作(希望剪切和粘贴正常工作:-) ...如果有任何疑问发表评论
跳过种子非常简单,迁移不是那么多。要跳过种子,请从DatabaseSeeder类中删除以下内容。
$this->call(TableYouDontWantToSeed::class);
对于迁移,有三种方法可以执行此操作:
UsersTableMigration.dud。希望这可以帮助
我在项目中也遇到了同样的问题,但经过长时间的研发浪费后,我发现Laravel没有提供任何方法来进行迁移和播种,但你有2种方法可以做到这一点。
1)只需将它们放入不同的文件夹即可节省大量时间。 理论上你可以 制作自己的工匠指挥 这样做 你想要的,或者 通过制作目录,移动文件和运行来欺骗它 php artisan migrate。
对于播种机,只需制作一个播种机,并在其中调用您想要运行的其他播种机。然后只是明确你要运行什么播种机。试试php artisan db:seed --help 有更多细节。
2)您可以手动创建一个表(与您在db中创建的迁移表同名)并插入这样的迁移值
insert into migrations(migration, batch) values('2015_12_08_134409_create_tables_script',1);
因此,migrate命令不会创建迁移表中已存在的表。
如果您只想省略(但保留)迁移和播种器:
.php 延期: mv your_migration_file.php your_migration_fileDatabaseSeeder.php 和你不想要的播种机注释掉: //$this->call('YourSeeder');。php artisan migrate --seed在db上执行以下sql查询(注意,应该有迁移文件名WITHOUT扩展名)(这将阻止工匠迁移以后执行your_migration_file):
插入 migrations (migration, batch)价值(your_migration_file,1) 
重命名您的迁移文件: mv your_migration_file your_migration_file.php
DatabaseSeeder.php你完成了。现在你跑的时候 php artisan migrate 应该执行任何迁移(如果添加一些新的迁移文件,则除了新的迁移)。
来自Laravel文档
默认情况下,db:seed命令运行DatabaseSeeder类,该类可用于调用其他种子类。但是,您可以使用--class选项指定要单独运行的特定播种器类
php artisan db:seed --class=UserTableSeeder
对于迁移,您需要将文件移动到另一个文件夹(您要迁移的文件夹)并指定路径
php artisan migrate --path=database/migrations/temp
只是一个想法评论 播种机 和 模式。这是我猜的方式
//$this->call(HvAccountsSeeder::class);
//Schema::create('users', function (Blueprint $table) {
          //  $table->increments('id');
          //  $table->string('name');
          //  $table->string('email')->unique();
          //  $table->string('password');
          //  $table->rememberToken();
           // $table->timestamps();
       // });
// Schema::drop('users');
要直接回答您的问题,Laravel目前无法做到这一点。
如果我理解正确,我假设您正在寻找一种方法来临时禁用/跳过默认DatabaseSeeder中的特定类。
你很容易 创建自己的命令 它将接受诸如模型/表名之类的字符串,并尝试为该特定表运行迁移和种子。您只需要以下内容:
public function handle(){ //fire for Laravel 4.*
    $tables = explode(',', $this->option('tables'));//default []
    $skip = explode(',', $this->option('skip'));//default [] 
    $migrations = glob("*table*.php");//get all migrations
    foreach($migrations as $migrate){
        //if tables argument is set, check to see if part of tables
        //if file name not like any in skip.. you get the point