Skip to content

Commit

Permalink
Make DB creation optional (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
stancl authored Feb 26, 2020
1 parent 5bb743f commit 98ce0ee
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions assets/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
13 changes: 12 additions & 1 deletion src/TenantManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
34 changes: 34 additions & 0 deletions tests/TenantManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 98ce0ee

Please sign in to comment.