mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 17:04:04 +00:00
Restructure config
This commit is contained in:
parent
15a7e52208
commit
fbe43fbb04
10 changed files with 57 additions and 47 deletions
|
|
@ -7,18 +7,10 @@ use Stancl\Tenancy\Database\Models\Tenant;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'tenant_model' => Tenant::class,
|
'tenant_model' => Tenant::class,
|
||||||
'domain_model' => Domain::class,
|
|
||||||
'internal_prefix' => 'tenancy_',
|
'internal_prefix' => 'tenancy_',
|
||||||
|
|
||||||
'central_connection' => 'central',
|
|
||||||
'template_tenant_connection' => null,
|
|
||||||
|
|
||||||
'id_generator' => Stancl\Tenancy\UniqueIDGenerators\UUIDGenerator::class,
|
'id_generator' => Stancl\Tenancy\UniqueIDGenerators\UUIDGenerator::class,
|
||||||
|
|
||||||
'custom_columns' => [
|
'domain_model' => Domain::class,
|
||||||
//
|
|
||||||
],
|
|
||||||
|
|
||||||
'central_domains' => [
|
'central_domains' => [
|
||||||
'127.0.0.1',
|
'127.0.0.1',
|
||||||
'localhost',
|
'localhost',
|
||||||
|
|
@ -47,12 +39,40 @@ return [
|
||||||
* Database tenancy config. Used by DatabaseTenancyBootstrapper.
|
* Database tenancy config. Used by DatabaseTenancyBootstrapper.
|
||||||
*/
|
*/
|
||||||
'database' => [
|
'database' => [
|
||||||
|
'central_connection' => 'central',
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connection used as a "template" for the tenant database connection.
|
||||||
|
*/
|
||||||
|
'template_tenant_connection' => null,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tenant database names are created like this:
|
* Tenant database names are created like this:
|
||||||
* prefix + tenant_id + suffix.
|
* prefix + tenant_id + suffix.
|
||||||
*/
|
*/
|
||||||
'prefix' => 'tenant',
|
'prefix' => 'tenant',
|
||||||
'suffix' => '',
|
'suffix' => '',
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TenantDatabaseManagers are classes that handle the creation & deletion of tenant databases.
|
||||||
|
*/
|
||||||
|
'managers' => [
|
||||||
|
'sqlite' => Stancl\Tenancy\TenantDatabaseManagers\SQLiteDatabaseManager::class,
|
||||||
|
'mysql' => Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager::class,
|
||||||
|
'pgsql' => Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager::class,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use this database manager for MySQL to have a DB user created for each tenant database.
|
||||||
|
* You can customize the grants given to these users by changing the $grants property.
|
||||||
|
*/
|
||||||
|
// 'mysql' => Stancl\Tenancy\TenantDatabaseManagers\PermissionControlledMySQLDatabaseManager::class,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disable the pgsql manager above, and enable the one below if you
|
||||||
|
* want to separate tenant DBs by schemas rather than databases.
|
||||||
|
*/
|
||||||
|
// 'pgsql' => Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLSchemaManager::class, // Separate by schema instead of database
|
||||||
|
],
|
||||||
],
|
],
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -131,27 +151,6 @@ return [
|
||||||
'asset_helper_tenancy' => true,
|
'asset_helper_tenancy' => true,
|
||||||
],
|
],
|
||||||
|
|
||||||
/**
|
|
||||||
* TenantDatabaseManagers are classes that handle the creation & deletion of tenant databases.
|
|
||||||
*/
|
|
||||||
'database_managers' => [
|
|
||||||
'sqlite' => Stancl\Tenancy\TenantDatabaseManagers\SQLiteDatabaseManager::class,
|
|
||||||
'mysql' => Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager::class,
|
|
||||||
'pgsql' => Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager::class,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Use this database manager for MySQL to have a DB user created for each tenant database.
|
|
||||||
* You can customize the grants given to these users by changing the $grants property.
|
|
||||||
*/
|
|
||||||
// 'mysql' => Stancl\Tenancy\TenantDatabaseManagers\PermissionControlledMySQLDatabaseManager::class,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Disable the pgsql manager above, and enable the one below if you
|
|
||||||
* want to separate tenant DBs by schemas rather than databases.
|
|
||||||
*/
|
|
||||||
// 'pgsql' => Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLSchemaManager::class, // Separate by schema instead of database
|
|
||||||
],
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Features are classes that provide additional functionality
|
* Features are classes that provide additional functionality
|
||||||
* not needed for tenancy to be bootstrapped. They are run
|
* not needed for tenancy to be bootstrapped. They are run
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,6 @@ trait CentralConnection
|
||||||
{
|
{
|
||||||
public function getConnectionName()
|
public function getConnectionName()
|
||||||
{
|
{
|
||||||
return config('tenancy.central_connection');
|
return config('tenancy.database.central_connection');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -121,6 +121,8 @@ trait HasDataColumn
|
||||||
|
|
||||||
public static function getCustomColums(): array
|
public static function getCustomColums(): array
|
||||||
{
|
{
|
||||||
return array_merge(['id'], config('tenancy.custom_columns'));
|
return [
|
||||||
|
'id',
|
||||||
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -100,8 +100,8 @@ class DatabaseConfig
|
||||||
public function getTemplateConnectionName(): string
|
public function getTemplateConnectionName(): string
|
||||||
{
|
{
|
||||||
return $this->tenant->getInternal('db_connection')
|
return $this->tenant->getInternal('db_connection')
|
||||||
?? config('tenancy.template_tenant_connection')
|
?? config('tenancy.database.template_tenant_connection')
|
||||||
?? config('tenancy.central_connection');
|
?? config('tenancy.database.central_connection');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -150,7 +150,7 @@ class DatabaseConfig
|
||||||
{
|
{
|
||||||
$driver = config("database.connections.{$this->getTemplateConnectionName()}.driver");
|
$driver = config("database.connections.{$this->getTemplateConnectionName()}.driver");
|
||||||
|
|
||||||
$databaseManagers = config('tenancy.database_managers');
|
$databaseManagers = config('tenancy.database.managers');
|
||||||
|
|
||||||
if (! array_key_exists($driver, $databaseManagers)) {
|
if (! array_key_exists($driver, $databaseManagers)) {
|
||||||
throw new DatabaseManagerNotRegisteredException($driver);
|
throw new DatabaseManagerNotRegisteredException($driver);
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ class DatabaseManager
|
||||||
$this->database->purge('tenant');
|
$this->database->purge('tenant');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->setDefaultConnection($this->config->get('tenancy.central_connection'));
|
$this->setDefaultConnection($this->config->get('tenancy.database.central_connection'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,16 @@ class CommandsTest extends TestCase
|
||||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function tearDown(): void
|
||||||
|
{
|
||||||
|
parent::tearDown();
|
||||||
|
|
||||||
|
// Cleanup tenancy config cache
|
||||||
|
if (file_exists(base_path('config/tenancy.php'))) {
|
||||||
|
unlink(base_path('config/tenancy.php'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function migrate_command_doesnt_change_the_db_connection()
|
public function migrate_command_doesnt_change_the_db_connection()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ class DatabasePreparationTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function database_can_be_created_after_tenant_creation()
|
public function database_can_be_created_after_tenant_creation()
|
||||||
{
|
{
|
||||||
config(['tenancy.template_tenant_connection' => 'mysql']);
|
config(['tenancy.database.template_tenant_connection' => 'mysql']);
|
||||||
|
|
||||||
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||||
return $event->tenant;
|
return $event->tenant;
|
||||||
|
|
|
||||||
|
|
@ -27,9 +27,9 @@ class DatabaseUsersTest extends TestCase
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
config([
|
config([
|
||||||
'tenancy.database_managers.mysql' => PermissionControlledMySQLDatabaseManager::class,
|
'tenancy.database.managers.mysql' => PermissionControlledMySQLDatabaseManager::class,
|
||||||
'tenancy.database.suffix' => '',
|
'tenancy.database.suffix' => '',
|
||||||
'tenancy.template_tenant_connection' => 'mysql',
|
'tenancy.database.template_tenant_connection' => 'mysql',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||||
|
|
@ -97,9 +97,9 @@ class DatabaseUsersTest extends TestCase
|
||||||
public function having_existing_databases_without_users_and_switching_to_permission_controlled_mysql_manager_doesnt_break_existing_dbs()
|
public function having_existing_databases_without_users_and_switching_to_permission_controlled_mysql_manager_doesnt_break_existing_dbs()
|
||||||
{
|
{
|
||||||
config([
|
config([
|
||||||
'tenancy.database_managers.mysql' => MySQLDatabaseManager::class,
|
'tenancy.database.managers.mysql' => MySQLDatabaseManager::class,
|
||||||
'tenancy.database.suffix' => '',
|
'tenancy.database.suffix' => '',
|
||||||
'tenancy.template_tenant_connection' => 'mysql',
|
'tenancy.database.template_tenant_connection' => 'mysql',
|
||||||
'tenancy.bootstrappers' => [
|
'tenancy.bootstrappers' => [
|
||||||
DatabaseTenancyBootstrapper::class,
|
DatabaseTenancyBootstrapper::class,
|
||||||
],
|
],
|
||||||
|
|
@ -116,7 +116,7 @@ class DatabaseUsersTest extends TestCase
|
||||||
tenancy()->initialize($tenant); // check if everything works
|
tenancy()->initialize($tenant); // check if everything works
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
|
|
||||||
config(['tenancy.database_managers.mysql' => PermissionControlledMySQLDatabaseManager::class]);
|
config(['tenancy.database.managers.mysql' => PermissionControlledMySQLDatabaseManager::class]);
|
||||||
|
|
||||||
tenancy()->initialize($tenant); // check if everything works
|
tenancy()->initialize($tenant); // check if everything works
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ class TenantDatabaseManagerTest extends TestCase
|
||||||
})->toListener());
|
})->toListener());
|
||||||
|
|
||||||
config()->set([
|
config()->set([
|
||||||
"tenancy.database_managers.$driver" => $databaseManager,
|
"tenancy.database.managers.$driver" => $databaseManager,
|
||||||
'tenancy.internal_prefix' => 'tenancy_',
|
'tenancy.internal_prefix' => 'tenancy_',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
@ -131,7 +131,7 @@ class TenantDatabaseManagerTest extends TestCase
|
||||||
public function schema_manager_uses_schema_to_separate_tenant_dbs()
|
public function schema_manager_uses_schema_to_separate_tenant_dbs()
|
||||||
{
|
{
|
||||||
config([
|
config([
|
||||||
'tenancy.database_managers.pgsql' => \Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLSchemaManager::class,
|
'tenancy.database.managers.pgsql' => \Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLSchemaManager::class,
|
||||||
'tenancy.boostrappers' => [
|
'tenancy.boostrappers' => [
|
||||||
DatabaseTenancyBootstrapper::class,
|
DatabaseTenancyBootstrapper::class,
|
||||||
],
|
],
|
||||||
|
|
@ -179,7 +179,7 @@ class TenantDatabaseManagerTest extends TestCase
|
||||||
public function tenant_database_can_be_created_on_a_foreign_server()
|
public function tenant_database_can_be_created_on_a_foreign_server()
|
||||||
{
|
{
|
||||||
config([
|
config([
|
||||||
'tenancy.database_managers.mysql' => PermissionControlledMySQLDatabaseManager::class,
|
'tenancy.database.managers.mysql' => PermissionControlledMySQLDatabaseManager::class,
|
||||||
'database.connections.mysql2' => [
|
'database.connections.mysql2' => [
|
||||||
'driver' => 'mysql',
|
'driver' => 'mysql',
|
||||||
'host' => 'mysql2', // important line
|
'host' => 'mysql2', // important line
|
||||||
|
|
|
||||||
|
|
@ -76,7 +76,6 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
'--realpath' => true,
|
'--realpath' => true,
|
||||||
'--force' => true,
|
'--force' => true,
|
||||||
],
|
],
|
||||||
'tenancy.storage.connection' => 'central',
|
|
||||||
'tenancy.bootstrappers.redis' => \Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class,
|
'tenancy.bootstrappers.redis' => \Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class,
|
||||||
'queue.connections.central' => [
|
'queue.connections.central' => [
|
||||||
'driver' => 'sync',
|
'driver' => 'sync',
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue