mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 16:34:04 +00:00
added support for microsoft sql server database
This commit is contained in:
parent
20e1fa1959
commit
a10490e6e4
2 changed files with 59 additions and 0 deletions
57
src/TenantDatabaseManagers/MicrosoftSQLDatabaseManager.php
Normal file
57
src/TenantDatabaseManagers/MicrosoftSQLDatabaseManager.php
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\TenantDatabaseManagers;
|
||||||
|
|
||||||
|
use Illuminate\Database\Connection;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||||
|
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||||
|
use Stancl\Tenancy\Exceptions\NoConnectionSetException;
|
||||||
|
|
||||||
|
class MicrosoftSQLDatabaseManager implements TenantDatabaseManager
|
||||||
|
{
|
||||||
|
/** @var string */
|
||||||
|
protected $connection;
|
||||||
|
|
||||||
|
protected function database(): Connection
|
||||||
|
{
|
||||||
|
if ($this->connection === null) {
|
||||||
|
throw new NoConnectionSetException(static::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
return DB::connection($this->connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setConnection(string $connection): void
|
||||||
|
{
|
||||||
|
$this->connection = $connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function createDatabase(TenantWithDatabase $tenant): bool
|
||||||
|
{
|
||||||
|
$database = $tenant->database()->getName();
|
||||||
|
$charset = $this->database()->getConfig('charset');
|
||||||
|
$collation = $this->database()->getConfig('collation');
|
||||||
|
|
||||||
|
return $this->database()->statement("CREATE DATABASE [{$database}]");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function deleteDatabase(TenantWithDatabase $tenant): bool
|
||||||
|
{
|
||||||
|
return $this->database()->statement("DROP DATABASE [{$tenant->database()->getName()}]");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function databaseExists(string $name): bool
|
||||||
|
{
|
||||||
|
return (bool) $this->database()->select("SELECT name FROM master.sys.databases WHERE name = '$name'");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function makeConnectionConfig(array $baseConfig, string $databaseName): array
|
||||||
|
{
|
||||||
|
$baseConfig['database'] = $databaseName;
|
||||||
|
|
||||||
|
return $baseConfig;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -15,6 +15,7 @@ use Stancl\Tenancy\Events\TenantCreated;
|
||||||
use Stancl\Tenancy\Exceptions\TenantDatabaseAlreadyExistsException;
|
use Stancl\Tenancy\Exceptions\TenantDatabaseAlreadyExistsException;
|
||||||
use Stancl\Tenancy\Jobs\CreateDatabase;
|
use Stancl\Tenancy\Jobs\CreateDatabase;
|
||||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||||
|
use Stancl\Tenancy\TenantDatabaseManagers\MicrosoftSQLDatabaseManager;
|
||||||
use Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager;
|
use Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager;
|
||||||
use Stancl\Tenancy\TenantDatabaseManagers\PermissionControlledMySQLDatabaseManager;
|
use Stancl\Tenancy\TenantDatabaseManagers\PermissionControlledMySQLDatabaseManager;
|
||||||
use Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager;
|
use Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager;
|
||||||
|
|
@ -99,6 +100,7 @@ class TenantDatabaseManagerTest extends TestCase
|
||||||
['sqlite', SQLiteDatabaseManager::class],
|
['sqlite', SQLiteDatabaseManager::class],
|
||||||
['pgsql', PostgreSQLDatabaseManager::class],
|
['pgsql', PostgreSQLDatabaseManager::class],
|
||||||
['pgsql', PostgreSQLSchemaManager::class],
|
['pgsql', PostgreSQLSchemaManager::class],
|
||||||
|
['sqlsrv', MicrosoftSQLDatabaseManager::class],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue