skip to Main Content

I have multiple databases setup. I want to change the default folder for migration run i.e. I want that if I run php artisan migrate. It should run the new migrations in /database/migrations/master_database instead of /database/migrations as I have migrations for child databases in main /database/migrations folder which I’m successfully running using php artisan migrate --all.

What I have done in AppServiceProvider:

$masterDatabasePath = database_path('migrations/master_database');

$this->loadMigrationsFrom($masterDatabasePath);

It works but it take migrations from both /database/migrations and /database/migrations/master_database folders while I want that it should only take migrations from /database/migrations/master_database.

Any Idea what I’m doing wrong or how it can be fixed?

3

Answers


  1. Chosen as BEST ANSWER

    I've achieved this by changing core Laravel BaseCommand.php file in: vendorlaravelframeworksrcIlluminateDatabaseConsoleMigrationsBaseCommand.php

    Changings done in getMigrationPaths() function, from this:

    return array_merge(
        $this->migrator->paths(), [$this->getMigrationPath()]
    );
    

    To this:

    return array_merge(
        $this->migrator->paths(), [$this->getMasterDatabaseMigrationPath()]
    );
        
    

    and then define getMasterDatabaseMigrationPath() function in same file:

    protected function getMasterDatabaseMigrationPath(){
      return $this->laravel->databasePath().DIRECTORY_SEPARATOR.'migrations/master_database';
    }
    

  2. php artisan make:migration  "create users table" --path=/var/www/html/custom_migration
    
    php artisan migrate --path=/var/www/html/custom_migration
    
    Login or Signup to reply.
  3. Try adding this 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search