From a3f37b7f771103bd52cd423f21c53888d72bcc5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Thu, 25 Jul 2019 23:26:36 +0200 Subject: [PATCH] Fix #71 --- src/Commands/Migrate.php | 6 +++++- src/Commands/Rollback.php | 6 ++++++ src/Commands/Seed.php | 8 ++++++-- src/Traits/BootstrapsTenancy.php | 13 ++++++++++++- tests/CommandsTest.php | 25 +++++++++++++++++++++++++ tests/Etc/ExampleSeeder.php | 24 ++++++++++++++++++++++++ tests/TenantManagerTest.php | 2 -- 7 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 tests/Etc/ExampleSeeder.php diff --git a/src/Commands/Migrate.php b/src/Commands/Migrate.php index d6c5ca35..472c189e 100644 --- a/src/Commands/Migrate.php +++ b/src/Commands/Migrate.php @@ -57,6 +57,10 @@ class Migrate extends MigrateCommand parent::handle(); }); - $this->database->disconnect(); + if (tenancy()->initialized) { + tenancy()->switchDatabaseConnection(); + } else { + $this->database->disconnect(); + } } } diff --git a/src/Commands/Rollback.php b/src/Commands/Rollback.php index 8de6dc53..6f66f4d8 100644 --- a/src/Commands/Rollback.php +++ b/src/Commands/Rollback.php @@ -56,5 +56,11 @@ class Rollback extends RollbackCommand // Migrate parent::handle(); }); + + if (tenancy()->initialized) { + tenancy()->switchDatabaseConnection(); + } else { + $this->database->disconnect(); + } } } diff --git a/src/Commands/Seed.php b/src/Commands/Seed.php index 16ada831..a2be37da 100644 --- a/src/Commands/Seed.php +++ b/src/Commands/Seed.php @@ -2,10 +2,8 @@ namespace Stancl\Tenancy\Commands; -use Illuminate\Console\Command; use Stancl\Tenancy\DatabaseManager; use Stancl\Tenancy\Traits\HasATenantsOption; -use Illuminate\Database\Migrations\Migrator; use Illuminate\Database\Console\Seeds\SeedCommand; use Illuminate\Database\ConnectionResolverInterface; @@ -56,5 +54,11 @@ class Seed extends SeedCommand // Seed parent::handle(); }); + + if (tenancy()->initialized) { + tenancy()->switchDatabaseConnection(); + } else { + $this->database->disconnect(); + } } } diff --git a/src/Traits/BootstrapsTenancy.php b/src/Traits/BootstrapsTenancy.php index 18e29f79..b53ce25f 100644 --- a/src/Traits/BootstrapsTenancy.php +++ b/src/Traits/BootstrapsTenancy.php @@ -10,9 +10,17 @@ use Stancl\Tenancy\Exceptions\PhpRedisNotInstalledException; trait BootstrapsTenancy { public $originalSettings = []; + /** + * Was tenancy initialized/bootstrapped? + * + * @var boolean + */ + public $initialized = false; public function bootstrap() { + $this->initialized = true; + $this->switchDatabaseConnection(); if ($this->app['config']['tenancy.redis.tenancy']) { $this->setPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']); @@ -23,6 +31,8 @@ trait BootstrapsTenancy public function end() { + $this->initialized = false; + $this->disconnectDatabase(); if ($this->app['config']['tenancy.redis.tenancy']) { $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) { $client = Redis::connection($connection)->client(); diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index fcdac6ad..046c2998 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -4,6 +4,8 @@ namespace Stancl\Tenancy\Tests; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Schema; +use Illuminate\Support\Facades\DB; +use Stancl\Tenancy\Tests\Etc\ExampleSeeder; class CommandsTest extends TestCase { @@ -72,4 +74,27 @@ class CommandsTest extends TestCase { $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(); + } } diff --git a/tests/Etc/ExampleSeeder.php b/tests/Etc/ExampleSeeder.php new file mode 100644 index 00000000..591b9ac6 --- /dev/null +++ b/tests/Etc/ExampleSeeder.php @@ -0,0 +1,24 @@ +insert([ + 'name' => Str::random(10), + 'email' => Str::random(10).'@gmail.com', + 'password' => bcrypt('password'), + ]); + } +} diff --git a/tests/TenantManagerTest.php b/tests/TenantManagerTest.php index f4cf2477..ea24ff0e 100644 --- a/tests/TenantManagerTest.php +++ b/tests/TenantManagerTest.php @@ -106,7 +106,6 @@ class TenantManagerTest extends TestCase public function tenancy_can_be_ended() { $originals = [ - 'databasePDO' => DB::connection()->getPDO(), 'databaseName' => DB::connection()->getDatabaseName(), 'storage_path' => storage_path(), 'storage_root' => Storage::disk('local')->getAdapter()->getPathPrefix(), @@ -139,7 +138,6 @@ class TenantManagerTest extends TestCase public function tenancy_can_be_ended_after_reidentification() { $originals = [ - 'databasePDO' => DB::connection()->getPDO(), 'databaseName' => DB::connection()->getDatabaseName(), 'storage_path' => storage_path(), 'storage_root' => Storage::disk('local')->getAdapter()->getPathPrefix(),