mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 15:54:03 +00:00
Respect custom columns during tenant creation (#191)
This commit is contained in:
parent
479df83027
commit
f489aba819
3 changed files with 51 additions and 5 deletions
|
|
@ -103,7 +103,9 @@ class DatabaseStorageDriver implements StorageDriver
|
|||
public function createTenant(Tenant $tenant): void
|
||||
{
|
||||
$this->centralDatabase->transaction(function () use ($tenant) {
|
||||
Tenants::create(['id' => $tenant->id, 'data' => json_encode($tenant->data)])->toArray();
|
||||
Tenants::create(array_merge(Tenants::encodeData($tenant->data), [
|
||||
'id' => $tenant->id,
|
||||
]))->toArray();
|
||||
|
||||
$domainData = [];
|
||||
foreach ($tenant->domains as $domain) {
|
||||
|
|
|
|||
|
|
@ -33,6 +33,24 @@ class TenantModel extends Model
|
|||
return config('tenancy.storage_drivers.db.custom_columns', []);
|
||||
}
|
||||
|
||||
public static function encodeData(array $data)
|
||||
{
|
||||
$result = [];
|
||||
$jsonData = [];
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
if (in_array($key, static::customColumns(), true)) {
|
||||
$result[$key] = $value;
|
||||
} else {
|
||||
$jsonData[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$result['data'] = $jsonData ? json_encode($jsonData) : '{}';
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function getAllTenants(array $ids)
|
||||
{
|
||||
$tenants = $ids ? static::findMany($ids) : static::all();
|
||||
|
|
|
|||
|
|
@ -137,7 +137,7 @@ class TenantStorageTest extends TestCase
|
|||
/** @test */
|
||||
public function custom_columns_work_with_db_storage_driver()
|
||||
{
|
||||
if (config('tenancy.storage_driver') != 'Stancl\Tenancy\StorageDrivers\DatabaseStorageDriver') {
|
||||
if (config('tenancy.storage_driver') != 'db') {
|
||||
$this->markTestSkipped();
|
||||
}
|
||||
|
||||
|
|
@ -153,12 +153,38 @@ class TenantStorageTest extends TestCase
|
|||
'foo',
|
||||
]]);
|
||||
|
||||
tenant()->create(['foo.localhost']);
|
||||
tenancy()->create(['foo.localhost']);
|
||||
tenancy()->init('foo.localhost');
|
||||
|
||||
tenant()->put(['foo' => 'bar', 'abc' => 'xyz']);
|
||||
$this->assertSame(['bar', 'xyz'], tenant()->get(['foo', 'abc']));
|
||||
$this->assertSame(['foo' => 'bar', 'abc' => 'xyz'], tenant()->get(['foo', 'abc']));
|
||||
|
||||
$this->assertSame('bar', DB::connection('central')->table('tenants')->where('id', tenant('id'))->first()->foo);
|
||||
$this->assertSame('bar', \DB::connection('central')->table('tenants')->where('id', tenant('id'))->first()->foo);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function custom_columns_can_be_used_on_tenant_create()
|
||||
{
|
||||
if (config('tenancy.storage_driver') != 'db') {
|
||||
$this->markTestSkipped();
|
||||
}
|
||||
|
||||
tenancy()->endTenancy();
|
||||
|
||||
$this->loadMigrationsFrom([
|
||||
'--path' => __DIR__ . '/Etc',
|
||||
'--database' => 'central',
|
||||
]);
|
||||
config(['database.default' => 'sqlite']); // fix issue caused by loadMigrationsFrom
|
||||
|
||||
config(['tenancy.storage_drivers.db.custom_columns' => [
|
||||
'foo',
|
||||
]]);
|
||||
|
||||
tenancy()->create(['foo.localhost'], ['foo' => 'bar', 'abc' => 'xyz']);
|
||||
tenancy()->init('foo.localhost');
|
||||
|
||||
$this->assertSame(['foo' => 'bar', 'abc' => 'xyz'], tenant()->get(['foo', 'abc']));
|
||||
$this->assertSame('bar', \DB::connection('central')->table('tenants')->where('id', tenant('id'))->first()->foo);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue