mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 17:04:04 +00:00
Get multiple envs (redis/db) to run correctly
This commit is contained in:
parent
9c9858e97e
commit
8565cc7acc
19 changed files with 31 additions and 35 deletions
|
|
@ -56,7 +56,7 @@ class Migrate extends MigrateCommand
|
||||||
// See Illuminate\Database\Migrations\DatabaseMigrationRepository::getConnection.
|
// See Illuminate\Database\Migrations\DatabaseMigrationRepository::getConnection.
|
||||||
// Database connections are cached by Illuminate\Database\ConnectionResolver.
|
// Database connections are cached by Illuminate\Database\ConnectionResolver.
|
||||||
$this->input->setOption('database', 'tenant');
|
$this->input->setOption('database', 'tenant');
|
||||||
tenancy()->initialize($tenant); // todo test that this works with multiple tenants with MySQL
|
tenancy()->initialize($tenant); // todo2 test that this works with multiple tenants with MySQL
|
||||||
|
|
||||||
// Migrate
|
// Migrate
|
||||||
parent::handle();
|
parent::handle();
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ use Stancl\Tenancy\Tenant;
|
||||||
|
|
||||||
interface TenancyBootstrapper
|
interface TenancyBootstrapper
|
||||||
{
|
{
|
||||||
public function start(Tenant $tenant); // todo TenantManager instead of Tenant
|
public function start(Tenant $tenant); // todo2 TenantManager instead of Tenant
|
||||||
|
|
||||||
public function end();
|
public function end();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -129,7 +129,7 @@ class DatabaseManager
|
||||||
|
|
||||||
protected function getTenantDatabaseManager(Tenant $tenant): TenantDatabaseManager
|
protected function getTenantDatabaseManager(Tenant $tenant): TenantDatabaseManager
|
||||||
{
|
{
|
||||||
// todo this shouldn't have to create a connection
|
// todo2 this shouldn't have to create a connection
|
||||||
$this->createTenantConnection($tenant->getDatabaseName(), $tenant->getConnectionName());
|
$this->createTenantConnection($tenant->getDatabaseName(), $tenant->getConnectionName());
|
||||||
$driver = $this->getDriver($tenant->getConnectionName());
|
$driver = $this->getDriver($tenant->getConnectionName());
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ namespace Stancl\Tenancy\Facades;
|
||||||
use Illuminate\Support\Facades\Facade;
|
use Illuminate\Support\Facades\Facade;
|
||||||
use Stancl\Tenancy\Tenant as Tenant;
|
use Stancl\Tenancy\Tenant as Tenant;
|
||||||
|
|
||||||
// todo rename to CurrentTenant?
|
// todo2 rename to CurrentTenant?
|
||||||
class TenantFacade extends Facade
|
class TenantFacade extends Facade
|
||||||
{
|
{
|
||||||
protected static function getFacadeAccessor()
|
protected static function getFacadeAccessor()
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ class TelescopeTags implements Feature
|
||||||
if (in_array('tenancy', optional(request()->route())->middleware() ?? [])) {
|
if (in_array('tenancy', optional(request()->route())->middleware() ?? [])) {
|
||||||
$tags = array_merge($tags, [
|
$tags = array_merge($tags, [
|
||||||
'tenant:' . tenant('id'),
|
'tenant:' . tenant('id'),
|
||||||
// 'domain:' . tenant('domain'), todo?
|
// todo2 domain?
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ use Stancl\Tenancy\Tenant;
|
||||||
|
|
||||||
class DatabaseStorageDriver implements StorageDriver
|
class DatabaseStorageDriver implements StorageDriver
|
||||||
{
|
{
|
||||||
// todo write tests verifying that data is decoded and added to the array
|
// todo2 write tests verifying that data is decoded and added to the array
|
||||||
|
|
||||||
/** @var Application */
|
/** @var Application */
|
||||||
protected $app;
|
protected $app;
|
||||||
|
|
@ -47,7 +47,7 @@ class DatabaseStorageDriver implements StorageDriver
|
||||||
|
|
||||||
public function ensureTenantCanBeCreated(Tenant $tenant): void
|
public function ensureTenantCanBeCreated(Tenant $tenant): void
|
||||||
{
|
{
|
||||||
// todo test this
|
// todo2 test this
|
||||||
if (Tenants::find($tenant->id)) {
|
if (Tenants::find($tenant->id)) {
|
||||||
throw new TenantWithThisIdAlreadyExistsException($tenant->id);
|
throw new TenantWithThisIdAlreadyExistsException($tenant->id);
|
||||||
}
|
}
|
||||||
|
|
@ -85,7 +85,7 @@ class DatabaseStorageDriver implements StorageDriver
|
||||||
public function updateTenant(Tenant $tenant): void
|
public function updateTenant(Tenant $tenant): void
|
||||||
{
|
{
|
||||||
Tenant::find($tenant->id)->putMany($tenant->data);
|
Tenant::find($tenant->id)->putMany($tenant->data);
|
||||||
// todo update domains
|
// todo1 update domains
|
||||||
}
|
}
|
||||||
|
|
||||||
public function deleteTenant(Tenant $tenant): void
|
public function deleteTenant(Tenant $tenant): void
|
||||||
|
|
|
||||||
|
|
@ -10,10 +10,10 @@ use Stancl\Tenancy\Contracts\StorageDriver;
|
||||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
|
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
|
||||||
use Stancl\Tenancy\Tenant;
|
use Stancl\Tenancy\Tenant;
|
||||||
|
|
||||||
// todo transactions instead of pipelines?
|
// todo2 transactions instead of pipelines?
|
||||||
class RedisStorageDriver implements StorageDriver
|
class RedisStorageDriver implements StorageDriver
|
||||||
{
|
{
|
||||||
// todo json encoding?
|
// todo2 json encoding?
|
||||||
|
|
||||||
/** @var Application */
|
/** @var Application */
|
||||||
protected $app;
|
protected $app;
|
||||||
|
|
@ -49,7 +49,7 @@ class RedisStorageDriver implements StorageDriver
|
||||||
|
|
||||||
public function ensureTenantCanBeCreated(Tenant $tenant): void
|
public function ensureTenantCanBeCreated(Tenant $tenant): void
|
||||||
{
|
{
|
||||||
// todo
|
// todo2
|
||||||
}
|
}
|
||||||
|
|
||||||
public function findByDomain(string $domain): Tenant
|
public function findByDomain(string $domain): Tenant
|
||||||
|
|
@ -76,7 +76,7 @@ class RedisStorageDriver implements StorageDriver
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = array_combine($keys, $values);
|
$data = array_combine($keys, $values);
|
||||||
$domains = []; // todo
|
$domains = []; // todo2
|
||||||
|
|
||||||
return Tenant::fromStorage($data)->withDomains($domains);
|
return Tenant::fromStorage($data)->withDomains($domains);
|
||||||
}
|
}
|
||||||
|
|
@ -107,7 +107,7 @@ class RedisStorageDriver implements StorageDriver
|
||||||
$pipe->hmset("domains:$domain", 'tenant_id', $tenant->id);
|
$pipe->hmset("domains:$domain", 'tenant_id', $tenant->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo deleted domains
|
// todo2 deleted domains
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -124,7 +124,7 @@ class RedisStorageDriver implements StorageDriver
|
||||||
|
|
||||||
public function all(array $ids = []): array
|
public function all(array $ids = []): array
|
||||||
{
|
{
|
||||||
// todo $this->redis->pipeline() - return?
|
// todo2 $this->redis->pipeline()
|
||||||
$hashes = array_map(function ($hash) {
|
$hashes = array_map(function ($hash) {
|
||||||
return "tenants:{$hash}";
|
return "tenants:{$hash}";
|
||||||
}, $ids);
|
}, $ids);
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper
|
||||||
|
|
||||||
public function start(Tenant $tenant)
|
public function start(Tenant $tenant)
|
||||||
{
|
{
|
||||||
// todo revisit this
|
// todo2 revisit this
|
||||||
$suffix = $this->app['config']['tenancy.filesystem.suffix_base'] . $tenant->id;
|
$suffix = $this->app['config']['tenancy.filesystem.suffix_base'] . $tenant->id;
|
||||||
|
|
||||||
// storage_path()
|
// storage_path()
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
|
||||||
'tenant_id' => $id,
|
'tenant_id' => $id,
|
||||||
'tags' => [
|
'tags' => [
|
||||||
"tenant:$id",
|
"tenant:$id",
|
||||||
// todo domain?
|
// todo2 domain
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,9 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
$this->mergeConfigFrom(__DIR__ . '/../assets/config.php', 'tenancy');
|
$this->mergeConfigFrom(__DIR__ . '/../assets/config.php', 'tenancy');
|
||||||
|
|
||||||
$this->app->bind(Contracts\StorageDriver::class, $this->app['config']['tenancy.storage_driver']);
|
$this->app->bind(Contracts\StorageDriver::class, function ($app) {
|
||||||
|
return $app->make($app['config']['tenancy.storage_driver']);
|
||||||
|
});
|
||||||
$this->app->bind(Contracts\UniqueIdentifierGenerator::class, $this->app['config']['tenancy.unique_id_generator']);
|
$this->app->bind(Contracts\UniqueIdentifierGenerator::class, $this->app['config']['tenancy.unique_id_generator']);
|
||||||
$this->app->singleton(DatabaseManager::class);
|
$this->app->singleton(DatabaseManager::class);
|
||||||
$this->app->singleton(TenantManager::class);
|
$this->app->singleton(TenantManager::class);
|
||||||
|
|
|
||||||
|
|
@ -240,7 +240,7 @@ class Tenant implements ArrayAccess
|
||||||
public function put($key, $value = null): self
|
public function put($key, $value = null): self
|
||||||
{
|
{
|
||||||
if ($key === 'id') {
|
if ($key === 'id') {
|
||||||
throw new TenantStorageException("The tenant's id can't be changed.");
|
throw new TenantStorageException("Tenant ids can't be changed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_array($key)) {
|
if (is_array($key)) {
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ class TenantManager
|
||||||
protected $artisan;
|
protected $artisan;
|
||||||
|
|
||||||
/** @var Contracts\StorageDriver */
|
/** @var Contracts\StorageDriver */
|
||||||
protected $storage;
|
public $storage;
|
||||||
|
|
||||||
/** @var DatabaseManager */
|
/** @var DatabaseManager */
|
||||||
public $database;
|
public $database;
|
||||||
|
|
@ -229,7 +229,7 @@ class TenantManager
|
||||||
|
|
||||||
protected function bootstrapFeatures(): self
|
protected function bootstrapFeatures(): self
|
||||||
{
|
{
|
||||||
// todo this doesn't work
|
// todo1 this doesn't work
|
||||||
// foreach ($this->app['config']['tenancy.features'] as $feature) {
|
// foreach ($this->app['config']['tenancy.features'] as $feature) {
|
||||||
// $this->app[$feature]->bootstrap($this);
|
// $this->app[$feature]->bootstrap($this);
|
||||||
// }
|
// }
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ namespace Stancl\Tenancy\Tests;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Redis;
|
use Illuminate\Support\Facades\Redis;
|
||||||
|
|
||||||
// todo rename
|
// todo2 rename
|
||||||
class BootstrapsTenancyTest extends TestCase
|
class BootstrapsTenancyTest extends TestCase
|
||||||
{
|
{
|
||||||
public $autoInitTenancy = false;
|
public $autoInitTenancy = false;
|
||||||
|
|
|
||||||
|
|
@ -117,7 +117,7 @@ class CommandsTest extends TestCase
|
||||||
->expectsOutput('xyz');
|
->expectsOutput('xyz');
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo check that multiple tenants can be migrated at once using all database engines
|
// todo2 check that multiple tenants can be migrated at once using all database engines
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function install_command_works()
|
public function install_command_works()
|
||||||
|
|
@ -129,7 +129,7 @@ class CommandsTest extends TestCase
|
||||||
\mkdir($dir, 0777, true);
|
\mkdir($dir, 0777, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo move this to a file
|
// todo2 move this to a file
|
||||||
\file_put_contents(app_path('Http/Kernel.php'), "<?php
|
\file_put_contents(app_path('Http/Kernel.php'), "<?php
|
||||||
|
|
||||||
namespace App\Http;
|
namespace App\Http;
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ class QueueTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function queues_use_non_tenant_db_connection()
|
public function queues_use_non_tenant_db_connection()
|
||||||
{
|
{
|
||||||
// todo finish this test. requires using the db driver
|
// todo2 finish this test. requires using the db driver
|
||||||
$this->markTestIncomplete();
|
$this->markTestIncomplete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,14 +15,14 @@ class TenantAssetTest extends TestCase
|
||||||
|
|
||||||
// response()->file() returns BinaryFileResponse whose content is
|
// response()->file() returns BinaryFileResponse whose content is
|
||||||
// inaccessible via getContent, so ->assertSee() can't be used
|
// inaccessible via getContent, so ->assertSee() can't be used
|
||||||
// $this->get(tenant_asset($filename))->assertSuccessful(); // TODO COMMENTED ASSERTIONS
|
// $this->get(tenant_asset($filename))->assertSuccessful(); // TODO2 COMMENTED ASSERTIONS
|
||||||
// $this->assertFileExists($path); // TODO COMMENTED ASSERTIONS
|
// $this->assertFileExists($path); // TODO2 COMMENTED ASSERTIONS
|
||||||
|
|
||||||
$f = \fopen($path, 'r');
|
$f = \fopen($path, 'r');
|
||||||
$content = \fread($f, \filesize($path));
|
$content = \fread($f, \filesize($path));
|
||||||
\fclose($f);
|
\fclose($f);
|
||||||
|
|
||||||
// $this->assertSame('bar', $content); // TODO COMMENTED ASSERTIONS
|
// $this->assertSame('bar', $content); // TODO2 COMMENTED ASSERTIONS
|
||||||
$this->assertTrue(true); // TODO COMMENTED ASSERTIONS
|
$this->assertTrue(true); // TODO2 COMMENTED ASSERTIONS
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,6 @@ use Stancl\Tenancy\TenantDatabaseManagers\SQLiteDatabaseManager;
|
||||||
|
|
||||||
class TenantDatabaseManagerTest extends TestCase
|
class TenantDatabaseManagerTest extends TestCase
|
||||||
{
|
{
|
||||||
// todo use data providers and TenantDatabaseManager::databaseExists()
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
* @dataProvider database_manager_provider
|
* @dataProvider database_manager_provider
|
||||||
|
|
|
||||||
|
|
@ -205,6 +205,6 @@ class TenantManagerTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function id_cannot_be_changed()
|
public function id_cannot_be_changed()
|
||||||
{
|
{
|
||||||
// todo
|
// todo1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -105,14 +105,10 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
$app['config']->set([
|
$app['config']->set([
|
||||||
'tenancy.storage_driver' => RedisStorageDriver::class,
|
'tenancy.storage_driver' => RedisStorageDriver::class,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// tenancy()->storage = $app->make(RedisStorageDriver::class); // TODO this shouldn't be necessary but is necessary
|
|
||||||
} elseif (env('TENANCY_TEST_STORAGE_DRIVER', 'redis') === 'db') {
|
} elseif (env('TENANCY_TEST_STORAGE_DRIVER', 'redis') === 'db') {
|
||||||
$app['config']->set([
|
$app['config']->set([
|
||||||
'tenancy.storage_driver' => DatabaseStorageDriver::class,
|
'tenancy.storage_driver' => DatabaseStorageDriver::class,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// tenancy()->storage = $app->make(DatabaseStorageDriver::class); // TODO this shouldn't be necessary but is necessary
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue