From cc5611b24d74cd0b00c05919143eece404abf956 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Thu, 17 Oct 2019 13:38:09 +0200 Subject: [PATCH] Use only one config for queueing --- assets/config.php | 5 ++--- src/DatabaseManager.php | 4 ++-- src/TenantManager.php | 28 +++++++++++++--------------- tests/TenantManagerTest.php | 18 +++++++++++------- 4 files changed, 28 insertions(+), 27 deletions(-) diff --git a/assets/config.php b/assets/config.php index b36bdbba..a6b84e68 100644 --- a/assets/config.php +++ b/assets/config.php @@ -89,10 +89,9 @@ return [ // 'paypal_api_key' => 'services.paypal.api_key', ], 'home_url' => '/app', - 'migrate_after_creation' => false, // run migrations after creating a tenant - 'queue_automatic_migration' => false, // queue the automatic post-tenant-creation migrations - 'delete_database_after_tenant_deletion' => false, // delete the tenant's database after deleting the tenant 'queue_database_creation' => false, + 'migrate_after_creation' => false, // run migrations after creating a tenant 'queue_database_deletion' => false, + 'delete_database_after_tenant_deletion' => false, // delete the tenant's database after deleting the tenant 'unique_id_generator' => Stancl\Tenancy\UniqueIDGenerators\UUIDGenerator::class, ]; diff --git a/src/DatabaseManager.php b/src/DatabaseManager.php index 6d90f218..640b6deb 100644 --- a/src/DatabaseManager.php +++ b/src/DatabaseManager.php @@ -126,14 +126,14 @@ class DatabaseManager * Create a database for a tenant. * * @param Tenant $tenant - * @param ShouldQueue[]|callable[] $afterCreating + * @param \Illuminate\Contracts\Queue\ShouldQueue[]|callable[] $afterCreating * @return void */ public function createDatabase(Tenant $tenant, array $afterCreating = []) { $database = $tenant->getDatabaseName(); $manager = $this->getTenantDatabaseManager($tenant); - + if ($this->app['config']['tenancy.queue_database_creation'] ?? false) { QueuedTenantDatabaseCreator::withChain($afterCreating)->dispatch($manager, $database); } else { diff --git a/src/TenantManager.php b/src/TenantManager.php index f5fcb7a7..e0562932 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -63,21 +63,19 @@ class TenantManager $this->storage->createTenant($tenant); + /** @var \Illuminate\Contracts\Queue\ShouldQueue[]|callable[] $afterCreating */ $afterCreating = []; + if ($this->shouldMigrateAfterCreation()) { - if ($this->shouldQueueMigration()) { - $afterCreating = [ - new QueuedTenantDatabaseMigrator($tenant), - ]; - } else { - $afterCreating = [ - function () use ($tenant) { - $this->artisan->call('tenants:migrate', [ - '--tenants' => [$tenant['id']], - ]); - }, - ]; - } + $afterCreating += $this->databaseCreationQueued() ? [ + new QueuedTenantDatabaseMigrator($tenant), + ] : [ + function () use ($tenant) { + $this->artisan->call('tenants:migrate', [ + '--tenants' => [$tenant['id']], + ]); + }, + ]; } $this->database->createDatabase($tenant, $afterCreating); @@ -323,9 +321,9 @@ class TenantManager return $this->app['config']['tenancy.migrate_after_creation'] ?? false; } - public function shouldQueueMigration(): bool + public function databaseCreationQueued(): bool { - return $this->app['config']['tenancy.queue_automatic_migration'] ?? false; + return $this->app['config']['tenancy.queue_database_creation'] ?? false; } public function shouldDeleteDatabase(): bool diff --git a/tests/TenantManagerTest.php b/tests/TenantManagerTest.php index f109236b..aaf69806 100644 --- a/tests/TenantManagerTest.php +++ b/tests/TenantManagerTest.php @@ -10,6 +10,7 @@ use Illuminate\Support\Facades\Storage; use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException; use Stancl\Tenancy\Exceptions\TenantDoesNotExistException; use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException; +use Stancl\Tenancy\Jobs\QueuedTenantDatabaseCreator; use Stancl\Tenancy\Jobs\QueuedTenantDatabaseMigrator; use Stancl\Tenancy\Tenant; use Stancl\Tenancy\TenantManager; @@ -249,23 +250,26 @@ class TenantManagerTest extends TestCase } /** @test */ - public function automigration_can_be_queued() + public function automigration_is_be_queued() { Queue::fake(); config([ + 'tenancy.queue_database_creation' => true, 'tenancy.migrate_after_creation' => true, - 'tenancy.queue_automatic_migration' => true, ]); $tenant = Tenant::new()->save(); - tenancy()->initialize($tenant); - Queue::assertPushed(QueuedTenantDatabaseMigrator::class); + Queue::assertPushedWithChain(QueuedTenantDatabaseCreator::class, [ + QueuedTenantDatabaseMigrator::class + ]); - $this->assertFalse(\Schema::hasTable('users')); - (new QueuedTenantDatabaseMigrator($tenant))->handle(); - $this->assertTrue(\Schema::hasTable('users')); + // foreach (Queue::pushedJobs() as $job) { + // $job[0]['job']->handle(); // this doesn't execute the chained job + // } + // tenancy()->initialize($tenant); + // $this->assertTrue(\Schema::hasTable('users')); } /** @test */