mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-04 20:54:03 +00:00
tearDown() no such table migrations
This commit is contained in:
parent
f7ca2937fd
commit
3036980ea2
4 changed files with 45 additions and 14 deletions
|
|
@ -7,6 +7,10 @@ use Stancl\Tenancy\Interfaces\StorageDriver;
|
||||||
|
|
||||||
class DatabaseStorageDriver implements StorageDriver
|
class DatabaseStorageDriver implements StorageDriver
|
||||||
{
|
{
|
||||||
|
public $useJson = false;
|
||||||
|
|
||||||
|
// todo use an instance of tenant model?
|
||||||
|
|
||||||
public function identifyTenant(string $domain): array
|
public function identifyTenant(string $domain): array
|
||||||
{
|
{
|
||||||
$id = $this->getTenantIdByDomain($domain);
|
$id = $this->getTenantIdByDomain($domain);
|
||||||
|
|
@ -50,13 +54,7 @@ class DatabaseStorageDriver implements StorageDriver
|
||||||
|
|
||||||
public function getAllTenants(array $uuids = []): array
|
public function getAllTenants(array $uuids = []): array
|
||||||
{
|
{
|
||||||
if ($uuids) {
|
return Tenant::getAllTenants($uuids)->toArray();
|
||||||
$tenants = Tenant::find($uuids);
|
|
||||||
} else {
|
|
||||||
$tenants = Tenant::all();
|
|
||||||
}
|
|
||||||
|
|
||||||
return $tenants->toArray();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get(string $uuid, string $key)
|
public function get(string $uuid, string $key)
|
||||||
|
|
|
||||||
|
|
@ -18,19 +18,34 @@ class Tenant extends Model
|
||||||
*/
|
*/
|
||||||
private $dataObject;
|
private $dataObject;
|
||||||
|
|
||||||
public function dataColumn()
|
public static function dataColumn()
|
||||||
{
|
{
|
||||||
return config('tenancy.storage.db.data_column', 'data');
|
return config('tenancy.storage.db.data_column', 'data');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function customColumns()
|
public static function customColumns()
|
||||||
{
|
{
|
||||||
return config('tenancy.storage.db.custom_columns', []);
|
return config('tenancy.storage.db.custom_columns', []);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getConnectionName()
|
public function getConnectionName()
|
||||||
{
|
{
|
||||||
return config('tenancy.storage.db.connection');
|
return config('tenancy.storage.db.connection', 'central');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getAllTenants(array $uuids)
|
||||||
|
{
|
||||||
|
$tenants = $uuids ? static::findMany($uuids) : static::all();
|
||||||
|
|
||||||
|
return $tenants->map(function ($tenant) {
|
||||||
|
$tenant = (array) $tenant->attributes;
|
||||||
|
foreach (json_decode($tenant[static::dataColumn()], true) as $key => $value) {
|
||||||
|
$tenant[$key] = $value;
|
||||||
|
}
|
||||||
|
unset($tenant[static::dataColumn()]); // todo what if 'data' key is stored in tenant storage?
|
||||||
|
|
||||||
|
return $tenant;
|
||||||
|
})->toBase();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getFromData(string $key)
|
public function getFromData(string $key)
|
||||||
|
|
|
||||||
|
|
@ -86,7 +86,10 @@ final class TenantManager
|
||||||
throw new \Exception("Domain $domain is already occupied by tenant $id.");
|
throw new \Exception("Domain $domain is already occupied by tenant $id.");
|
||||||
}
|
}
|
||||||
|
|
||||||
$tenant = $this->jsonDecodeArrayValues($this->storage->createTenant($domain, (string) \Webpatser\Uuid\Uuid::generate(1, $domain)));
|
$tenant = $this->storage->createTenant($domain, (string) \Webpatser\Uuid\Uuid::generate(1, $domain));
|
||||||
|
if ($this->useJson()) {
|
||||||
|
$tenant = $this->jsonDecodeArrayValues($tenant);
|
||||||
|
}
|
||||||
|
|
||||||
if ($data) {
|
if ($data) {
|
||||||
$this->put($data, null, $tenant['uuid']);
|
$this->put($data, null, $tenant['uuid']);
|
||||||
|
|
@ -227,10 +230,15 @@ final class TenantManager
|
||||||
public function all($uuids = [])
|
public function all($uuids = [])
|
||||||
{
|
{
|
||||||
$uuids = (array) $uuids;
|
$uuids = (array) $uuids;
|
||||||
|
$tenants = $this->storage->getAllTenants($uuids);
|
||||||
|
|
||||||
return collect(array_map(function ($tenant_array) {
|
if ($this->useJson()) {
|
||||||
return $this->jsonDecodeArrayValues($tenant_array);
|
$tenants = array_map(function ($tenant_array) {
|
||||||
}, $this->storage->getAllTenants($uuids)));
|
return $this->jsonDecodeArrayValues($tenant_array);
|
||||||
|
}, $tenants);
|
||||||
|
}
|
||||||
|
|
||||||
|
return collect($tenants);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -336,6 +344,15 @@ final class TenantManager
|
||||||
return $array;
|
return $array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function useJson()
|
||||||
|
{
|
||||||
|
if (property_exists($this->storage, 'useJson') && $this->storage->useJson === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the identified tenant's attribute(s).
|
* Return the identified tenant's attribute(s).
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
Redis::connection('cache')->flushdb();
|
Redis::connection('cache')->flushdb();
|
||||||
|
|
||||||
config(['database.default' => 'central']);
|
config(['database.default' => 'central']);
|
||||||
|
$this->loadLaravelMigrations();
|
||||||
$this->loadMigrationsFrom(realpath(__DIR__ . '/../src/assets/migrations'));
|
$this->loadMigrationsFrom(realpath(__DIR__ . '/../src/assets/migrations'));
|
||||||
|
|
||||||
if ($this->autoCreateTenant) {
|
if ($this->autoCreateTenant) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue