1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 13:54:04 +00:00

Make phpunit run

This commit is contained in:
Samuel Štancl 2020-04-30 21:25:41 +02:00
parent b07f5bdc22
commit 2de46e4274
8 changed files with 40 additions and 22 deletions

View file

@ -52,7 +52,7 @@ class Migrate extends MigrateCommand
tenancy()->all($this->option('tenants'))->each(function ($tenant) {
$this->line("Tenant: {$tenant['id']}");
$this->input->setOption('database', $tenant->getConnectionName());
$this->input->setOption('database', $tenant->database()->getTemplateConnectionName());
$tenant->run(function () {
// Migrate

View file

@ -39,7 +39,7 @@ final class MigrateFresh extends Command
$tenant->run(function ($tenant) {
$this->info('Dropping tables.');
$this->call('db:wipe', array_filter([
'--database' => $tenant->getConnectionName(),
'--database' => $tenant->database()->getTemplateConnectionName(),
'--force' => true,
]));

View file

@ -52,7 +52,7 @@ class Rollback extends RollbackCommand
tenancy()->all($this->option('tenants'))->each(function ($tenant) {
$this->line("Tenant: {$tenant['id']}");
$this->input->setOption('database', $tenant->getConnectionName());
$this->input->setOption('database', $tenant->database()->getTemplateConnectionName());
$tenant->run(function () {
// Rollback

View file

@ -56,7 +56,7 @@ class Seed extends SeedCommand
tenancy()->all($this->option('tenants'))->each(function ($tenant) {
$this->line("Tenant: {$tenant['id']}");
$this->input->setOption('database', $tenant->getConnectionName());
$this->input->setOption('database', $tenant->database()->getTemplateConnectionName());
$tenant->run(function () {
// Seed

View file

@ -0,0 +1,11 @@
<?php
namespace Stancl\Tenancy\Contracts;
/**
* Used by sqlite to wrap database name in database_path().
*/
interface ModifiesDatabaseNameForConnection
{
public function getDatabaseNameForConnection(string $original): string;
}

View file

@ -8,6 +8,7 @@ use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Stancl\Tenancy\Contracts\Future\CanSetConnection;
use Stancl\Tenancy\Contracts\ManagesDatabaseUsers;
use Stancl\Tenancy\Contracts\ModifiesDatabaseNameForConnection;
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
use Stancl\Tenancy\Exceptions\DatabaseManagerNotRegisteredException;
@ -62,24 +63,19 @@ class DatabaseConfig
static::$passwordGenerator = $passwordGenerator;
}
public function getName(): string
public function getName(): ?string
{
return $this->tenant->data['_tenancy_db_name'];
return $this->tenant->data['_tenancy_db_name'] ?? (static::$databaseNameGenerator)($this->tenant);
}
public function getUsername(): string
public function getUsername(): ?string
{
return $this->tenant->data['_tenancy_db_username'];
return $this->tenant->data['_tenancy_db_username'] ?? null;
}
public function getPassword(): string
public function getPassword(): ?string
{
return $this->tenant->data['_tenancy_db_password'];
}
public function getTemplateDatabaseConnection(): string
{
return $this->tenant->data['_tenancy_db_connection'];
return $this->tenant->data['_tenancy_db_password'] ?? null;
}
public function makeCredentials(): void
@ -100,7 +96,7 @@ class DatabaseConfig
*/
public function getTemplateConnectionName()
{
$name = $this->tenant->getTemplateDatabaseConnection();
$name = $this->tenant->data['_tenancy_db_connection'] ?? 'tenant';
// If we're using e.g. 'tenant', the default, template connection
// and it doesn't exist, we'll go for the default DB template.
@ -118,8 +114,13 @@ class DatabaseConfig
{
$templateConnection = config("database.connections.{$this->getTemplateConnectionName()}");
if (($manager = $this->manager()) instanceof ModifiesDatabaseNameForConnection) {
/** @var ModifiesDatabaseNameForConnection $manager */
$databaseName = $manager->getDatabaseNameForConnection($this->getName());
}
return array_merge($templateConnection, $this->tenantConfig(), [
$this->manager()->getSeparator() => $this->tenant->database()->getName(),
$this->manager()->getSeparator() => $databaseName,
]);
}

View file

@ -54,9 +54,9 @@ class DatabaseManager
*/
public function connect(Tenant $tenant)
{
$this->createTenantConnection($tenant, $tenant->getConnectionName());
$this->setDefaultConnection($tenant->getConnectionName());
$this->switchConnection($tenant->getConnectionName());
$this->createTenantConnection($tenant, $tenant->database()->getTemplateConnectionName());
$this->setDefaultConnection($tenant->database()->getTemplateConnectionName());
$this->switchConnection($tenant->database()->getTemplateConnectionName());
}
/**
@ -83,7 +83,7 @@ class DatabaseManager
*/
public function createTenantConnection(Tenant $tenant, $connectionName)
{
$this->app['config']["database.connections.$connectionName"] = $tenant->database->connection();
$this->app['config']["database.connections.$connectionName"] = $tenant->database()->connection();
}
/**

View file

@ -4,9 +4,10 @@ declare(strict_types=1);
namespace Stancl\Tenancy\TenantDatabaseManagers;
use Stancl\Tenancy\Contracts\ModifiesDatabaseNameForConnection;
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
class SQLiteDatabaseManager implements TenantDatabaseManager
class SQLiteDatabaseManager implements TenantDatabaseManager, ModifiesDatabaseNameForConnection
{
public function getSeparator(): string
{
@ -35,4 +36,9 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
{
return file_exists(database_path($name));
}
public function getDatabaseNameForConnection(string $original): string
{
return database_path($original);
}
}