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

jeFFF's avatar
Level 3

Deploy migrations for test env only

Hello there,

I think the question is explicit but let be more precise. A client of mine have a DB which is created and seeded by his system, on top of that I have a few migrations made by Laravel. For testing purposes I need to create Objects created by the tables of my client so I want to create migrations for these "outside" and apply this only in the testing environment.

Any help for that ?

0 likes
7 replies
DavidPetrov's avatar

You may want to refer to laravel factories for that matter. Look up the documentation! :)

Taner's avatar

You can check your .env file to run some migrations.

public function up()
{
    if (env('APP_ENV') == 'testing'){
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestampsTz();
        });
    }
}
2 likes
Sti3bas's avatar

@taner that's not a clean solution, because these empty migrations will still run in the production.

1 like
chrispage1's avatar

Just came here after a little bit of Googling and can't believe I didn't think of it myself but @taner solution makes complete sense.

It'll only run when unit tests are being performed as the environment on production will match production

Thanks!

1 like
da_Mask's avatar

Instead of checking your env in the migration, add this to the boot method of your AppServiceProvider

 if($this->app->environment('testing')){
     $this->loadMigrationsFrom(base_path('tests/database/migrations'));
}

Then create a directory at /tests/database/migrations and add the migrations you want to only run in your test suite there.

3 likes

Please or to participate in this conversation.