Skip to content

Commit

Permalink
Respect custom columns during tenant creation (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
stancl committed Oct 19, 2019
1 parent 479df83 commit f489aba
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/StorageDrivers/Database/DatabaseStorageDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ public function getTenantIdByDomain(string $domain): ?string
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) {
Expand Down
18 changes: 18 additions & 0 deletions src/StorageDrivers/Database/TenantModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ public static function customColumns()
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();
Expand Down
34 changes: 30 additions & 4 deletions tests/TenantStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function retrieving_data_without_cache_works()
/** @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();
}

Expand All @@ -153,12 +153,38 @@ public function custom_columns_work_with_db_storage_driver()
'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);
}
}

0 comments on commit f489aba

Please sign in to comment.