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

五迷三道 提交于 2019-12-18 11:18:43

问题


So my migrations folder looks like this since I have dozens of tables it keeps things organized and clean:

migrations/
  create_user_table.php
  relations/
  translations/

I'm trying to do a refresh all migrations and seed but it seems like I've run into a slight hiccup where I don't know the artisan command to run migrations recursively (i.e. run migrations in the relations and translations folders as well).

I've tried adding --path="app/database/migrations/*" however it spat out an error. Does anyone know the solution to this?


回答1:


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




回答2:


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




回答3:


You can also use a wildcard, like so:

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



回答4:


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



回答5:


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




回答6:


You can try this package nscreed/laravel-migration-paths. By default all sub directories inside the migrations folder will be autoloaded. Even you can add any directories very easily.

'paths' => [
    database_path('migrations'),
    'path/to/custom_migrations', // Your Custom Migration Directory
],

Don't need any special command just the generic: php artisan migrate will do your tasks.




回答7:


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




回答8:


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



回答9:


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/');



回答10:


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'.




回答11:


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'
]);



回答12:


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

php artisan migrate --path=database/migrations/your-folder-with-migrations


来源:https://stackoverflow.com/questions/21641606/laravel-running-migrations-on-app-database-migrations-folder-recursively

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