dimanche 22 mai 2016

Running migrations while changing database

I'm making a multi tenancy application. For reasons I've chosen to go with a database for each of the tenants, and then a "master" database which contains meta information about the different tenants, and such.

I've therefore grouped my migrations into two directories:

  • Master - which contains the migrations for the master database.
  • Tenants - contains the migrations for each tenant database.

Instead of having to specify the path to the migration folders and the database to run on, each time I migrate, I've created a console command instead. However this is where the issue occurs.

I handle tenants using the subdomain as an identifier for which database to load from, using a middleware like this:

public function handle($request, Closure $next)
{
    $domains = explode('.', parse_url($request->url())['host']);
    if (count($domains) < 3)
        return app()->abort(403, "A valid subdomain is required");
    \Config::set('database.connections.tenant.database', $domains[0]);
    return $next($request);
}

This works fine for web.

However when I use Config::set() within my console command, it's being ignored, and Laravel is just using the one from my .env file.

database config file:

return [
    'default' => env('DB_CONNECTION', 'tenant'),

    'connections' => [
        'tenant' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_TENANT_DATABASE'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', '')
        ],
        'master' => [
            'driver' => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_MASTER_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
        ],
    ]
];

(I've cleaned it up a bit, so only the essential is shown).

In my .env file I've then specified a default tenant database, which is used when I'm using the cli.

DB_HOST=127.0.0.1
DB_MASTER_DATABASE=master
DB_TENANT_DATABASE=company
DB_USERNAME=root
DB_PASSWORD=

Within my TenantMigrater I iterate through each tenant, and run migrate for each of them with a different connection and path.

foreach(Tenant::all() as $tenant)
{
    $this->info("Running migrations for the tenant: {$tenant->name}");
    \Config::set('database.connections.tenant.database', $tenant->database);
    $this->call('migrate', [
        '--database' => 'tenant',
        '--path' => 'database/migrations/tenants/'
    ]);
}

Although I'm setting a new database for the tenant connection, it's still falling back to the one specified in the .env file.

I tried going through Laravel's migrater, and as far I could see, the name was being set in the config correctly, so I'm feeling a bit confused. Any ideas?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/27ODjSe
via IFTTT

Aucun commentaire:

Enregistrer un commentaire