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
4 changes: 2 additions & 2 deletions src/Command/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@
->setHelp('This command generates new migration file.');
}

protected function getMissingDatabaseOptionPolicy(InputInterface $input): MissingDatabaseOptionPolicy
protected function resolveDatabaseNamesWithMissingOption(InputInterface $input): array
{
return MissingDatabaseOptionPolicy::ProcessDefaultDatabase;
return [DatabaseRegistry::DEFAULT_DATABASE];
}

protected function createDefaultContext(): CreateContext
Expand All @@ -121,7 +121,7 @@
object $context,
): int {
$io = new SymfonyStyle($input, $output);
$context->migrator->setIo($io);

Check warning on line 124 in src/Command/CreateCommand.php

View workflow job for this annotation

GitHub Actions / tests / PHP 8.5

Escaped Mutant for Mutator "MethodCallRemoval": @@ @@ object $context, ): int { $io = new SymfonyStyle($input, $output); - $context->migrator->setIo($io); + $context->migrationService->setIo($io); $context->createService->setIo($io);
$context->migrationService->setIo($io);
$context->createService->setIo($io);

Expand All @@ -147,7 +147,7 @@
/** @var string $table */
$table = $input->getArgument('name');

if (!preg_match('/^[\w\\\\]+$/', $table)) {

Check warning on line 150 in src/Command/CreateCommand.php

View workflow job for this annotation

GitHub Actions / tests / PHP 8.5

Escaped Mutant for Mutator "PregMatchRemoveCaret": @@ @@ /** @var string $table */ $table = $input->getArgument('name'); - if (!preg_match('/^[\w\\\\]+$/', $table)) { + if (!preg_match('/[\w\\\]+$/', $table)) { $io->error( 'The migration name should contain letters, digits, underscore and/or backslash characters only.', );
$io->error(
'The migration name should contain letters, digits, underscore and/or backslash characters only.',
);
Expand All @@ -173,7 +173,7 @@
$className = $context->migrationService->generateClassName($name);
$nameLimit = $context->migrator->getMigrationNameLimit();

if ($nameLimit !== 0 && strlen($className) > $nameLimit) {

Check warning on line 176 in src/Command/CreateCommand.php

View workflow job for this annotation

GitHub Actions / tests / PHP 8.5

Escaped Mutant for Mutator "DecrementInteger": @@ @@ $className = $context->migrationService->generateClassName($name); $nameLimit = $context->migrator->getMigrationNameLimit(); - if ($nameLimit !== 0 && strlen($className) > $nameLimit) { + if ($nameLimit !== -1 && strlen($className) > $nameLimit) { $io->error('The migration name is too long.'); return Command::INVALID;
$io->error('The migration name is too long.');

return Command::INVALID;
Expand Down Expand Up @@ -213,7 +213,7 @@
file_put_contents($file, $content, LOCK_EX);

$output->writeln("\n\t<info>$className</info>");
$output->writeln("\n");

Check warning on line 216 in src/Command/CreateCommand.php

View workflow job for this annotation

GitHub Actions / tests / PHP 8.5

Escaped Mutant for Mutator "MethodCallRemoval": @@ @@ file_put_contents($file, $content, LOCK_EX); $output->writeln("\n\t<info>$className</info>"); - $output->writeln("\n"); + $io->success('New migration created successfully.'); return Command::SUCCESS;
$io->success('New migration created successfully.');

return Command::SUCCESS;
Expand All @@ -223,12 +223,12 @@
{
$result = '';

return match ($command) {

Check warning on line 226 in src/Command/CreateCommand.php

View workflow job for this annotation

GitHub Actions / tests / PHP 8.5

Escaped Mutant for Mutator "MatchArmRemoval": @@ @@ 'addColumn' => 'Add_Column_' . $name, 'dropColumn' => 'Drop_Column_' . $name, 'junction' => 'Junction_Table_For_' . $name . '_And_' . (string) $and . '_Tables', - default => $result, }; } }
'create' => $name,
'table' => 'Create_' . $name . '_Table',
'dropTable' => 'Drop_' . $name . '_Table',
'addColumn' => 'Add_Column_' . $name,

Check warning on line 230 in src/Command/CreateCommand.php

View workflow job for this annotation

GitHub Actions / tests / PHP 8.5

Escaped Mutant for Mutator "ConcatOperandRemoval": @@ @@ 'create' => $name, 'table' => 'Create_' . $name . '_Table', 'dropTable' => 'Drop_' . $name . '_Table', - 'addColumn' => 'Add_Column_' . $name, + 'addColumn' => 'Add_Column_', 'dropColumn' => 'Drop_Column_' . $name, 'junction' => 'Junction_Table_For_' . $name . '_And_' . (string) $and . '_Tables', default => $result,

Check warning on line 230 in src/Command/CreateCommand.php

View workflow job for this annotation

GitHub Actions / tests / PHP 8.5

Escaped Mutant for Mutator "ConcatOperandRemoval": @@ @@ 'create' => $name, 'table' => 'Create_' . $name . '_Table', 'dropTable' => 'Drop_' . $name . '_Table', - 'addColumn' => 'Add_Column_' . $name, + 'addColumn' => $name, 'dropColumn' => 'Drop_Column_' . $name, 'junction' => 'Junction_Table_For_' . $name . '_And_' . (string) $and . '_Tables', default => $result,

Check warning on line 230 in src/Command/CreateCommand.php

View workflow job for this annotation

GitHub Actions / tests / PHP 8.5

Escaped Mutant for Mutator "Concat": @@ @@ 'create' => $name, 'table' => 'Create_' . $name . '_Table', 'dropTable' => 'Drop_' . $name . '_Table', - 'addColumn' => 'Add_Column_' . $name, + 'addColumn' => $name . 'Add_Column_', 'dropColumn' => 'Drop_Column_' . $name, 'junction' => 'Junction_Table_For_' . $name . '_And_' . (string) $and . '_Tables', default => $result,
'dropColumn' => 'Drop_Column_' . $name,

Check warning on line 231 in src/Command/CreateCommand.php

View workflow job for this annotation

GitHub Actions / tests / PHP 8.5

Escaped Mutant for Mutator "ConcatOperandRemoval": @@ @@ 'table' => 'Create_' . $name . '_Table', 'dropTable' => 'Drop_' . $name . '_Table', 'addColumn' => 'Add_Column_' . $name, - 'dropColumn' => 'Drop_Column_' . $name, + 'dropColumn' => $name, 'junction' => 'Junction_Table_For_' . $name . '_And_' . (string) $and . '_Tables', default => $result, };

Check warning on line 231 in src/Command/CreateCommand.php

View workflow job for this annotation

GitHub Actions / tests / PHP 8.5

Escaped Mutant for Mutator "Concat": @@ @@ 'table' => 'Create_' . $name . '_Table', 'dropTable' => 'Drop_' . $name . '_Table', 'addColumn' => 'Add_Column_' . $name, - 'dropColumn' => 'Drop_Column_' . $name, + 'dropColumn' => $name . 'Drop_Column_', 'junction' => 'Junction_Table_For_' . $name . '_And_' . (string) $and . '_Tables', default => $result, };
'junction' => 'Junction_Table_For_' . $name . '_And_' . (string) $and . '_Tables',
default => $result,
};
Expand Down
64 changes: 36 additions & 28 deletions src/Command/DatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Yiisoft\Db\Migration\DatabaseRegistry;
use Yiisoft\Db\Migration\DatabaseSetup;

use function count;
use function implode;
use function in_array;
use function is_string;
Expand All @@ -33,22 +32,22 @@ public function __construct(private readonly ?DatabaseRegistry $additionalDataba

final protected function execute(InputInterface $input, OutputInterface $output): int
{
$availableNames = [DatabaseRegistry::DEFAULT_DATABASE, ...($this->additionalDatabases?->getNames() ?? [])];
$multipleDatabases = count($availableNames) > 1;
$io = new SymfonyStyle($input, $output);

$names = $this->resolveDatabaseNames($input, $availableNames, $multipleDatabases);
$names = $this->resolveDatabaseNames($input);
if (is_string($names)) {
$io->error($names);
return Command::INVALID;
}

$multipleDatabases = $this->isMultipleDatabases();
$hasResults = false;

foreach ($names as $name) {
if ($this->additionalDatabases === null || $name === DatabaseRegistry::DEFAULT_DATABASE) {
if (!$multipleDatabases || $name === DatabaseRegistry::DEFAULT_DATABASE) {
$context = $this->createDefaultContext();
} else {
/** @psalm-var DatabaseRegistry $this->additionalDatabases */
$context = $this->createContext($this->additionalDatabases->createSetup($name));
}

Expand All @@ -70,15 +69,20 @@ final protected function execute(InputInterface $input, OutputInterface $output)
}

/**
* Returns how the command behaves when the `--db` option isn't specified.
*/
abstract protected function getMissingDatabaseOptionPolicy(InputInterface $input): MissingDatabaseOptionPolicy;

/**
* Returns the error message shown when the `--db` option is required, but isn't specified.
* Resolves the database names or returns a message indicating that an option is missing if multiple databases
* are configured.
*
* @param InputInterface $input The input interface containing the command options and arguments.
*
* @return string[]|string
* @psalm-return non-empty-list<string>|string
*/
protected function getMissingDatabaseOptionError(): string
protected function resolveDatabaseNamesWithMissingOption(InputInterface $input): array|string
{
if (!$this->isMultipleDatabases()) {
return [DatabaseRegistry::DEFAULT_DATABASE];
}

return 'The --db option is required when multiple databases are configured.';
}

Expand Down Expand Up @@ -110,34 +114,38 @@ abstract protected function executeForDatabase(
object $context,
): ?int;

/**
* @return string[]
* @psalm-return non-empty-list<string>
*/
protected function getAvailableDatabaseNames(): array
{
return [DatabaseRegistry::DEFAULT_DATABASE, ...($this->additionalDatabases?->getNames() ?? [])];
}

protected function isMultipleDatabases(): bool
{
return $this->additionalDatabases !== null && $this->additionalDatabases->getNames() !== [];
}

/**
* Resolves names of databases to process.
*
* @param string[] $availableNames
* @return string[]|string The database names, or an error message if the databases can't be resolved.
*
* @psalm-param list<string> $availableNames
* @psalm-return list<string>|string
* @psalm-return non-empty-list<string>|string
*/
private function resolveDatabaseNames(
InputInterface $input,
array $availableNames,
bool $multipleDatabases,
): array|string {
private function resolveDatabaseNames(InputInterface $input): array|string
{
/** @var string|null $name */
$name = $input->getOption('db');

if ($name === null) {
$policy = $this->getMissingDatabaseOptionPolicy($input);
if ($multipleDatabases && $policy === MissingDatabaseOptionPolicy::RejectIfMultiple) {
return $this->getMissingDatabaseOptionError();
}

return $policy === MissingDatabaseOptionPolicy::ProcessDefaultDatabase
? [DatabaseRegistry::DEFAULT_DATABASE]
: $availableNames;
return $this->resolveDatabaseNamesWithMissingOption($input);
}

$availableNames = $this->getAvailableDatabaseNames();

if (!in_array($name, $availableNames, true)) {
return 'Unknown database "' . OutputFormatter::escape($name) . '". Available databases: '
. OutputFormatter::escape(implode(', ', $availableNames)) . '.';
Expand Down
5 changes: 0 additions & 5 deletions src/Command/DownCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ protected function configure(): void
->addOption('force-yes', 'y', InputOption::VALUE_NONE, 'Force yes to all questions.');
}

protected function getMissingDatabaseOptionPolicy(InputInterface $input): MissingDatabaseOptionPolicy
{
return MissingDatabaseOptionPolicy::RejectIfMultiple;
}

protected function createDefaultContext(): DownContext
{
return new DownContext($this->migrator, $this->migrationService, $this->downRunner);
Expand Down
4 changes: 2 additions & 2 deletions src/Command/HistoryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ protected function configure(): void
->addOption('all', 'a', InputOption::VALUE_NONE, 'All migrations.');
}

protected function getMissingDatabaseOptionPolicy(InputInterface $input): MissingDatabaseOptionPolicy
protected function resolveDatabaseNamesWithMissingOption(InputInterface $input): array
{
return MissingDatabaseOptionPolicy::ProcessAllDatabases;
return $this->getAvailableDatabaseNames();
}

protected function createDefaultContext(): MigrationContext
Expand Down
5 changes: 0 additions & 5 deletions src/Command/MarkCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ protected function configure(): void
->addOption('force-yes', 'y', InputOption::VALUE_NONE, 'Force yes to all questions.');
}

protected function getMissingDatabaseOptionPolicy(InputInterface $input): MissingDatabaseOptionPolicy
{
return MissingDatabaseOptionPolicy::RejectIfMultiple;
}

protected function createDefaultContext(): MigrationContext
{
return new MigrationContext($this->migrator, $this->migrationService);
Expand Down
28 changes: 0 additions & 28 deletions src/Command/MissingDatabaseOptionPolicy.php

This file was deleted.

15 changes: 7 additions & 8 deletions src/Command/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,15 @@ protected function configure(): void
->addOption('namespace', 'ns', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Namespace of migrations to display.');
}

protected function getMissingDatabaseOptionPolicy(InputInterface $input): MissingDatabaseOptionPolicy
protected function resolveDatabaseNamesWithMissingOption(InputInterface $input): array|string
{
return $input->getOption('path') !== [] || $input->getOption('namespace') !== []
? MissingDatabaseOptionPolicy::RejectIfMultiple
: MissingDatabaseOptionPolicy::ProcessAllDatabases;
}
if (!$this->isMultipleDatabases()) {
return [DatabaseRegistry::DEFAULT_DATABASE];
}

protected function getMissingDatabaseOptionError(): string
{
return 'The --db option is required with --path or --namespace when multiple databases are configured.';
return $input->getOption('path') !== [] || $input->getOption('namespace') !== []
? 'The --db option is required with --path or --namespace when multiple databases are configured.'
: $this->getAvailableDatabaseNames();
}

protected function createDefaultContext(): MigrationContext
Expand Down
5 changes: 0 additions & 5 deletions src/Command/RedoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ protected function configure(): void
->addOption('force-yes', 'y', InputOption::VALUE_NONE, 'Force yes to all questions.');
}

protected function getMissingDatabaseOptionPolicy(InputInterface $input): MissingDatabaseOptionPolicy
{
return MissingDatabaseOptionPolicy::RejectIfMultiple;
}

protected function createDefaultContext(): RedoContext
{
return new RedoContext($this->migrator, $this->migrationService, $this->downRunner, $this->updateRunner);
Expand Down
15 changes: 7 additions & 8 deletions src/Command/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,15 @@ protected function configure(): void
->addOption('force-yes', 'y', InputOption::VALUE_NONE, 'Force yes to all questions.');
}

protected function getMissingDatabaseOptionPolicy(InputInterface $input): MissingDatabaseOptionPolicy
protected function resolveDatabaseNamesWithMissingOption(InputInterface $input): array|string
{
return $input->getOption('path') !== [] || $input->getOption('namespace') !== []
? MissingDatabaseOptionPolicy::RejectIfMultiple
: MissingDatabaseOptionPolicy::ProcessAllDatabases;
}
if (!$this->isMultipleDatabases()) {
return [DatabaseRegistry::DEFAULT_DATABASE];
}

protected function getMissingDatabaseOptionError(): string
{
return 'The --db option is required with --path or --namespace when multiple databases are configured.';
return $input->getOption('path') !== [] || $input->getOption('namespace') !== []
? 'The --db option is required with --path or --namespace when multiple databases are configured.'
: $this->getAvailableDatabaseNames();
}

protected function createDefaultContext(): UpdateContext
Expand Down
Loading