mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 23:04:03 +00:00
Get old tests to pass
This commit is contained in:
parent
65ebc043dc
commit
439b5b1dc1
28 changed files with 154 additions and 100 deletions
|
|
@ -45,6 +45,12 @@ class Migrate extends MigrateCommand
|
|||
*/
|
||||
public function handle()
|
||||
{
|
||||
foreach (config('tenancy.migration_parameters') as $parameter => $value) {
|
||||
if (! $this->input->hasParameterOption($parameter)) {
|
||||
$this->input->setOption(ltrim($parameter, '-'), $value);
|
||||
}
|
||||
}
|
||||
|
||||
if (! $this->confirmToProceed()) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -52,8 +58,6 @@ class Migrate extends MigrateCommand
|
|||
tenancy()->all($this->option('tenants'))->each(function ($tenant) {
|
||||
$this->line("Tenant: {$tenant['id']}");
|
||||
|
||||
$this->input->setOption('database', $tenant->database()->getTemplateConnectionName());
|
||||
|
||||
$tenant->run(function () {
|
||||
// Migrate
|
||||
parent::handle();
|
||||
|
|
|
|||
|
|
@ -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->database()->getTemplateConnectionName(),
|
||||
'--database' => 'tenant',
|
||||
'--force' => true,
|
||||
]));
|
||||
|
||||
|
|
|
|||
|
|
@ -45,6 +45,12 @@ class Rollback extends RollbackCommand
|
|||
*/
|
||||
public function handle()
|
||||
{
|
||||
foreach (config('tenancy.migration_parameters') as $parameter => $value) {
|
||||
if (! $this->input->hasParameterOption($parameter)) {
|
||||
$this->input->setOption(ltrim($parameter, '-'), $value);
|
||||
}
|
||||
}
|
||||
|
||||
if (! $this->confirmToProceed()) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -52,8 +58,6 @@ class Rollback extends RollbackCommand
|
|||
tenancy()->all($this->option('tenants'))->each(function ($tenant) {
|
||||
$this->line("Tenant: {$tenant['id']}");
|
||||
|
||||
$this->input->setOption('database', $tenant->database()->getTemplateConnectionName());
|
||||
|
||||
$tenant->run(function () {
|
||||
// Rollback
|
||||
parent::handle();
|
||||
|
|
|
|||
|
|
@ -56,8 +56,6 @@ class Seed extends SeedCommand
|
|||
tenancy()->all($this->option('tenants'))->each(function ($tenant) {
|
||||
$this->line("Tenant: {$tenant['id']}");
|
||||
|
||||
$this->input->setOption('database', $tenant->database()->getTemplateConnectionName());
|
||||
|
||||
$tenant->run(function () {
|
||||
// Seed
|
||||
parent::handle();
|
||||
|
|
|
|||
|
|
@ -48,17 +48,17 @@ class DatabaseConfig
|
|||
$this->tenant = $tenant;
|
||||
}
|
||||
|
||||
public static function generateDatabaseNameUsing(callable $databaseNameGenerator): void
|
||||
public static function generateDatabaseNamesUsing(callable $databaseNameGenerator): void
|
||||
{
|
||||
static::$databaseNameGenerator = $databaseNameGenerator;
|
||||
}
|
||||
|
||||
public static function generateUsernameUsing(callable $usernameGenerator): void
|
||||
public static function generateUsernamesUsing(callable $usernameGenerator): void
|
||||
{
|
||||
static::$usernameGenerator = $usernameGenerator;
|
||||
}
|
||||
|
||||
public static function generatePasswordUsing(callable $passwordGenerator): void
|
||||
public static function generatePasswordsUsing(callable $passwordGenerator): void
|
||||
{
|
||||
static::$passwordGenerator = $passwordGenerator;
|
||||
}
|
||||
|
|
@ -86,25 +86,13 @@ class DatabaseConfig
|
|||
$this->tenant->data['_tenancy_db_username'] = $this->getUsername() ?? (static::$usernameGenerator)($this->tenant);
|
||||
$this->tenant->data['_tenancy_db_password'] = $this->getPassword() ?? (static::$passwordGenerator)($this->tenant);
|
||||
}
|
||||
|
||||
$this->tenant->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Template used to construct the tenant connection. Also serves as
|
||||
* the root connection on which the tenant database is created.
|
||||
*/
|
||||
public function getTemplateConnectionName()
|
||||
public function getTemplateConnectionName(): string
|
||||
{
|
||||
$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.
|
||||
if (! array_key_exists($name, config('database.connections'))) {
|
||||
$name = config('tenancy.database.template_connection') ?? DatabaseManager::$originalDefaultConnectionName;
|
||||
}
|
||||
|
||||
return $name;
|
||||
return $this->tenant->data['_tenancy_db_connection']
|
||||
?? config('tenancy.database.template_connection')
|
||||
?? DatabaseManager::$originalDefaultConnectionName;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -112,7 +100,9 @@ class DatabaseConfig
|
|||
*/
|
||||
public function connection(): array
|
||||
{
|
||||
$templateConnection = config("database.connections.{$this->getTemplateConnectionName()}");
|
||||
$template = $this->getTemplateConnectionName();
|
||||
|
||||
$templateConnection = config("database.connections.{$template}");
|
||||
|
||||
$databaseName = $this->getName();
|
||||
if (($manager = $this->manager()) instanceof ModifiesDatabaseNameForConnection) {
|
||||
|
|
|
|||
|
|
@ -55,8 +55,8 @@ class DatabaseManager
|
|||
public function connect(Tenant $tenant)
|
||||
{
|
||||
$this->createTenantConnection($tenant);
|
||||
$this->setDefaultConnection($tenant->database()->getTemplateConnectionName());
|
||||
$this->switchConnection($tenant->database()->getTemplateConnectionName());
|
||||
$this->setDefaultConnection('tenant');
|
||||
$this->switchConnection('tenant');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -64,10 +64,11 @@ class DatabaseManager
|
|||
*/
|
||||
public function reconnect()
|
||||
{
|
||||
// Opposite order to connect() because we don't
|
||||
// want to ever purge the central connection
|
||||
$this->switchConnection(static::$originalDefaultConnectionName);
|
||||
if ($this->tenancy->initialized) {
|
||||
$this->database->purge('tenant');
|
||||
}
|
||||
$this->setDefaultConnection(static::$originalDefaultConnectionName);
|
||||
$this->switchConnection(static::$originalDefaultConnectionName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -83,7 +84,7 @@ class DatabaseManager
|
|||
*/
|
||||
public function createTenantConnection(Tenant $tenant)
|
||||
{
|
||||
$this->app['config']["database.connections.{$tenant->database()->getTemplateConnectionName()}"] = $tenant->database()->connection();
|
||||
$this->app['config']["database.connections.tenant"] = $tenant->database()->connection();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -91,7 +92,6 @@ class DatabaseManager
|
|||
*/
|
||||
public function switchConnection(string $connection)
|
||||
{
|
||||
$this->database->purge();
|
||||
$this->database->reconnect($connection);
|
||||
$this->database->setDefaultConnection($connection);
|
||||
}
|
||||
|
|
@ -120,8 +120,6 @@ class DatabaseManager
|
|||
*/
|
||||
public function createDatabase(Tenant $tenant, array $afterCreating = [])
|
||||
{
|
||||
$tenant->database()->makeCredentials();
|
||||
|
||||
$afterCreating = array_merge(
|
||||
$afterCreating,
|
||||
$this->tenancy->event('database.creating', $tenant->database()->getName(), $tenant)
|
||||
|
|
|
|||
|
|
@ -21,22 +21,22 @@ use Stancl\Tenancy\Tenant;
|
|||
class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys, CanFindByAnyKey
|
||||
{
|
||||
/** @var Application */
|
||||
protected $app;
|
||||
public $app;
|
||||
|
||||
/** @var Connection */
|
||||
protected $centralDatabase;
|
||||
public $centralDatabase;
|
||||
|
||||
/** @var TenantRepository */
|
||||
protected $tenants;
|
||||
public $tenants;
|
||||
|
||||
/** @var DomainRepository */
|
||||
protected $domains;
|
||||
public $domains;
|
||||
|
||||
/** @var CachedTenantResolver */
|
||||
protected $cache;
|
||||
public $cache;
|
||||
|
||||
/** @var Tenant The default tenant. */
|
||||
protected $tenant;
|
||||
public $tenant;
|
||||
|
||||
public function __construct(Application $app, ConfigRepository $config, CachedTenantResolver $cache)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -50,8 +50,6 @@ class DomainRepository extends Repository
|
|||
|
||||
public function getTable(ConfigRepository $config)
|
||||
{
|
||||
return $config->get('tenancy.storage_drivers.db.table_names.DomainModel') // legacy
|
||||
?? $config->get('tenancy.storage_drivers.db.table_names.domains')
|
||||
?? 'domains';
|
||||
return $config->get('tenancy.storage_drivers.db.table_names.domains') ?? 'domains';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -230,6 +230,7 @@ class Tenant implements ArrayAccess
|
|||
if ($this->persisted) {
|
||||
$this->manager->updateTenant($this);
|
||||
} else {
|
||||
$this->database()->makeCredentials();
|
||||
$this->manager->createTenant($this);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -25,12 +25,12 @@ class PermissionControlledMySQLDatabaseManager extends MySQLDatabaseManager impl
|
|||
$hostname = $databaseConfig->connection()['host'];
|
||||
$password = $databaseConfig->getPassword();
|
||||
|
||||
$this->database()->statement("CREATE USER `{$username}`@`{$hostname}` IDENTIFIED BY `{$password}`");
|
||||
$this->database()->statement("CREATE USER `{$username}`@`{$hostname}` IDENTIFIED BY '{$password}'");
|
||||
|
||||
$grants = implode(', ', static::$grants);
|
||||
|
||||
if ($this->isVersion8()) { // MySQL 8+
|
||||
$grantQuery = "GRANT $grants ON `$database`.* TO `$username`@`$hostname`";
|
||||
$grantQuery = "GRANT $grants ON $database.* TO `$username`@`$hostname`";
|
||||
} else { // MySQL 5.7
|
||||
$grantQuery = "GRANT $grants ON $database.* TO `$username`@`$hostname` IDENTIFIED BY '$password'";
|
||||
}
|
||||
|
|
@ -40,13 +40,13 @@ class PermissionControlledMySQLDatabaseManager extends MySQLDatabaseManager impl
|
|||
|
||||
protected function isVersion8(): bool
|
||||
{
|
||||
$version = $this->database()->select($this->db->raw('select version()'))[0]->{'version()'};
|
||||
$version = $this->database()->select($this->database()->raw('select version()'))[0]->{'version()'};
|
||||
|
||||
return version_compare($version, '8.0.0') >= 0;
|
||||
}
|
||||
|
||||
public function deleteUser(DatabaseConfig $databaseConfig): bool
|
||||
{
|
||||
return $this->database()->statement("DROP USER IF EXISTS '{$databaseConfig->username}'");
|
||||
return $this->database()->statement("DROP USER IF EXISTS '{$databaseConfig->getUsername()}'");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,6 @@ trait DealsWithMigrations
|
|||
return parent::getMigrationPaths();
|
||||
}
|
||||
|
||||
return config('tenancy.migration_paths', [config('tenancy.migrations_directory') ?? database_path('migrations/tenant')]);
|
||||
return database_path('migrations/tenant');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue