mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 18:04:03 +00:00
Merge hotfix branch (#834)
* try specifying the signature in __construct * constructor doesn't work since Reflection is used, try specifying getDefaultName() instead * Fixed: make migration commands compatible * Fix failing tests * Fix username generation * Re-create tmp dir as well if needed * wip
This commit is contained in:
parent
4f19609797
commit
349125c02e
10 changed files with 75 additions and 27 deletions
|
|
@ -8,32 +8,26 @@ use Illuminate\Contracts\Events\Dispatcher;
|
|||
use Illuminate\Database\Console\Migrations\MigrateCommand;
|
||||
use Illuminate\Database\Migrations\Migrator;
|
||||
use Stancl\Tenancy\Concerns\DealsWithMigrations;
|
||||
use Stancl\Tenancy\Concerns\ExtendsLaravelCommand;
|
||||
use Stancl\Tenancy\Concerns\HasATenantsOption;
|
||||
use Stancl\Tenancy\Events\DatabaseMigrated;
|
||||
use Stancl\Tenancy\Events\MigratingDatabase;
|
||||
|
||||
class Migrate extends MigrateCommand
|
||||
{
|
||||
use HasATenantsOption, DealsWithMigrations;
|
||||
use HasATenantsOption, DealsWithMigrations, ExtendsLaravelCommand;
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Run migrations for tenant(s)';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @param Migrator $migrator
|
||||
* @param Dispatcher $dispatcher
|
||||
*/
|
||||
protected static function getTenantCommandName(): string
|
||||
{
|
||||
return 'tenants:migrate';
|
||||
}
|
||||
|
||||
public function __construct(Migrator $migrator, Dispatcher $dispatcher)
|
||||
{
|
||||
parent::__construct($migrator, $dispatcher);
|
||||
|
||||
$this->setName('tenants:migrate');
|
||||
$this->specifyParameters();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ final class MigrateFresh extends Command
|
|||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
|
||||
$this->addOption('--drop-views', null, InputOption::VALUE_NONE, 'Drop views along with tenant tables.', null);
|
||||
|
||||
$this->setName('tenants:migrate-fresh');
|
||||
|
|
|
|||
|
|
@ -7,13 +7,19 @@ namespace Stancl\Tenancy\Commands;
|
|||
use Illuminate\Database\Console\Migrations\RollbackCommand;
|
||||
use Illuminate\Database\Migrations\Migrator;
|
||||
use Stancl\Tenancy\Concerns\DealsWithMigrations;
|
||||
use Stancl\Tenancy\Concerns\ExtendsLaravelCommand;
|
||||
use Stancl\Tenancy\Concerns\HasATenantsOption;
|
||||
use Stancl\Tenancy\Events\DatabaseRolledBack;
|
||||
use Stancl\Tenancy\Events\RollingBackDatabase;
|
||||
|
||||
class Rollback extends RollbackCommand
|
||||
{
|
||||
use HasATenantsOption, DealsWithMigrations;
|
||||
use HasATenantsOption, DealsWithMigrations, ExtendsLaravelCommand;
|
||||
|
||||
protected static function getTenantCommandName(): string
|
||||
{
|
||||
return 'tenants:rollback';
|
||||
}
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
|
|
@ -31,8 +37,7 @@ class Rollback extends RollbackCommand
|
|||
{
|
||||
parent::__construct($migrator);
|
||||
|
||||
$this->setName('tenants:rollback');
|
||||
$this->specifyParameters();
|
||||
$this->specifyTenantSignature();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
23
src/Concerns/ExtendsLaravelCommand.php
Normal file
23
src/Concerns/ExtendsLaravelCommand.php
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Concerns;
|
||||
|
||||
trait ExtendsLaravelCommand
|
||||
{
|
||||
protected function specifyTenantSignature(): void
|
||||
{
|
||||
$this->specifyParameters();
|
||||
}
|
||||
|
||||
public function getName(): ?string
|
||||
{
|
||||
return static::getTenantCommandName();
|
||||
}
|
||||
|
||||
public static function getDefaultName(): ?string
|
||||
{
|
||||
return static::getTenantCommandName();
|
||||
}
|
||||
|
||||
abstract protected static function getTenantCommandName(): string;
|
||||
}
|
||||
|
|
@ -7,10 +7,12 @@ namespace Stancl\Tenancy\Database;
|
|||
use Illuminate\Config\Repository;
|
||||
use Illuminate\Contracts\Foundation\Application;
|
||||
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
|
||||
use Stancl\Tenancy\Contracts\ManagesDatabaseUsers;
|
||||
use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException;
|
||||
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||
use Stancl\Tenancy\Exceptions\DatabaseManagerNotRegisteredException;
|
||||
use Stancl\Tenancy\Exceptions\TenantDatabaseAlreadyExistsException;
|
||||
use Stancl\Tenancy\Exceptions\TenantDatabaseUserAlreadyExistsException;
|
||||
|
||||
/**
|
||||
* @internal Class is subject to breaking changes in minor and patch versions.
|
||||
|
|
@ -90,8 +92,14 @@ class DatabaseManager
|
|||
*/
|
||||
public function ensureTenantCanBeCreated(TenantWithDatabase $tenant): void
|
||||
{
|
||||
if ($tenant->database()->manager()->databaseExists($database = $tenant->database()->getName())) {
|
||||
$manager = $tenant->database()->manager();
|
||||
|
||||
if ($manager->databaseExists($database = $tenant->database()->getName())) {
|
||||
throw new TenantDatabaseAlreadyExistsException($database);
|
||||
}
|
||||
|
||||
if ($manager instanceof ManagesDatabaseUsers && $manager->userExists($username = $tenant->database()->getUsername())) {
|
||||
throw new TenantDatabaseUserAlreadyExistsException($username);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,8 +36,8 @@ class CreateDatabase implements ShouldQueue
|
|||
return false;
|
||||
}
|
||||
|
||||
$databaseManager->ensureTenantCanBeCreated($this->tenant);
|
||||
$this->tenant->database()->makeCredentials();
|
||||
$databaseManager->ensureTenantCanBeCreated($this->tenant);
|
||||
$this->tenant->database()->manager()->createDatabase($this->tenant);
|
||||
|
||||
event(new DatabaseCreated($this->tenant));
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ namespace Stancl\Tenancy\TenantDatabaseManagers;
|
|||
use Stancl\Tenancy\Concerns\CreatesDatabaseUsers;
|
||||
use Stancl\Tenancy\Contracts\ManagesDatabaseUsers;
|
||||
use Stancl\Tenancy\DatabaseConfig;
|
||||
use Stancl\Tenancy\Exceptions\TenantDatabaseUserAlreadyExistsException;
|
||||
|
||||
class PermissionControlledMySQLDatabaseManager extends MySQLDatabaseManager implements ManagesDatabaseUsers
|
||||
{
|
||||
|
|
@ -26,10 +25,6 @@ class PermissionControlledMySQLDatabaseManager extends MySQLDatabaseManager impl
|
|||
$hostname = $databaseConfig->connection()['host'];
|
||||
$password = $databaseConfig->getPassword();
|
||||
|
||||
if ($this->userExists($username)) {
|
||||
throw new TenantDatabaseUserAlreadyExistsException($username);
|
||||
}
|
||||
|
||||
$this->database()->statement("CREATE USER `{$username}`@`%` IDENTIFIED BY '{$password}'");
|
||||
|
||||
$grants = implode(', ', static::$grants);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue