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\TenantRedirect',
],
'migrate_after_creation' => false, // run migrations after creating a tenant
'queue_database_creation' => false,
'queue_database_deletion' => false,
'unique_id_generator' => 'Stancl\Tenancy\UUIDGenerator',

View file

@ -54,9 +54,8 @@ class Migrate extends MigrateCommand
// See Illuminate\Database\Migrations\DatabaseMigrationRepository::getConnection.
// Database connections are cached by Illuminate\Database\ConnectionResolver.
$connectionName = "tenant{$tenant['uuid']}"; // todo use Illuminate DatabaseManager reconnect()?
$this->input->setOption('database', $connectionName);
$this->database->connectToTenant($tenant, $connectionName);
$this->input->setOption('database', 'tenant');
$this->database->connectToTenant($tenant); // todo test that this works with multiple tenants with MySQL
// Migrate
parent::handle();

View file

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

View file

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

View file

@ -97,7 +97,7 @@ class Tenant implements ArrayAccess
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)

View file

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

View file

@ -37,7 +37,7 @@ class TenantModel extends Model
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)