jeudi 4 juillet 2019

Use a specific database connection when I run database migration

I have the following database migration that I use it for testing:

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facade\App;
use Illuminate\Database\Migrations\Migration;

class TestWebsitedbMigration extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if(App::environment() == "testing")
        {
            ini_set('memory_limit', '-1');
            DB::unprepared( file_get_contents( "resources/database_dumps/my_website.sql" ) );
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        if(App::environment() == "testing")
        {
            // Burn it down!!!!
            // But needs some im plementation first
        }
    }
}

The whole idea is to use the migrations in order to create and nuking databases when I run phpunit integration tests. Because of no prior migration script existance I created a schema-only database dump in resources/database_dumps/my_website.sql.

And I use that dump in order to load the schema into my database. BUT my application has 2 database connections:

return [

    'default'     => env('DB_CONNECTION', 'brock_lesnar'),
    'connections' => [
        'seth_rollins' => [
            'driver'   => 'pgsql',
            'host'     => env('DB_API_HOST', '192.168.10.70'),
            'port'     => env('DB_API_PORT', '5432'),
            'database' => env('DB_API_DATABASE', 'etable_api'),
            'username' => env('DB_API_USERNAME', ''),
            'password' => env('DB_API_PASSWORD', ''),
            'charset'  => 'utf8',
            'prefix'   => '',
            'schema'   => 'public',
            'sslmode'  => 'prefer',
        ],

        'brock_lesnar' => [
            'driver'   => 'pgsql',
            'host' => env('BROCK_DB_HOST', '192.168.10.70'),
            'port'     => env('BROCK_DB_PORT', '5432'),
            'database' => env('BROCK_DB_DATABASE', 'etable'),
            'username' => env('BROCK_DB_USERNAME', ''),
            'password' => env('BROCK_DB_PASSWORD', ''),
            'charset'  => 'utf8',
            'prefix'   => '',
            'schema'   => 'public',
            'sslmode'  => 'prefer',
        ],
    ],

    'migrations' => 'migrations',

    /*
    |--------------------------------------------------------------------------
    | Redis Databases
    |--------------------------------------------------------------------------
    |
    | Redis is an open source, fast, and advanced key-value store that also
    | provides a richer set of commands than a typical key-value systems
    | such as APC or Memcached. Laravel makes it easy to dig right in.
    |
     */

    'redis' => [
        'client'  => 'predis',
        'default' => [
            'host'     => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', NULL),
            'port'     => env('REDIS_PORT', 6379),
            'database' => 0,
        ],
    ],
];

And I want to run this migration into the seth_rollins database connection. Do you have any idea how to do that? As you can see my default database is the brock_lestnar one.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire