Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Adapter/Doctrine/DoctrineTenantDatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function getTenantDbListByDatabaseStatus(DatabaseStatusEnum $status): arr
);
}

public function getTenantDatabaseById(int $identifier): TenantConnectionConfigDTO
public function getTenantDatabaseById(mixed $identifier): TenantConnectionConfigDTO
{
$tenantDbConfig = $this->tenantDatabaseRepository->findOneBy([$this->tenantDbIdentifier => $identifier]);
if (null === $tenantDbConfig) {
Expand Down Expand Up @@ -109,7 +109,7 @@ public function createTenantDatabase(TenantConnectionConfigDTO $tenantConnection
}
}

public function updateTenantDatabaseStatus(int $identifier, DatabaseStatusEnum $status): bool
public function updateTenantDatabaseStatus(mixed $identifier, DatabaseStatusEnum $status): bool
{
$tenantDbConfig = $this->tenantDatabaseRepository->findOneBy([$this->tenantDbIdentifier => $identifier]);
if (null === $tenantDbConfig) {
Expand Down Expand Up @@ -142,7 +142,7 @@ public function addNewTenantDbConfig(TenantConnectionConfigDTO $dto): TenantConn
private function convertToDTO(TenantDbConfigurationInterface $dbConfig): TenantConnectionConfigDTO
{
return TenantConnectionConfigDTO::fromArgs(
identifier: $dbConfig->getId() ?? null,
identifier: $dbConfig->getIdentifierValue() ?? null,
driver: $dbConfig->getDriverType(),
dbStatus: $dbConfig->getDatabaseStatus(),
host: $dbConfig->getDbHost(),
Expand Down
56 changes: 56 additions & 0 deletions src/Command/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,36 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$dbId = $input->getArgument('dbId');
$migrationType = $input->getArgument('type');
$io = new SymfonyStyle($input, $output);

if ($dbId !== null) {
$io->warning('DEPRECATION: The "dbId" argument is deprecated and will be removed in v4.0.');
$io->note(sprintf('Migrating specific database with identifier: %s', $dbId));
$io->newLine();

try {
$tenantDb = $this->tenantDatabaseManager->getTenantDatabaseById($dbId);

if ($migrationType === self::MIGRATE_TYPE_INIT && $tenantDb->dbStatus !== DatabaseStatusEnum::DATABASE_CREATED) {
$io->error(sprintf('Database "%s" is not in CREATED status. Current status: %s', $dbId, $tenantDb->dbStatus->value));
return 1;
}

if ($migrationType === self::MIGRATE_TYPE_UPDATE && $tenantDb->dbStatus !== DatabaseStatusEnum::DATABASE_MIGRATED) {
$io->error(sprintf('Database "%s" is not in MIGRATED status. Current status: %s', $dbId, $tenantDb->dbStatus->value));
return 1;
}

return $this->migrateSingleDB($input, $output, $io, $tenantDb);

} catch (\RuntimeException $e) {
$io->error(sprintf('Tenant database with identifier "%s" not found: %s', $dbId, $e->getMessage()));
return 1;
}
}
// Migrate all databases based on the type
switch ($input->getArgument('type')) {
case self::MIGRATE_TYPE_INIT:
$io->note('Migrating the new databases');
Expand Down Expand Up @@ -120,4 +149,31 @@ private function runMigrateCommand(InputInterface $input, OutputInterface $outpu
$otherCommand = new \Doctrine\Migrations\Tools\Console\Command\MigrateCommand($this->getDependencyFactory($input));
$otherCommand->run($newInput, $output);
}

private function migrateSingleDB(InputInterface $input, OutputInterface $output, SymfonyStyle $io, TenantConnectionConfigDTO $tenantDb): int
{
try {
// we already checked that dbId is not null or add it in the loop
$io->note(sprintf('Start Migrating database with identifier "%s" (Database: %s, Host: %s)',
$tenantDb->identifier, $tenantDb->dbname, $tenantDb->host));
$io->newLine();

$this->runMigrateCommand($input, $output);

// Update database status if this was an init migration
if ($tenantDb->dbStatus === DatabaseStatusEnum::DATABASE_CREATED) {
$this->tenantDatabaseManager->updateTenantDatabaseStatus(
$tenantDb->identifier,
DatabaseStatusEnum::DATABASE_MIGRATED
);
$this->registry->getManager()->flush();
}

$io->success(sprintf('Database with identifier "%s" migrated successfully.', $tenantDb->identifier));
return 0;
} catch (Throwable $e) {
$io->error(sprintf('Failed to migrate database with identifier "%s": %s', $tenantDb->identifier, $e->getMessage()));
return 1;
}
}
}
4 changes: 2 additions & 2 deletions src/Config/TenantConnectionConfigDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class TenantConnectionConfigDTO
{
private function __construct(
public ?int $identifier,
public mixed $identifier,
public DriverTypeEnum $driver,
public DatabaseStatusEnum $dbStatus,
public string $host,
Expand All @@ -24,7 +24,7 @@ private function __construct(
}

public static function fromArgs(
?int $identifier,
mixed $identifier,
DriverTypeEnum $driver,
DatabaseStatusEnum $dbStatus,
string $host,
Expand Down
6 changes: 6 additions & 0 deletions src/Services/TenantDbConfigurationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,10 @@ public function setDatabaseStatus(DatabaseStatusEnum $databaseStatus): self;
public function getDsnUrl(): string;

public function getDriverType(): DriverTypeEnum;

/**
* Get the tenant identifier value as configured in the system.
* This should return the value of the field specified in tenant_database_identifier config.
*/
public function getIdentifierValue(): mixed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function testListDatabasesReturnsDtos(): void
{
$entity1 = $this->createConfiguredMock(TenantDbConfigurationInterface::class, [
'getId' => 12,
'getIdentifierValue' => 12,
'getDriverType' => DriverTypeEnum::MYSQL,
'getDbHost' => 'h',
'getDatabaseStatus' => DatabaseStatusEnum::DATABASE_MIGRATED,
Expand Down Expand Up @@ -79,6 +80,7 @@ public function testListMissingDatabasesReturnsDtos(): void
{
$entity = $this->createConfiguredMock(TenantDbConfigurationInterface::class, [
'getId' => 11,
'getIdentifierValue' =>"Tenant ID",
'getDriverType' => DriverTypeEnum::MYSQL,
'getDatabaseStatus' => DatabaseStatusEnum::DATABASE_NOT_CREATED,
'getDbHost' => 'h',
Expand All @@ -91,7 +93,7 @@ public function testListMissingDatabasesReturnsDtos(): void
->willReturn([$entity]);
$result = $this->manager->listMissingDatabases();
$this->assertCount(1, $result);
$this->assertSame(11, $result[0]->identifier);
$this->assertSame('Tenant ID', $result[0]->identifier);
}

public function testListMissingDatabasesThrowsIfEmpty(): void
Expand All @@ -106,6 +108,7 @@ public function testGetDefaultTenantIDatabaseReturnsDto(): void
{
$entity = $this->createConfiguredMock(TenantDbConfigurationInterface::class, [
'getId' => 13,
'getIdentifierValue' => 13,
'getDriverType' => DriverTypeEnum::MYSQL,
'getDbHost' => 'h',
'getDatabaseStatus' => DatabaseStatusEnum::DATABASE_CREATED,
Expand Down Expand Up @@ -170,6 +173,7 @@ public function testGetTenantDatabaseByIdReturnsDto(): void
{
$entity = $this->createConfiguredMock(TenantDbConfigurationInterface::class, [
'getId' => 42,
'getIdentifierValue' => 42,
'getDriverType' => DriverTypeEnum::MYSQL,
'getDbHost' => 'host',
'getDatabaseStatus' => DatabaseStatusEnum::DATABASE_NOT_CREATED,
Expand Down
5 changes: 5 additions & 0 deletions tests/Unit/EventListener/DbSwitchEventListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public function getDbName(): string
return $this->dbName;
}

public function getIdentifierValue(): mixed
{
return 1;
}

public function setDbName(string $dbName): void
{
$this->dbName = $dbName;
Expand Down
Loading