From 98ce0ee29468e0b6c0519393ad096fe8a8aa267c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 26 Feb 2020 08:49:08 +0100 Subject: [PATCH] Make DB creation optional (#299) --- assets/config.php | 1 + src/TenantManager.php | 13 ++++++++++++- tests/TenantManagerTest.php | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/assets/config.php b/assets/config.php index 2657dfb6a..9ff317eda 100644 --- a/assets/config.php +++ b/assets/config.php @@ -91,6 +91,7 @@ // 'paypal_api_key' => 'services.paypal.api_key', ], 'home_url' => '/app', + 'create_database' => true, 'queue_database_creation' => false, 'migrate_after_creation' => false, // run migrations after creating a tenant 'migration_parameters' => [ diff --git a/src/TenantManager.php b/src/TenantManager.php index 2128da5ad..d0d8b4282 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -98,7 +98,9 @@ public function createTenant(Tenant $tenant): self }; } - $this->database->createDatabase($tenant, $afterCreating); + if ($this->shouldCreateDatabase($tenant)) { + $this->database->createDatabase($tenant, $afterCreating); + } $this->event('tenant.created', $tenant); @@ -378,6 +380,15 @@ public function tenancyBootstrappers($except = []): array return array_diff_key($this->app['config']['tenancy.bootstrappers'], array_flip($except)); } + public function shouldCreateDatabase(Tenant $tenant): bool + { + if (array_key_exists('_tenancy_create_database', $tenant->data)) { + return $tenant->data['_tenancy_create_database']; + } + + return $this->app['config']['tenancy.create_database'] ?? true; + } + public function shouldMigrateAfterCreation(): bool { return $this->app['config']['tenancy.migrate_after_creation'] ?? false; diff --git a/tests/TenantManagerTest.php b/tests/TenantManagerTest.php index c3df859c3..8e78a6079 100644 --- a/tests/TenantManagerTest.php +++ b/tests/TenantManagerTest.php @@ -345,4 +345,38 @@ public function tenant_creating_hook_can_be_used_to_modify_tenants_data() $this->assertArrayHasKey('foo', $tenant->data); $this->assertArrayHasKey('abc123', $tenant->data); } + + /** @test */ + public function database_creation_can_be_disabled() + { + config(['tenancy.create_database' => false]); + + tenancy()->hook('database.creating', function () { + $this->fail(); + }); + + $tenant = Tenant::new()->save(); + + $this->assertTrue(true); + } + + /** @test */ + public function database_creation_can_be_disabled_for_specific_tenants() + { + config(['tenancy.create_database' => true]); + + tenancy()->hook('database.creating', function () { + $this->assertTrue(true); + }); + + $tenant = Tenant::new()->save(); + + tenancy()->hook('database.creating', function () { + $this->fail(); + }); + + $tenant2 = Tenant::new()->withData([ + '_tenancy_create_database' => false, + ])->save(); + } }