diff --git a/src/Commands/Migrate.php b/src/Commands/Migrate.php index 206aed32..d6c5ca35 100644 --- a/src/Commands/Migrate.php +++ b/src/Commands/Migrate.php @@ -7,7 +7,6 @@ use Stancl\Tenancy\DatabaseManager; use Illuminate\Database\Migrations\Migrator; use Stancl\Tenancy\Traits\HasATenantsOption; use Stancl\Tenancy\Traits\DealsWithMigrations; -use Symfony\Component\Console\Input\InputOption; use Illuminate\Database\Console\Migrations\MigrateCommand; class Migrate extends MigrateCommand @@ -57,5 +56,7 @@ class Migrate extends MigrateCommand // Migrate parent::handle(); }); + + $this->database->disconnect(); } } diff --git a/src/Commands/Rollback.php b/src/Commands/Rollback.php index 24f9a592..8de6dc53 100644 --- a/src/Commands/Rollback.php +++ b/src/Commands/Rollback.php @@ -7,7 +7,6 @@ use Stancl\Tenancy\DatabaseManager; use Illuminate\Database\Migrations\Migrator; use Stancl\Tenancy\Traits\HasATenantsOption; use Stancl\Tenancy\Traits\DealsWithMigrations; -use Symfony\Component\Console\Input\InputOption; use Illuminate\Database\Console\Migrations\RollbackCommand; class Rollback extends RollbackCommand diff --git a/src/Commands/Seed.php b/src/Commands/Seed.php index 7da3181c..16ada831 100644 --- a/src/Commands/Seed.php +++ b/src/Commands/Seed.php @@ -6,7 +6,6 @@ use Illuminate\Console\Command; use Stancl\Tenancy\DatabaseManager; use Stancl\Tenancy\Traits\HasATenantsOption; use Illuminate\Database\Migrations\Migrator; -use Symfony\Component\Console\Input\InputOption; use Illuminate\Database\Console\Seeds\SeedCommand; use Illuminate\Database\ConnectionResolverInterface; diff --git a/src/DatabaseManager.php b/src/DatabaseManager.php index 65b1558b..94fc1d9f 100644 --- a/src/DatabaseManager.php +++ b/src/DatabaseManager.php @@ -9,8 +9,11 @@ use Illuminate\Database\DatabaseManager as BaseDatabaseManager; class DatabaseManager { + public $originalDefaultConnection; + public function __construct(BaseDatabaseManager $database) { + $this->originalDefaultConnection = config('database.default'); $this->database = $database; } @@ -28,8 +31,9 @@ class DatabaseManager public function disconnect() { - $this->database->reconnect('default'); - $this->database->setDefaultConnection('default'); + $default_connection = $this->originalDefaultConnection; + $this->database->reconnect($default_connection); + $this->database->setDefaultConnection($default_connection); } public function create(string $name, string $driver = null) diff --git a/src/TenantManager.php b/src/TenantManager.php index dcc75fa1..d0acedb8 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -170,6 +170,16 @@ class TenantManager return $tenant; } + /** + * Reconnects to the default database. + * + * @return void + */ + public function disconnectDatabase() + { + $this->database->disconnect(); + } + /** * Get all tenants. * diff --git a/src/Traits/BootstrapsTenancy.php b/src/Traits/BootstrapsTenancy.php index afe29aeb..20809a4c 100644 --- a/src/Traits/BootstrapsTenancy.php +++ b/src/Traits/BootstrapsTenancy.php @@ -8,7 +8,7 @@ use Illuminate\Support\Facades\Storage; trait BootstrapsTenancy { - public $oldStoragePaths = []; + public $originalSettings = []; public function bootstrap() { @@ -41,7 +41,7 @@ trait BootstrapsTenancy public function suffixFilesystemRootPaths() { - $old = $this->oldStoragePaths ?: [ + $old = $this->originalSettings ?: [ "storage_disks" => [], "storage_path" => $this->app->storagePath(), ]; @@ -64,6 +64,6 @@ trait BootstrapsTenancy $old['storage_disks'][$disk] = $root; } - $this->oldStoragePaths = $old; + $this->originalSettings = $old; } } diff --git a/src/Traits/DealsWithMigrations.php b/src/Traits/DealsWithMigrations.php index 4c22dd10..155eadf0 100644 --- a/src/Traits/DealsWithMigrations.php +++ b/src/Traits/DealsWithMigrations.php @@ -6,6 +6,6 @@ trait DealsWithMigrations { protected function getMigrationPaths() { - return [database_path('migrations/tenant')]; + return [config('tenancy.migrations_directory')]; } } diff --git a/src/Traits/HasATenantsOption.php b/src/Traits/HasATenantsOption.php index 99dcb1ca..5eadc3d4 100644 --- a/src/Traits/HasATenantsOption.php +++ b/src/Traits/HasATenantsOption.php @@ -2,6 +2,8 @@ namespace Stancl\Tenancy\Traits; +use Symfony\Component\Console\Input\InputOption; + trait HasATenantsOption { protected function getOptions() diff --git a/src/config/tenancy.php b/src/config/tenancy.php index f1d9207f..5004ada1 100644 --- a/src/config/tenancy.php +++ b/src/config/tenancy.php @@ -41,4 +41,5 @@ return [ ], 'queue_database_creation' => false, 'queue_database_deletion' => false, + 'migrations_directory' => database_path('migrations/tenant'), ]; diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index 8dd32a5d..bd059996 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -2,18 +2,65 @@ namespace Stancl\Tenancy\Tests; +use Illuminate\Support\Facades\Artisan; +use Illuminate\Support\Facades\Schema; + class CommandsTest extends TestCase { - /** @test */ - public function migrate_command_works() + public $autoInitTenancy = false; + + public function setUp() { - $this->markTestIncomplete(); + parent::setUp(); + + config(['tenancy.migrations_directory' => database_path('../migrations')]); + } + + /** @test */ + public function migrate_command_doesnt_change_the_db_connection() + { + $old_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName(); + Artisan::call('tenants:migrate'); + $new_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName(); + + $this->assertEquals($old_connection_name, $new_connection_name); + $this->assertNotEquals('tenant', $new_connection_name); + } + + /** @test */ + public function migrate_command_works_without_options() + { + Artisan::call('tenants:migrate'); + $this->assertFalse(Schema::hasTable('users')); + tenancy()->init(); + $this->assertTrue(Schema::hasTable('users')); + } + + /** @test */ + public function migrate_command_works_with_tenants_option() + { + $tenant = tenant()->create('test.localhost'); + Artisan::call('tenants:migrate', [ + '--tenants' => [$tenant['uuid']] + ]); + + $this->assertFalse(Schema::hasTable('users')); + tenancy()->init(); + $this->assertFalse(Schema::hasTable('users')); + + tenancy()->init('test.localhost'); + $this->assertTrue(Schema::hasTable('users')); } /** @test */ public function rollback_command_works() { - $this->markTestIncomplete(); + Artisan::call('tenants:migrate'); + $this->assertFalse(Schema::hasTable('users')); + tenancy()->init(); + $this->assertTrue(Schema::hasTable('users')); + Artisan::call('tenants:rollback'); + $this->assertFalse(Schema::hasTable('users')); } /** @test */ diff --git a/tests/DatabaseManagerTest.php b/tests/DatabaseManagerTest.php new file mode 100644 index 00000000..c96161fd --- /dev/null +++ b/tests/DatabaseManagerTest.php @@ -0,0 +1,20 @@ +connection()->getName(); + tenancy()->init(); + tenancy()->disconnectDatabase(); + $new_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName(); + + $this->assertSame($old_connection_name, $new_connection_name); + $this->assertNotEquals('tenant', $new_connection_name); + } +} diff --git a/tests/HttpKernel.php b/tests/Etc/HttpKernel.php similarity index 98% rename from tests/HttpKernel.php rename to tests/Etc/HttpKernel.php index 18675c48..4b21982e 100644 --- a/tests/HttpKernel.php +++ b/tests/Etc/HttpKernel.php @@ -1,6 +1,6 @@ singleton('Illuminate\Contracts\Http\Kernel', HttpKernel::class); + $app->singleton('Illuminate\Contracts\Http\Kernel', Etc\HttpKernel::class); } public function randomString(int $length = 10)