0

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?

Engr. Umar Ejaz
  • 106
  • 1
  • 6
  • 1
    Does this answer your question? [How to specify folder for laravel migrations?](https://stackoverflow.com/questions/55018704/how-to-specify-folder-for-laravel-migrations) – Hamza Zafeer Nov 02 '22 at 13:39
  • Depending on your implementation, you might use a similar solution to https://stackoverflow.com/questions/21641606/laravel-running-migrations-on-app-database-migrations-folder-recursively which sais you can add `$this->loadMigrationsFrom($paths);` to `AppServiceProvider::boot()` method – Techno Nov 02 '22 at 13:41
  • @Techno Yes it works but it take migrations from both `database/migrations` folder and `/database/migrations/master_database` as well while I want that it should migrations only from `/database/migrations/master_database`. Any idea? – Engr. Umar Ejaz Nov 03 '22 at 09:50
  • My best guess is to make sure the `$paths` array should be literally: `['database/migrations/master_database']` the example I sent you was about recursive loading, you only want 1 path. – Techno Nov 03 '22 at 10:59

3 Answers3

0
php artisan make:migration  "create users table" --path=/var/www/html/custom_migration

php artisan migrate --path=/var/www/html/custom_migration
Ramil Huseynov
  • 350
  • 4
  • 7
  • Yes, it works but I want to automate the process i.e. If I run `php artisan migrate` it should run the migrations from `/database/migrations/master_database` so that I do not have to specify the path again and again. – Engr. Umar Ejaz Nov 03 '22 at 06:22
  • would you can use laravel custom command https://laravel.com/docs/9.x/artisan – Ramil Huseynov Nov 03 '22 at 07:49
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. You can find more information on how to write good answers in the help center: https://stackoverflow.com/help/how-to-answer . Good luck – nima Nov 03 '22 at 08:37
  • @RamilHuseynov I've created custom command for child databases which run using `php artisan migrate --all` and it takes the migrations from `database/migrations` folder. – Engr. Umar Ejaz Nov 03 '22 at 09:53
0

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.

pasindu
  • 529
  • 1
  • 4
  • 16
  • Yes it works but it take migrations from both `/database/migrations` and `/database/migrations/master_database` folder as well while I want that it should migrations only from `/database/migrations/master_database`. What I have done in AppServiceProvider: $masterDatabasePath = database_path('migrations/master_database'); $this->loadMigrationsFrom($masterDatabasePath); Any Idea what I'm doing wrong? – Engr. Umar Ejaz Nov 03 '22 at 10:05
-1

I've achieved this by changing core Laravel BaseCommand.php file in: vendor\laravel\framework\src\Illuminate\Database\Console\Migrations\BaseCommand.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';
}
Engr. Umar Ejaz
  • 106
  • 1
  • 6
  • Changing stuff in the `vendor` folder is never a good idea, as such a change would be lost after updating the package the file belongs to – Nico Haase Nov 07 '22 at 09:44
  • @NicoHaase Yes, it is not a good idea, but I'm writing documentation to keep a track of it because I cannot find any other way to achieve the required results. – Engr. Umar Ejaz Nov 07 '22 at 12:56