mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 21:14:03 +00:00
Fix #71
This commit is contained in:
parent
ae5527c9a9
commit
a3f37b7f77
7 changed files with 78 additions and 6 deletions
|
|
@ -57,6 +57,10 @@ class Migrate extends MigrateCommand
|
||||||
parent::handle();
|
parent::handle();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (tenancy()->initialized) {
|
||||||
|
tenancy()->switchDatabaseConnection();
|
||||||
|
} else {
|
||||||
$this->database->disconnect();
|
$this->database->disconnect();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -56,5 +56,11 @@ class Rollback extends RollbackCommand
|
||||||
// Migrate
|
// Migrate
|
||||||
parent::handle();
|
parent::handle();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (tenancy()->initialized) {
|
||||||
|
tenancy()->switchDatabaseConnection();
|
||||||
|
} else {
|
||||||
|
$this->database->disconnect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,8 @@
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Commands;
|
namespace Stancl\Tenancy\Commands;
|
||||||
|
|
||||||
use Illuminate\Console\Command;
|
|
||||||
use Stancl\Tenancy\DatabaseManager;
|
use Stancl\Tenancy\DatabaseManager;
|
||||||
use Stancl\Tenancy\Traits\HasATenantsOption;
|
use Stancl\Tenancy\Traits\HasATenantsOption;
|
||||||
use Illuminate\Database\Migrations\Migrator;
|
|
||||||
use Illuminate\Database\Console\Seeds\SeedCommand;
|
use Illuminate\Database\Console\Seeds\SeedCommand;
|
||||||
use Illuminate\Database\ConnectionResolverInterface;
|
use Illuminate\Database\ConnectionResolverInterface;
|
||||||
|
|
||||||
|
|
@ -56,5 +54,11 @@ class Seed extends SeedCommand
|
||||||
// Seed
|
// Seed
|
||||||
parent::handle();
|
parent::handle();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (tenancy()->initialized) {
|
||||||
|
tenancy()->switchDatabaseConnection();
|
||||||
|
} else {
|
||||||
|
$this->database->disconnect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,9 +10,17 @@ use Stancl\Tenancy\Exceptions\PhpRedisNotInstalledException;
|
||||||
trait BootstrapsTenancy
|
trait BootstrapsTenancy
|
||||||
{
|
{
|
||||||
public $originalSettings = [];
|
public $originalSettings = [];
|
||||||
|
/**
|
||||||
|
* Was tenancy initialized/bootstrapped?
|
||||||
|
*
|
||||||
|
* @var boolean
|
||||||
|
*/
|
||||||
|
public $initialized = false;
|
||||||
|
|
||||||
public function bootstrap()
|
public function bootstrap()
|
||||||
{
|
{
|
||||||
|
$this->initialized = true;
|
||||||
|
|
||||||
$this->switchDatabaseConnection();
|
$this->switchDatabaseConnection();
|
||||||
if ($this->app['config']['tenancy.redis.tenancy']) {
|
if ($this->app['config']['tenancy.redis.tenancy']) {
|
||||||
$this->setPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']);
|
$this->setPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']);
|
||||||
|
|
@ -23,6 +31,8 @@ trait BootstrapsTenancy
|
||||||
|
|
||||||
public function end()
|
public function end()
|
||||||
{
|
{
|
||||||
|
$this->initialized = false;
|
||||||
|
|
||||||
$this->disconnectDatabase();
|
$this->disconnectDatabase();
|
||||||
if ($this->app['config']['tenancy.redis.tenancy']) {
|
if ($this->app['config']['tenancy.redis.tenancy']) {
|
||||||
$this->resetPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']);
|
$this->resetPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']);
|
||||||
|
|
@ -53,7 +63,8 @@ trait BootstrapsTenancy
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function resetPhpRedisPrefix($connections = ['default']) {
|
public function resetPhpRedisPrefix($connections = ['default'])
|
||||||
|
{
|
||||||
foreach ($connections as $connection) {
|
foreach ($connections as $connection) {
|
||||||
$client = Redis::connection($connection)->client();
|
$client = Redis::connection($connection)->client();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@ namespace Stancl\Tenancy\Tests;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Artisan;
|
use Illuminate\Support\Facades\Artisan;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
|
||||||
|
|
||||||
class CommandsTest extends TestCase
|
class CommandsTest extends TestCase
|
||||||
{
|
{
|
||||||
|
|
@ -72,4 +74,27 @@ class CommandsTest extends TestCase
|
||||||
{
|
{
|
||||||
$this->markTestIncomplete();
|
$this->markTestIncomplete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function database_connection_is_switched_to_default_after_migrating_or_seeding_or_rolling_back()
|
||||||
|
{
|
||||||
|
$originalDBName = DB::connection()->getDatabaseName();
|
||||||
|
|
||||||
|
Artisan::call('tenants:migrate');
|
||||||
|
$this->assertSame($originalDBName, DB::connection()->getDatabaseName());
|
||||||
|
|
||||||
|
Artisan::call('tenants:seed', ['--class' => ExampleSeeder::class]);
|
||||||
|
$this->assertSame($originalDBName, DB::connection()->getDatabaseName());
|
||||||
|
|
||||||
|
Artisan::call('tenants:rollback');
|
||||||
|
$this->assertSame($originalDBName, DB::connection()->getDatabaseName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function database_connection_is_switched_to_default_after_migrating_or_seeding_or_rolling_back_when_tenancy_has_been_initialized()
|
||||||
|
{
|
||||||
|
tenancy()->init('localhost');
|
||||||
|
|
||||||
|
$this->database_connection_is_switched_to_default_after_migrating_or_seeding_or_rolling_back();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
24
tests/Etc/ExampleSeeder.php
Normal file
24
tests/Etc/ExampleSeeder.php
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Tests\Etc;
|
||||||
|
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
class ExampleSeeder extends Seeder
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the database seeds.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function run()
|
||||||
|
{
|
||||||
|
DB::table('users')->insert([
|
||||||
|
'name' => Str::random(10),
|
||||||
|
'email' => Str::random(10).'@gmail.com',
|
||||||
|
'password' => bcrypt('password'),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -106,7 +106,6 @@ class TenantManagerTest extends TestCase
|
||||||
public function tenancy_can_be_ended()
|
public function tenancy_can_be_ended()
|
||||||
{
|
{
|
||||||
$originals = [
|
$originals = [
|
||||||
'databasePDO' => DB::connection()->getPDO(),
|
|
||||||
'databaseName' => DB::connection()->getDatabaseName(),
|
'databaseName' => DB::connection()->getDatabaseName(),
|
||||||
'storage_path' => storage_path(),
|
'storage_path' => storage_path(),
|
||||||
'storage_root' => Storage::disk('local')->getAdapter()->getPathPrefix(),
|
'storage_root' => Storage::disk('local')->getAdapter()->getPathPrefix(),
|
||||||
|
|
@ -139,7 +138,6 @@ class TenantManagerTest extends TestCase
|
||||||
public function tenancy_can_be_ended_after_reidentification()
|
public function tenancy_can_be_ended_after_reidentification()
|
||||||
{
|
{
|
||||||
$originals = [
|
$originals = [
|
||||||
'databasePDO' => DB::connection()->getPDO(),
|
|
||||||
'databaseName' => DB::connection()->getDatabaseName(),
|
'databaseName' => DB::connection()->getDatabaseName(),
|
||||||
'storage_path' => storage_path(),
|
'storage_path' => storage_path(),
|
||||||
'storage_root' => Storage::disk('local')->getAdapter()->getPathPrefix(),
|
'storage_root' => Storage::disk('local')->getAdapter()->getPathPrefix(),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue