Skip to content

Commit f489aba

Browse files
authored
Respect custom columns during tenant creation (#191)
1 parent 479df83 commit f489aba

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

src/StorageDrivers/Database/DatabaseStorageDriver.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ public function getTenantIdByDomain(string $domain): ?string
103103
public function createTenant(Tenant $tenant): void
104104
{
105105
$this->centralDatabase->transaction(function () use ($tenant) {
106-
Tenants::create(['id' => $tenant->id, 'data' => json_encode($tenant->data)])->toArray();
106+
Tenants::create(array_merge(Tenants::encodeData($tenant->data), [
107+
'id' => $tenant->id,
108+
]))->toArray();
107109

108110
$domainData = [];
109111
foreach ($tenant->domains as $domain) {

src/StorageDrivers/Database/TenantModel.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ public static function customColumns()
3333
return config('tenancy.storage_drivers.db.custom_columns', []);
3434
}
3535

36+
public static function encodeData(array $data)
37+
{
38+
$result = [];
39+
$jsonData = [];
40+
41+
foreach ($data as $key => $value) {
42+
if (in_array($key, static::customColumns(), true)) {
43+
$result[$key] = $value;
44+
} else {
45+
$jsonData[$key] = $value;
46+
}
47+
}
48+
49+
$result['data'] = $jsonData ? json_encode($jsonData) : '{}';
50+
51+
return $result;
52+
}
53+
3654
public static function getAllTenants(array $ids)
3755
{
3856
$tenants = $ids ? static::findMany($ids) : static::all();

tests/TenantStorageTest.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function retrieving_data_without_cache_works()
137137
/** @test */
138138
public function custom_columns_work_with_db_storage_driver()
139139
{
140-
if (config('tenancy.storage_driver') != 'Stancl\Tenancy\StorageDrivers\DatabaseStorageDriver') {
140+
if (config('tenancy.storage_driver') != 'db') {
141141
$this->markTestSkipped();
142142
}
143143

@@ -153,12 +153,38 @@ public function custom_columns_work_with_db_storage_driver()
153153
'foo',
154154
]]);
155155

156-
tenant()->create(['foo.localhost']);
156+
tenancy()->create(['foo.localhost']);
157157
tenancy()->init('foo.localhost');
158158

159159
tenant()->put(['foo' => 'bar', 'abc' => 'xyz']);
160-
$this->assertSame(['bar', 'xyz'], tenant()->get(['foo', 'abc']));
160+
$this->assertSame(['foo' => 'bar', 'abc' => 'xyz'], tenant()->get(['foo', 'abc']));
161161

162-
$this->assertSame('bar', DB::connection('central')->table('tenants')->where('id', tenant('id'))->first()->foo);
162+
$this->assertSame('bar', \DB::connection('central')->table('tenants')->where('id', tenant('id'))->first()->foo);
163+
}
164+
165+
/** @test */
166+
public function custom_columns_can_be_used_on_tenant_create()
167+
{
168+
if (config('tenancy.storage_driver') != 'db') {
169+
$this->markTestSkipped();
170+
}
171+
172+
tenancy()->endTenancy();
173+
174+
$this->loadMigrationsFrom([
175+
'--path' => __DIR__ . '/Etc',
176+
'--database' => 'central',
177+
]);
178+
config(['database.default' => 'sqlite']); // fix issue caused by loadMigrationsFrom
179+
180+
config(['tenancy.storage_drivers.db.custom_columns' => [
181+
'foo',
182+
]]);
183+
184+
tenancy()->create(['foo.localhost'], ['foo' => 'bar', 'abc' => 'xyz']);
185+
tenancy()->init('foo.localhost');
186+
187+
$this->assertSame(['foo' => 'bar', 'abc' => 'xyz'], tenant()->get(['foo', 'abc']));
188+
$this->assertSame('bar', \DB::connection('central')->table('tenants')->where('id', tenant('id'))->first()->foo);
163189
}
164190
}

0 commit comments

Comments
 (0)