mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 13:54:04 +00:00
tenancy_db_connection tenant config test
This commit is contained in:
parent
c252e0c405
commit
b95b6cadca
2 changed files with 52 additions and 2 deletions
|
|
@ -132,8 +132,9 @@ class DatabaseConfig
|
||||||
|
|
||||||
if ($this->manager() instanceof Contracts\ManagesDatabaseUsers) {
|
if ($this->manager() instanceof Contracts\ManagesDatabaseUsers) {
|
||||||
// We're removing the username and password because user with these credentials is not created yet
|
// We're removing the username and password because user with these credentials is not created yet
|
||||||
unset($config['username']);
|
// If you need to provide username and password when using PermissionControlledMySQLDatabaseManager,
|
||||||
unset($config['password']);
|
// consider creating a new connection and use it as `tenancy_db_connection` tenant config key
|
||||||
|
unset($config['username'], $config['password']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! $config) {
|
if (! $config) {
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
|
@ -266,6 +267,54 @@ test('tenant database can be created on a foreign server by using the host from
|
||||||
expect($manager->databaseExists($name))->toBeTrue();
|
expect($manager->databaseExists($name))->toBeTrue();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('tenant database can be created using host and credentials from the tenant config', closure: function () {
|
||||||
|
config([
|
||||||
|
'tenancy.database.managers.mysql' => PermissionControlledMySQLDatabaseManager::class,
|
||||||
|
'tenancy.database.template_tenant_connection' => 'mysql',
|
||||||
|
'database.connections.mysql2' => [
|
||||||
|
'driver' => 'mysql',
|
||||||
|
'host' => 'mysql2',
|
||||||
|
'port' => 3306,
|
||||||
|
'database' => 'main',
|
||||||
|
'username' => 'root',
|
||||||
|
'password' => 'password',
|
||||||
|
'unix_socket' => env('DB_SOCKET', ''),
|
||||||
|
'charset' => 'utf8mb4',
|
||||||
|
'collation' => 'utf8mb4_unicode_ci',
|
||||||
|
'prefix' => '',
|
||||||
|
'prefix_indexes' => true,
|
||||||
|
'strict' => true,
|
||||||
|
'engine' => null,
|
||||||
|
'options' => extension_loaded('pdo_mysql') ? array_filter([
|
||||||
|
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
|
||||||
|
]) : [],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Create a new and random database user with privileges to use with mysql2 connection
|
||||||
|
$username = 'dbuser' . Str::random(4);
|
||||||
|
DB::statement("CREATE USER `{$username}`@`%` IDENTIFIED BY 'password'");
|
||||||
|
DB::connection('mysql2')->statement("GRANT ALL PRIVILEGES ON *.* TO `{$username}`@`%` identified by 'password' WITH GRANT OPTION;");
|
||||||
|
DB::connection('mysql2')->statement("FLUSH PRIVILEGES;");
|
||||||
|
config(['database.connections.mysql2.username' => $username]);
|
||||||
|
|
||||||
|
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||||
|
return $event->tenant;
|
||||||
|
})->toListener());
|
||||||
|
|
||||||
|
$name = 'foo' . Str::random(8);
|
||||||
|
$tenant = Tenant::create([
|
||||||
|
'tenancy_db_name' => $name,
|
||||||
|
'tenancy_db_connection' => 'mysql2',
|
||||||
|
]);
|
||||||
|
|
||||||
|
/** @var MySQLDatabaseManager $manager */
|
||||||
|
$manager = $tenant->database()->manager();
|
||||||
|
|
||||||
|
//$manager->setConnection('mysql2');
|
||||||
|
expect($manager->databaseExists($name))->toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
test('tenant database can be created on a foreign server by using the username and password from tenant config', function () {
|
test('tenant database can be created on a foreign server by using the username and password from tenant config', function () {
|
||||||
config([
|
config([
|
||||||
'tenancy.database.managers.mysql' => MySQLDatabaseManager::class,
|
'tenancy.database.managers.mysql' => MySQLDatabaseManager::class,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue