Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.2.4] [WIP] Respect custom connections when creating database #244

Merged
merged 2 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Contracts/Future/CanSetConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Stancl\Tenancy\Contracts\Future;

use Illuminate\Database\Connection;

/**
* This interface *might* be part of the TenantDatabaseManager interface in 3.x.
*/
interface CanSetConnection
{
public function setConnection(Connection $connection);
}
11 changes: 9 additions & 2 deletions src/DatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
use Illuminate\Foundation\Application;
use Stancl\Tenancy\Contracts\Future\CanSetConnection;
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
use Stancl\Tenancy\Exceptions\DatabaseManagerNotRegisteredException;
use Stancl\Tenancy\Exceptions\TenantDatabaseAlreadyExistsException;
Expand Down Expand Up @@ -226,14 +227,20 @@ public function deleteDatabase(Tenant $tenant)
*/
public function getTenantDatabaseManager(Tenant $tenant): TenantDatabaseManager
{
$driver = $this->getDriver($this->getBaseConnection($tenant->getConnectionName()));
$driver = $this->getDriver($this->getBaseConnection($connectionName = $tenant->getConnectionName()));

$databaseManagers = $this->app['config']['tenancy.database_managers'];

if (! array_key_exists($driver, $databaseManagers)) {
throw new DatabaseManagerNotRegisteredException($driver);
}

return $this->app[$databaseManagers[$driver]];
$databaseManager = $this->app[$databaseManagers[$driver]];

if ($connectionName !== 'tenant' && $databaseManager instanceof CanSetConnection) {
$databaseManager->setConnection($this->database->connection($connectionName));
}

return $databaseManager;
}
}
11 changes: 9 additions & 2 deletions src/TenantDatabaseManagers/MySQLDatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@
namespace Stancl\Tenancy\TenantDatabaseManagers;

use Illuminate\Contracts\Config\Repository;
use Illuminate\Database\Connection;
use Illuminate\Database\DatabaseManager as IlluminateDatabaseManager;
use Stancl\Tenancy\Contracts\Future\CanSetConnection;
use Stancl\Tenancy\Contracts\TenantDatabaseManager;

class MySQLDatabaseManager implements TenantDatabaseManager
class MySQLDatabaseManager implements TenantDatabaseManager, CanSetConnection
{
/** @var \Illuminate\Database\Connection */
/** @var Connection */
protected $database;

public function __construct(Repository $config, IlluminateDatabaseManager $databaseManager)
{
$this->database = $databaseManager->connection($config['tenancy.database_manager_connections.mysql']);
}

public function setConnection(Connection $connection)
{
$this->database = $connection;
}

public function createDatabase(string $name): bool
{
$charset = $this->database->getConfig('charset');
Expand Down
8 changes: 7 additions & 1 deletion src/TenantDatabaseManagers/PostgreSQLDatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@
namespace Stancl\Tenancy\TenantDatabaseManagers;

use Illuminate\Contracts\Config\Repository;
use Illuminate\Database\Connection;
use Illuminate\Database\DatabaseManager as IlluminateDatabaseManager;
use Stancl\Tenancy\Contracts\TenantDatabaseManager;

class PostgreSQLDatabaseManager implements TenantDatabaseManager
{
/** @var \Illuminate\Database\Connection */
/** @var Connection */
protected $database;

public function __construct(Repository $config, IlluminateDatabaseManager $databaseManager)
{
$this->database = $databaseManager->connection($config['tenancy.database_manager_connections.pgsql']);
}

public function setConnection(Connection $connection)
{
$this->database = $connection;
}

public function createDatabase(string $name): bool
{
return $this->database->statement("CREATE DATABASE \"$name\" WITH TEMPLATE=template0");
Expand Down