php Laravel 以递归方式在“app/database/migrations”文件夹上运行迁移

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21641606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:18:34  来源:igfitidea点击:

Laravel running migrations on "app/database/migrations" folder recursively

phplaraveldatabase-migration

提问by tiffanyhwang

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 relationsand translationsfolders as well).

我正在尝试刷新所有迁移和种子,但似乎我遇到了一个小问题,我不知道以递归方式运行迁移的工匠命令(即也在relationstranslations文件夹中运行迁移)。

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

我试过添加--path="app/database/migrations/*"但是它吐出了一个错误。有谁知道这个问题的解决方案?

回答by 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.

但是,您可以轻松扩展 artisan 系统以编写您自己的 migrate 命令,该命令将遍历迁移文件夹下的所有文件夹,为您创建这些命令并运行它们。

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

如果您不想通过 artisan 进行此操作,您也可以简单地编写一个 shell 脚本

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

编辑:对于 Laravel >= 5.0,在子目录中迁移迁移文件的正确命令是:

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

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

回答by Vinco Velky

This add to bootmethod in AppServiceProvider

这添加到AppServiceProvider 中的引导方法

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

$this->loadMigrationsFrom($paths);

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

现在你使用 canphp artisan migrate并且还php artisan migrate:back

回答by David Driessen

You can also use a wildcard, like so:

您还可以使用通配符,如下所示:

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

回答by Sam Deering

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

在 Laravel 5 中,数据库文件夹默认位于 app 文件夹旁边。所以你可以运行它来迁移单个文件夹的迁移:

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

回答by Mazzy

You can use the following command to do this recursively:

您可以使用以下命令递归执行此操作:

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

**/*is also known as the globstar

**/*也被称为 globstar

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

在此工作之前,您必须检查您的 bash 是否支持 globstar。您可以通过执行shopt并检查globstar.

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

大多数服务器发行版默认支持 Globstar,但可能不适用于 MAC。

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

有关 globstar 的更多信息,请参见:https://www.linuxjournal.com/content/globstar-new-bash-globbing-option

回答by Mohammad Faisal Islam

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

你可以试试这个包nscreed/laravel-migration-paths。默认情况下,将自动加载迁移文件夹内的所有子目录。即使您可以非常轻松地添加任何目录。

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

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

不需要任何特殊命令只是通用的:php artisan migrate将完成您的任务。

回答by Ifnot

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

这不是一个“直接”的解决方案,但我建议您在 Laravel 项目中查看模块化。

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

这个包是一个好的开始:https: //github.com/pingpong-labs/modules

回答by shahalpk

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

一个简单的 Laravel 解决方案是创建一个 gulp 任务(比如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

回答by Carlos Arturo Alaniz

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

回答by Sphinxila

I rewrote the MigrationServiceProvider:

我重写了 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'.

或者您只需在应用程序启动时注册路径。在我了解 '$this->loadMigrationsFrom' 之前,我已经完成了我的解决方案。