Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mbellido's avatar

RefreshDatabase migration path

Hi!

I've my migrations at differente default location (database/migrations/tenants/) and i want to use the RefreshDatabase trait, however it uses the default migration location.

How can i specify at setUp or somewhere the "path" option for this migrations?

The only way that i found to do that is overriding "refreshInMemoryDatabase" and "refreshTestDatabase" methods but im thinking in a better way.

0 likes
6 replies
mushood's avatar

If you run the following command:

php artisan migrate -h

You will get this:

Usage:
  migrate [options]

Options:
      --database[=DATABASE]  The database connection to use.
      --force                Force the operation to run when in production.
      --path[=PATH]          The path of migrations files to be executed.
      --pretend              Dump the SQL queries that would be run.
      --seed                 Indicates if the seed task should be re-run.
      --step                 Force the migrations to be run so they can be rolled back individually.
  -h, --help                 Display this help message
  -q, --quiet                Do not output any message
  -V, --version              Display this application version
      --ansi                 Force ANSI output
      --no-ansi              Disable ANSI output
  -n, --no-interaction       Do not ask any interactive question
      --env[=ENV]            The environment the command should run under
  -v|vv|vvv, --verbose       Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

As you can see, you have a "--path" option

So you can run

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

OR

php artisan migrate:fresh --path=/database/migrations/tenants

OR

php artisan migrate:refresh --path=/database/migrations/tenants

According to what you need

1 like
mbellido's avatar

I know, but this is for manual migrations, i'm looking a way to do this in my tests automatically using the RefreshDatabase (or DatabaseMigrations) trait.

For now, my best aproach is overriding the artisan method on my TestCase:

public function artisan($command, $parameters = [])
{
    if (str_before($command, ':') === 'migrate' && !isset($parameters['--path'])) {
        $parameters['--path'] = 'database/migrations/tenants/';
    }

    return parent::artisan($command, $parameters);
}
1 like
mushood's avatar

I actually did not know that could be done. Thanks for sharing.

bpjuli22's avatar

We can create the migrations in a particular directory that is inside a migrations like /database/migrations/newLocation by changing the path inside vendor/laravel/framework/src/Database/Migrations/MigrationCreater.php >> here:: inside getPath function => change the $path locations to new one like

return $path.'/tenant/'.$this->getDatePrefix().'_'.$name.'.php';

Please or to participate in this conversation.