44

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?

tiffanyhwang
  • 1,413
  • 4
  • 18
  • 26
  • After digging around the web, looks like it's impossible to do: https://github.com/laravel/framework/issues/2561 – tiffanyhwang Feb 08 '14 at 03:58
  • Is there a reason why not sorting/organizing your migrations is a good thing? – tiffanyhwang Feb 08 '14 at 03:59
  • 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/'); – Carlos Arturo Alaniz Feb 26 '16 at 04:06

15 Answers15

63

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

dbep
  • 647
  • 6
  • 20
automaticAllDramatic
  • 2,025
  • 1
  • 21
  • 25
52

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

zessx
  • 68,042
  • 28
  • 135
  • 158
Vinco Velky
  • 521
  • 4
  • 3
  • 5
    This is a really quick and easy solution to the problem. Don't have to think about it again, nor have a different bash script... just put your migrations in sub folders and run. – Dustin Graham Jun 18 '18 at 16:55
  • 2
    $this should be the approved answer the `path` param is just a workaround :) – dev Sep 15 '18 at 07:07
  • 2
    Could it happen that the migration files are not called in successive order? Image you have `subfolder1/2018-10.php` , `subfolder1/2018-12.php` and ``subfolder2/2018-11.php`. WIll the migration files stil be called as `2018-10`, `2018-11` and `2018-12`? – Adam Oct 31 '18 at 12:24
  • 2
    @Adam - I just tried it on Laravel 5.4 and it put the files in order based on the file names. So it ran them based on the file names (i.e. `2020_07_22_create....`) regardless of which directory they came from. – turtlechief Jul 22 '20 at 18:56
  • @turtlechief yes I made the same experience – Adam Jul 22 '20 at 19:35
  • This should actually be part of laravel by default. – rockstardev Jul 24 '21 at 19:13
16

You can also use a wildcard, like so:

php artisan migrate --path=/database/migrations/*
David Driessen
  • 161
  • 1
  • 3
4

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

Mazzy
  • 1,901
  • 2
  • 16
  • 36
  • Tested and it doesn't work. Just gives an error that it can't find the path. – Michael Ryan Soileau Dec 23 '18 at 19:46
  • @MichaelRyanSoileau We have been running this in production for 2 years, what exactly is not working for you? What system are you using? – Mazzy Jan 03 '19 at 15:02
  • PHP 7.3 running on mac, Laravel version 5.5. Every time I try that, it's a no-op. :( – Michael Ryan Soileau Jan 03 '19 at 23:44
  • Ah okay, I have done some research and it shows that Mac's bash does not support globstar out of the box (https://apple.stackexchange.com/questions/291287/globstar-invalid-shell-option-name-on-macos-even-with-bash-4-x). I myself am running homestead/vagrant and using the terminal in virtual box. But if you're using your mac's terminal globstar is not natively supported. I will edit my answer accordinly – Mazzy Jan 04 '19 at 13:43
  • Ah, all is explained. Thanks for the due diligence. – Michael Ryan Soileau Jan 04 '19 at 20:53
3

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
Sam Deering
  • 369
  • 1
  • 7
3

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.

  • 2
    This is a nice package and I'm using it in my current project. It would be nicer if you added a blacklist feature. I don't really need it in my current project. However researching this functionality I came across a rejected feature request to Laravel and one of the stated reasons is that people often use a sub folder called "archive" to store older migration files. You might also want to blacklist that by default. Otherwise, thank you for your excellent work – kaan_a Oct 02 '19 at 09:10
  • Thank you for your suggestion. :-) – Mohammad Faisal Islam Oct 02 '19 at 15:44
1

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

Sphinxila
  • 11
  • 3
1

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'
]);
DANISH
  • 11
  • 2
1

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

php artisan migrate --path=database/migrations/your-folder-with-migrations
Štefan Ondáš
  • 358
  • 1
  • 5
  • 18
1

This works at laravel 8

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

Pri Nce
  • 576
  • 6
  • 18
0

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

Ifnot
  • 4,914
  • 4
  • 33
  • 47
0

No changes are necessary, whenever you need it, use this command:

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

If you want a shortcut, you can include that in your composer.json:

"scripts": {

    "migrate": [
        "@php artisan migrate --force --path=database/migrations/*"
    ]
}

Then just use composer migrate in the terminal.

0

add in app/Providers/AppServiceProvider.php the path of your migration folder, you can add a string or an array

    public function register()
    {
        $this->registerMigrations();
    }

    protected function registerMigrations()
    {
        return $this->loadMigrationsFrom(__DIR__ . '/../../database/migrations/**/*');
    }


Elvis Reis
  • 155
  • 4
  • 12
-1

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
shahalpk
  • 3,383
  • 7
  • 36
  • 55
  • when i use this command they shows error --"'gulp' is not recognized as an internal or external command," – Vipul Apr 05 '16 at 07:09
  • @Vipul: install gulp (via `npm install --global gulp`) and try again. More info here.. https://laravel.com/docs/5.0/elixir – shahalpk Apr 05 '16 at 10:26
  • Do this in your composer.json https://getcomposer.org/doc/articles/scripts.md#defining-scripts – abenevaut Mar 02 '21 at 13:46
-1

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