1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 11:34:02 +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) { tenancy()->all($this->option('tenants'))->each(function ($tenant) {
$this->line("Tenant: {$tenant['id']}"); $this->line("Tenant: {$tenant['id']}");
$this->input->setOption('database', $tenant->getConnectionName()); $this->input->setOption('database', $tenant->database()->getTemplateConnectionName());
$tenant->run(function () { $tenant->run(function () {
// Migrate // Migrate

View file

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

View file

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

View file

@ -56,7 +56,7 @@ class Seed extends SeedCommand
tenancy()->all($this->option('tenants'))->each(function ($tenant) { tenancy()->all($this->option('tenants'))->each(function ($tenant) {
$this->line("Tenant: {$tenant['id']}"); $this->line("Tenant: {$tenant['id']}");
$this->input->setOption('database', $tenant->getConnectionName()); $this->input->setOption('database', $tenant->database()->getTemplateConnectionName());
$tenant->run(function () { $tenant->run(function () {
// Seed // 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 Illuminate\Support\Str;
use Stancl\Tenancy\Contracts\Future\CanSetConnection; use Stancl\Tenancy\Contracts\Future\CanSetConnection;
use Stancl\Tenancy\Contracts\ManagesDatabaseUsers; use Stancl\Tenancy\Contracts\ManagesDatabaseUsers;
use Stancl\Tenancy\Contracts\ModifiesDatabaseNameForConnection;
use Stancl\Tenancy\Contracts\TenantDatabaseManager; use Stancl\Tenancy\Contracts\TenantDatabaseManager;
use Stancl\Tenancy\Exceptions\DatabaseManagerNotRegisteredException; use Stancl\Tenancy\Exceptions\DatabaseManagerNotRegisteredException;
@ -62,24 +63,19 @@ class DatabaseConfig
static::$passwordGenerator = $passwordGenerator; 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']; return $this->tenant->data['_tenancy_db_password'] ?? null;
}
public function getTemplateDatabaseConnection(): string
{
return $this->tenant->data['_tenancy_db_connection'];
} }
public function makeCredentials(): void public function makeCredentials(): void
@ -100,7 +96,7 @@ class DatabaseConfig
*/ */
public function getTemplateConnectionName() 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 // If we're using e.g. 'tenant', the default, template connection
// and it doesn't exist, we'll go for the default DB template. // 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()}"); $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(), [ 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) public function connect(Tenant $tenant)
{ {
$this->createTenantConnection($tenant, $tenant->getConnectionName()); $this->createTenantConnection($tenant, $tenant->database()->getTemplateConnectionName());
$this->setDefaultConnection($tenant->getConnectionName()); $this->setDefaultConnection($tenant->database()->getTemplateConnectionName());
$this->switchConnection($tenant->getConnectionName()); $this->switchConnection($tenant->database()->getTemplateConnectionName());
} }
/** /**
@ -83,7 +83,7 @@ class DatabaseManager
*/ */
public function createTenantConnection(Tenant $tenant, $connectionName) 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; namespace Stancl\Tenancy\TenantDatabaseManagers;
use Stancl\Tenancy\Contracts\ModifiesDatabaseNameForConnection;
use Stancl\Tenancy\Contracts\TenantDatabaseManager; use Stancl\Tenancy\Contracts\TenantDatabaseManager;
class SQLiteDatabaseManager implements TenantDatabaseManager class SQLiteDatabaseManager implements TenantDatabaseManager, ModifiesDatabaseNameForConnection
{ {
public function getSeparator(): string public function getSeparator(): string
{ {
@ -35,4 +36,9 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
{ {
return file_exists(database_path($name)); return file_exists(database_path($name));
} }
public function getDatabaseNameForConnection(string $original): string
{
return database_path($original);
}
} }