kitloong / laravel-migrations-generator

Laravel Migrations Generator: Automatically generate your migrations from an existing database schema.
MIT License
2.5k stars 276 forks source link

Option to save the connection name #95

Closed Danysan1 closed 2 years ago

Danysan1 commented 2 years ago

I have an application which uses multiple schemas on the same DB, with a connection for each schema:

config/database.php:

// ...
    'default' => env('DB_CONNECTION', 'public_schema'),
// ...
    'connections' => [
        'public_schema' => [
            'driver' => 'pgsql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
            'search_path' => 'public',
            'sslmode' => 'prefer',
        ],
        'foo_schema' => [
            'driver' => 'pgsql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
            'search_path' => 'foo',
            'sslmode' => 'prefer',
        ],
// ...

If I generate the migrations they don't specify the connection for any schema:

php artisan migrate:generate --connection 'foo_schema'

database/migrations/..._create_bar_table:

// ...
    public function up()
    {
        Schema::create('bar', function (Blueprint $table) {
// ...
        });
    }

    public function down()
    {
        Schema::dropIfExists('bar');
    }
// ...

This means that when executing the migrations all operations (like table creation) are executed on the public schema. It would be helpful to have an option, for example --save-connection, which when enabled in combinatio with --connection='...' saved the connection name:

php artisan migrate:generate --connection 'foo_schema' --save-connection

database/migrations/..._create_bar_table:

// ...
    public function up()
    {
        Schema::connection('foo_schema')->create('bar', function (Blueprint $table) {
// ...
        });
    }

    public function down()
    {
        Schema::connection('foo_schema')->dropIfExists('bar');
    }
// ...
kitloong commented 2 years ago

Hi @Danysan1

Thank you for the details. In fact, generating with the connection name is the expected behavior. I will label this issue as a bug and push the fix soon.

kitloong commented 2 years ago

Released with tag v6.3.0