mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 15:34:03 +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 [
|
||||
'tenant_model' => Tenant::class,
|
||||
'domain_model' => Domain::class,
|
||||
'internal_prefix' => 'tenancy_',
|
||||
|
||||
'central_connection' => 'central',
|
||||
'template_tenant_connection' => null,
|
||||
|
||||
'id_generator' => Stancl\Tenancy\UniqueIDGenerators\UUIDGenerator::class,
|
||||
|
||||
'custom_columns' => [
|
||||
//
|
||||
],
|
||||
|
||||
|
||||
'domain_model' => Domain::class,
|
||||
'central_domains' => [
|
||||
'127.0.0.1',
|
||||
'localhost',
|
||||
|
|
@ -47,12 +39,40 @@ return [
|
|||
* Database tenancy config. Used by DatabaseTenancyBootstrapper.
|
||||
*/
|
||||
'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:
|
||||
* prefix + tenant_id + suffix.
|
||||
*/
|
||||
'prefix' => 'tenant',
|
||||
'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,
|
||||
],
|
||||
|
||||
/**
|
||||
* 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
|
||||
* not needed for tenancy to be bootstrapped. They are run
|
||||
|
|
|
|||
|
|
@ -6,6 +6,6 @@ trait CentralConnection
|
|||
{
|
||||
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
|
||||
{
|
||||
return array_merge(['id'], config('tenancy.custom_columns'));
|
||||
return [
|
||||
'id',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -100,8 +100,8 @@ class DatabaseConfig
|
|||
public function getTemplateConnectionName(): string
|
||||
{
|
||||
return $this->tenant->getInternal('db_connection')
|
||||
?? config('tenancy.template_tenant_connection')
|
||||
?? config('tenancy.central_connection');
|
||||
?? config('tenancy.database.template_tenant_connection')
|
||||
?? config('tenancy.database.central_connection');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -150,7 +150,7 @@ class DatabaseConfig
|
|||
{
|
||||
$driver = config("database.connections.{$this->getTemplateConnectionName()}.driver");
|
||||
|
||||
$databaseManagers = config('tenancy.database_managers');
|
||||
$databaseManagers = config('tenancy.database.managers');
|
||||
|
||||
if (! array_key_exists($driver, $databaseManagers)) {
|
||||
throw new DatabaseManagerNotRegisteredException($driver);
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ class DatabaseManager
|
|||
$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);
|
||||
}
|
||||
|
||||
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 */
|
||||
public function migrate_command_doesnt_change_the_db_connection()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ class DatabasePreparationTest extends TestCase
|
|||
/** @test */
|
||||
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) {
|
||||
return $event->tenant;
|
||||
|
|
|
|||
|
|
@ -27,9 +27,9 @@ class DatabaseUsersTest extends TestCase
|
|||
parent::setUp();
|
||||
|
||||
config([
|
||||
'tenancy.database_managers.mysql' => PermissionControlledMySQLDatabaseManager::class,
|
||||
'tenancy.database.managers.mysql' => PermissionControlledMySQLDatabaseManager::class,
|
||||
'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) {
|
||||
|
|
@ -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()
|
||||
{
|
||||
config([
|
||||
'tenancy.database_managers.mysql' => MySQLDatabaseManager::class,
|
||||
'tenancy.database.managers.mysql' => MySQLDatabaseManager::class,
|
||||
'tenancy.database.suffix' => '',
|
||||
'tenancy.template_tenant_connection' => 'mysql',
|
||||
'tenancy.database.template_tenant_connection' => 'mysql',
|
||||
'tenancy.bootstrappers' => [
|
||||
DatabaseTenancyBootstrapper::class,
|
||||
],
|
||||
|
|
@ -116,7 +116,7 @@ class DatabaseUsersTest extends TestCase
|
|||
tenancy()->initialize($tenant); // check if everything works
|
||||
tenancy()->end();
|
||||
|
||||
config(['tenancy.database_managers.mysql' => PermissionControlledMySQLDatabaseManager::class]);
|
||||
config(['tenancy.database.managers.mysql' => PermissionControlledMySQLDatabaseManager::class]);
|
||||
|
||||
tenancy()->initialize($tenant); // check if everything works
|
||||
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ class TenantDatabaseManagerTest extends TestCase
|
|||
})->toListener());
|
||||
|
||||
config()->set([
|
||||
"tenancy.database_managers.$driver" => $databaseManager,
|
||||
"tenancy.database.managers.$driver" => $databaseManager,
|
||||
'tenancy.internal_prefix' => 'tenancy_',
|
||||
]);
|
||||
|
||||
|
|
@ -131,7 +131,7 @@ class TenantDatabaseManagerTest extends TestCase
|
|||
public function schema_manager_uses_schema_to_separate_tenant_dbs()
|
||||
{
|
||||
config([
|
||||
'tenancy.database_managers.pgsql' => \Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLSchemaManager::class,
|
||||
'tenancy.database.managers.pgsql' => \Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLSchemaManager::class,
|
||||
'tenancy.boostrappers' => [
|
||||
DatabaseTenancyBootstrapper::class,
|
||||
],
|
||||
|
|
@ -179,7 +179,7 @@ class TenantDatabaseManagerTest extends TestCase
|
|||
public function tenant_database_can_be_created_on_a_foreign_server()
|
||||
{
|
||||
config([
|
||||
'tenancy.database_managers.mysql' => PermissionControlledMySQLDatabaseManager::class,
|
||||
'tenancy.database.managers.mysql' => PermissionControlledMySQLDatabaseManager::class,
|
||||
'database.connections.mysql2' => [
|
||||
'driver' => 'mysql',
|
||||
'host' => 'mysql2', // important line
|
||||
|
|
|
|||
|
|
@ -76,7 +76,6 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
'--realpath' => true,
|
||||
'--force' => true,
|
||||
],
|
||||
'tenancy.storage.connection' => 'central',
|
||||
'tenancy.bootstrappers.redis' => \Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class,
|
||||
'queue.connections.central' => [
|
||||
'driver' => 'sync',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue