1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 12:54:05 +00:00

WIP DatabaseManager

This commit is contained in:
Samuel Štancl 2019-09-08 13:46:07 +02:00
parent ab0a40a17e
commit 04b4e0644f
7 changed files with 27 additions and 11 deletions

View file

@ -68,6 +68,7 @@ return [
'Stancl\Tenancy\Features\TelescopeTags', 'Stancl\Tenancy\Features\TelescopeTags',
'Stancl\Tenancy\Features\TenantRedirect', 'Stancl\Tenancy\Features\TenantRedirect',
], ],
'migrate_after_creation' => false, // run migrations after creating a tenant
'queue_database_creation' => false, 'queue_database_creation' => false,
'queue_database_deletion' => false, 'queue_database_deletion' => false,
'unique_id_generator' => 'Stancl\Tenancy\UUIDGenerator', 'unique_id_generator' => 'Stancl\Tenancy\UUIDGenerator',

View file

@ -54,9 +54,8 @@ 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.
$connectionName = "tenant{$tenant['uuid']}"; // todo use Illuminate DatabaseManager reconnect()? $this->input->setOption('database', 'tenant');
$this->input->setOption('database', $connectionName); $this->database->connectToTenant($tenant); // todo test that this works with multiple tenants with MySQL
$this->database->connectToTenant($tenant, $connectionName);
// Migrate // Migrate
parent::handle(); parent::handle();

View file

@ -7,6 +7,7 @@ namespace Stancl\Tenancy\Contracts;
use Stancl\Tenancy\TenantManager; use Stancl\Tenancy\TenantManager;
/** Additional features, like Telescope tags and tenant redirects. */ /** Additional features, like Telescope tags and tenant redirects. */
// todo should this be FeatureProvider?
interface Feature interface Feature
{ {
// todo is the tenantManager argument necessary? // todo is the tenantManager argument necessary?

View file

@ -4,37 +4,44 @@ declare(strict_types=1);
namespace Stancl\Tenancy; namespace Stancl\Tenancy;
use Illuminate\Foundation\Application;
use Illuminate\Database\DatabaseManager as BaseDatabaseManager; use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
class DatabaseManagerv2 class DatabaseManagerv2
{ {
/** @var string */
public $originalDefaultConnectionName;
/** @var Application */
protected $app;
/** @var BaseDatabaseManager */ /** @var BaseDatabaseManager */
protected $database; protected $database;
public function __construct(BaseDatabaseManager $database) public function __construct(Application $app, BaseDatabaseManager $database)
{ {
$this->database = $database; $this->database = $database;
$this->originalDefaultConnectionName = $app['config']['database.default'];
} }
public function connect(Tenant $tenant) public function connect(Tenant $tenant)
{ {
$connection = $tenant->getConnectionName(); // todo $connection = 'tenant'; // todo tenant-specific connections
$this->createTenantConnection($tenant->getDatabaseName(), $connection); $this->createTenantConnection($tenant->getDatabaseName(), $connection);
$this->switchConnection($connection); $this->switchConnection($connection);
} }
public function reconnect() public function reconnect()
{ {
$this->switchConnection($this->originalDefaultConnection); $this->switchConnection($this->originalDefaultConnectionName);
} }
public function createTenantConnection(string $databaseName, string $connectionName = null) public function createTenantConnection(string $databaseName, string $connectionName = null)
{ {
$connectionName = $connectionName ?: $this->defaultTenantConnectionName; $connectionName = $connectionName ?? 'tenant'; // todo
// Create the database connection. // Create the database connection.
$based_on = config('tenancy.database.based_on') ?: config('database.default'); $based_on = config('tenancy.database.based_on') ?? $this->originalDefaultConnectionName;
config()->set([ config()->set([
"database.connections.$connectionName" => config('database.connections.' . $based_on), "database.connections.$connectionName" => config('database.connections.' . $based_on),
]); ]);

View file

@ -97,7 +97,7 @@ class Tenant implements ArrayAccess
public function getDatabaseName() public function getDatabaseName()
{ {
return $this['_tenancy_database'] ?? $this->app['config']['tenancy.database.prefix'] . $this->uuid . $this->app['config']['tenancy.database.suffix']; return $this['_tenancy_db_name'] ?? $this->app['config']['tenancy.database.prefix'] . $this->uuid . $this->app['config']['tenancy.database.suffix'];
} }
public function __get($name) public function __get($name)

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy; namespace Stancl\Tenancy;
use Illuminate\Foundation\Application; use Illuminate\Foundation\Application;
use Stancl\Tenancy\Exceptions\NoTenantIdentifiedException;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
/** /**
@ -41,6 +42,7 @@ class TenantManagerv2
{ {
// todo make this atomic // todo make this atomic
$this->storage->createTenant($tenant); $this->storage->createTenant($tenant);
$this->database->create($tenant);
// todo create database, optionally migrate // todo create database, optionally migrate
return $this; return $this;
@ -125,6 +127,12 @@ class TenantManagerv2
return $this; return $this;
} }
/**
* Get the current tenant
*
* @return Tenant
* @throws NoTenantIdentifiedException
*/
public function getTenant(): Tenant public function getTenant(): Tenant
{ {
if (! $this->tenant) { if (! $this->tenant) {

View file

@ -37,7 +37,7 @@ class TenantModel extends Model
public function getConnectionName() public function getConnectionName()
{ {
return config('tenancy.storage.db.connection'); return config('tenancy.storage.db.connection') ?? app(DatabaseManager::class)->originalDefaultConnectionName;
} }
public static function getAllTenants(array $uuids) public static function getAllTenants(array $uuids)