CLab's avatar
Level 3

Set different folder for local/production and test migrations

I have an app which was written in a different language and has an existing database, which I am trying to create a Laravel version of (Laravel 11.x). The local/production database is already setup with tables (as the local database is a copy of the production), however I would like to create migrations for these tables to use in tests. There are certain migrations I would like to be performed in both test and production. So my thought was to have separate folder for migrations for the test and production.

I tried creating a folder called database/migrations_production where I would copy over any migrations which I would want to run on the production database, and I was hoping to use the default database/migrations folder for the test migrations. And I tried to add the following code in the AppServiceProvider (based on a suggestion from ChatGPT/Larry):

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        //
        $this->configureMigrationPaths();
    }

    /**
     * Configure the migration paths based on the environment.
     *
     * @return void
     */
    protected function configureMigrationPaths()
    {
        $migrator = $this->app->make('migrator');

        // Clear existing migration paths
        $migrator->paths([]);

        // Determine the appropriate migration path
        $migrationPath = database_path('migrations');
        if ($this->app->environment('local', 'production')) {
            $migrationPath = database_path('migrations_production');
        }

        // Set the migration path
        $migrator->path($migrationPath);

        // Debugging output to verify the path
        \Log::info('Migration path set to: ' . $migrationPath);
        echo 'Migration path set to: ' . $migrationPath . PHP_EOL;
    }

However, when I run php artisan migrate:status, even though the debugging output at the end of the function shows the correct paths, the status shows all the migrations in the default migration folder.

Please help with any solutions on how to tackle this situation.

0 likes
3 replies
Tray2's avatar

You can dump the structure with php artisan schema dump then use it for tests.

martinbean's avatar

@clab You don’t want to create different migrations for different environments.

If you’re creating a new Laravel application on an existing database, then just use the schema:dump command to create a “squash” migration file that will create the database schema as it is now. Then create individual migrations for schema changes going forward.

CLab's avatar
Level 3

@martinbean So what has happened is that my local db has partially the prod DB and partially some of the newer migrations which I have run. So does this mean I should rollback my migrations -> do a schema:dump and then run migrations again?

Also, is it possible to do a schema dump for postgres/mysql and make use of it in sqlite in tests?

Please or to participate in this conversation.