Laravel running migrations on “app/database/migrations” folder recursively

99封情书 提交于 2019-11-30 04:54:49
automaticAllDramatic

The only way to do it right now is to manually go through all the migrations. That is, you have to run the migration command on each of your subfolders:

php artisan migrate --path=/app/database/migrations/relations  
php artisan migrate --path=/app/database/migrations/translations

However, what you can do is easily extend the artisan system to write your own migrate command that will iterate through all folders under the migrations folder, create these commands for you and run them.

You can also simply write a shell script if you don't want to get into doing this via artisan

Edit: for Laravel >= 5.0, the correct commands to migrate migration files in sub directories would be:

php artisan migrate --path=/database/migrations/relations
php artisan migrate --path=/database/migrations/translations

This add to boot method in AppServiceProvider

$mainPath = database_path('migrations');ň
$directories = glob($mainPath . '/*' , GLOB_ONLYDIR);
$paths = array_merge([$mainPath], $directories);

$this->loadMigrationsFrom($paths);

Now you use can php artisan migrate and also php artisan migrate:back

You can also use a wildcard, like so:

php artisan migrate --path=/database/migrations/*

In Laravel 5 the database folder sits alongside the app folder by default. So you could run this to migrate a single folders migrations:

php artisan migrate --path=/database/migrations/users

You can use the following command to do this recursively:

php artisan migrate --path=/database/migrations/**/*

**/* is also known as the globstar

Before this works you must check if your bash supports the globstar. You can do this by executing shopt and checking for globstar.

Globstar is supported by default by most server distributions but might not work on MAC.

For more on globstar see: https://www.linuxjournal.com/content/globstar-new-bash-globbing-option

It is a not a "direct" solution but i suggest you to look at Modularity into your laravel project.

Modules can segment your application in several smaller "folders" and bring migration, seeds, classes, routes, controllers, commands together in easily maintainable folders.

This package is a good start : https://github.com/pingpong-labs/modules

A Simple Laravel solution is to create a gulp task (say migrate-others).

var elixir = require('laravel-elixir');
var gulp = require('gulp');
var shell = require('gulp-shell')

/*
 |--------------------------------------------------------------------------
 | Elixir Asset Management
 |--------------------------------------------------------------------------
 |
 | Elixir provides a clean, fluent API for defining some basic Gulp tasks
 | for your Laravel application. By default, we are compiling the Sass
 | file for our application, as well as publishing vendor resources.
 |
 */

elixir(function(mix) {
    mix.sass('app.scss');
});

// Our Task
gulp.task('migrate-others', shell.task([
    'php artisan migrate --path=/app/database/migrations/relations',
    'php artisan migrate --path=/app/database/migrations/translations',
]));

Now you can simply call

gulp migrate-others

Here you go!

 function rei($folder)
    {
        $iterator = new DirectoryIterator($folder);
        system("php artisan migrate --path=" . $folder);
        foreach ($iterator as $fileinfo) {
            if ($fileinfo->isDir() && !$fileinfo->isDot()) {
                echo $fileinfo->getFilename() . "\n";
                rei($folder . $fileinfo->getFilename() . '/');
            }
        }
    }

 rei('./database/');

I rewrote the MigrationServiceProvider:

- registerResetCommand()
- registerStatusCommand()
- registerMigrateCommand()

There you can register your own commands:

class MigrateCommand extends Illuminate\Database\Console\Migrations\MigrateCommand

After that you just need to extend youd directories:

protected function getMigrationPaths()

Or you just register the paths on application boot. I've already done my solution before I knwewd about '$this->loadMigrationsFrom'.

A Simple solution is to create an Artisan Command for example (migrate:all),

then inside handle function define migrate command for each sub directories as mentioned bellow.

Artisan::call('migrate', [
   '--path' => '/database/migrations/employee'
]);

Only relative path works for me (in Laravel 5.7):

php artisan migrate --path=database/migrations/your-folder-with-migrations
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!