From d31cbcda43c9389ac631816ca2385180306ae6f4 Mon Sep 17 00:00:00 2001 From: Dmitriy Krivopalov Date: Sun, 8 Mar 2026 17:57:00 +0300 Subject: [PATCH 1/3] naming correction --- example/mysql.php | 6 +- example/presentation/CommandOptions.php | 6 +- example/presentation/CreateCommand.php | 2 +- example/presentation/DownCommand.php | 2 +- example/presentation/FixtureCommand.php | 2 +- example/presentation/RedoCommand.php | 2 +- example/presentation/UpCommand.php | 2 +- example/presentation/VerifyCommand.php | 2 +- src/{InputArgs.php => InputOptions.php} | 2 +- src/Migrator.php | 14 ++-- src/MigratorInterface.php | 12 ++-- src/internal/action/Workflow.php | 65 ++++++++++++------- src/internal/command/Command.php | 31 ++++++--- src/internal/command/CommandInterface.php | 2 +- .../command/{Args.php => Options.php} | 8 +-- src/internal/filesystem/Action.php | 12 ++-- .../filesystem/{Args.php => Options.php} | 6 +- src/template/Factory.php | 14 ++-- tests/internal/FilesDownTest.php | 8 +-- tests/internal/FilesFixtureTest.php | 8 +-- tests/internal/FilesUpTest.php | 8 +-- tests/stub/TestCommand.php | 6 +- tests/workflow/ArgumentsTest.php | 32 ++++----- tests/workflow/CommandTest.php | 12 ++-- tests/workflow/ConfigurationTest.php | 6 +- tests/workflow/CreateTest.php | 8 +-- tests/workflow/EventTest.php | 10 +-- tests/workflow/MigrationTest.php | 4 +- tests/workflow/UpExactlyTest.php | 10 +-- tests/workflow/WorkflowTest.php | 4 +- 30 files changed, 168 insertions(+), 138 deletions(-) rename src/{InputArgs.php => InputOptions.php} (98%) rename src/internal/command/{Args.php => Options.php} (75%) rename src/internal/filesystem/{Args.php => Options.php} (74%) diff --git a/example/mysql.php b/example/mysql.php index f414486..545a7a7 100644 --- a/example/mysql.php +++ b/example/mysql.php @@ -4,7 +4,7 @@ use kuaukutsu\poc\migration\internal\connection\PDO\Driver; use kuaukutsu\poc\migration\tools\PrettyConsoleOutput; -use kuaukutsu\poc\migration\InputArgs; +use kuaukutsu\poc\migration\InputOptions; use kuaukutsu\poc\migration\Migration; use kuaukutsu\poc\migration\MigrationCollection; use kuaukutsu\poc\migration\Migrator; @@ -28,7 +28,7 @@ ); foreach (range(1, 1000) as $row) { - $migrator->create(new InputArgs(dbName: "mysql/main", migrationName: $row . "-test")); + $migrator->create(new InputOptions(dbName: "mysql/main", migrationName: $row . "-test")); } try { @@ -38,7 +38,7 @@ } try { - $migrator->up(new InputArgs(limit: 100)); + $migrator->up(new InputOptions(limit: 100)); } catch (Throwable $exception) { echo $exception->getMessage() . PHP_EOL; } diff --git a/example/presentation/CommandOptions.php b/example/presentation/CommandOptions.php index 0cd4413..9cb2e1b 100644 --- a/example/presentation/CommandOptions.php +++ b/example/presentation/CommandOptions.php @@ -6,14 +6,14 @@ use InvalidArgumentException; use Symfony\Component\Console\Input\InputInterface; -use kuaukutsu\poc\migration\InputArgs; +use kuaukutsu\poc\migration\InputOptions; trait CommandOptions { /** * @throws InvalidArgumentException */ - protected function getArguments(InputInterface $input): InputArgs + protected function getOptions(InputInterface $input): InputOptions { $options = []; if ($input->hasOption('limit')) { @@ -44,7 +44,7 @@ protected function getArguments(InputInterface $input): InputArgs $options['migrationName'] = $this->getMigrationName($input); } - return new InputArgs(...$options); + return new InputOptions(...$options); } /** diff --git a/example/presentation/CreateCommand.php b/example/presentation/CreateCommand.php index 3a049dc..a76ebf0 100644 --- a/example/presentation/CreateCommand.php +++ b/example/presentation/CreateCommand.php @@ -47,7 +47,7 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { try { - $this->migrator->create($this->getArguments($input)); + $this->migrator->create($this->getOptions($input)); } catch (InvalidArgumentException | MigratorException $e) { $output->writeln($e->getMessage()); return Command::INVALID; diff --git a/example/presentation/DownCommand.php b/example/presentation/DownCommand.php index f89fc2c..40cdc3b 100644 --- a/example/presentation/DownCommand.php +++ b/example/presentation/DownCommand.php @@ -64,7 +64,7 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { try { - $this->migrator->down($this->getArguments($input)); + $this->migrator->down($this->getOptions($input)); } catch (InvalidArgumentException | MigratorException $e) { if ($e instanceof InitializationException) { $output->writeln('Calling the command "migrate:init" may help fix the error.'); diff --git a/example/presentation/FixtureCommand.php b/example/presentation/FixtureCommand.php index 6c7cc04..ba796e8 100644 --- a/example/presentation/FixtureCommand.php +++ b/example/presentation/FixtureCommand.php @@ -57,7 +57,7 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { try { - $this->migrator->fixture($this->getArguments($input)); + $this->migrator->fixture($this->getOptions($input)); } catch (InvalidArgumentException | MigratorException $e) { $output->writeln($e->getMessage()); return Command::INVALID; diff --git a/example/presentation/RedoCommand.php b/example/presentation/RedoCommand.php index 7537f83..cedf3a9 100644 --- a/example/presentation/RedoCommand.php +++ b/example/presentation/RedoCommand.php @@ -64,7 +64,7 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { try { - $this->migrator->redo($this->getArguments($input)); + $this->migrator->redo($this->getOptions($input)); } catch (InvalidArgumentException | MigratorException $e) { if ($e instanceof InitializationException) { $output->writeln('Calling the command "migrate:init" may help fix the error.'); diff --git a/example/presentation/UpCommand.php b/example/presentation/UpCommand.php index 95c9ab1..1558cbd 100644 --- a/example/presentation/UpCommand.php +++ b/example/presentation/UpCommand.php @@ -72,7 +72,7 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { try { - $this->migrator->up($this->getArguments($input)); + $this->migrator->up($this->getOptions($input)); } catch (InvalidArgumentException | MigratorException $e) { if ($e instanceof InitializationException) { $output->writeln('Calling the command "migrate:init" may help fix the error.'); diff --git a/example/presentation/VerifyCommand.php b/example/presentation/VerifyCommand.php index 1a9be4e..11edb7f 100644 --- a/example/presentation/VerifyCommand.php +++ b/example/presentation/VerifyCommand.php @@ -58,7 +58,7 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { try { - $this->migrator->verify($this->getArguments($input)); + $this->migrator->verify($this->getOptions($input)); } catch (InvalidArgumentException | MigratorException $e) { if ($e instanceof InitializationException) { $output->writeln('Calling the command "migrate:init" may help fix the error.'); diff --git a/src/InputArgs.php b/src/InputOptions.php similarity index 98% rename from src/InputArgs.php rename to src/InputOptions.php index 6d9c916..f1cb56e 100644 --- a/src/InputArgs.php +++ b/src/InputOptions.php @@ -8,7 +8,7 @@ * @api * @infection-ignore-all */ -final readonly class InputArgs +final readonly class InputOptions { /** * @param non-negative-int $limit diff --git a/src/Migrator.php b/src/Migrator.php index 6b02048..bf46b52 100644 --- a/src/Migrator.php +++ b/src/Migrator.php @@ -42,7 +42,7 @@ public function init(): void } #[Override] - public function up(InputArgs $args = new InputArgs()): void + public function up(InputOptions $args = new InputOptions()): void { foreach ($this->selectDb($args) as $migration) { $this->actionWorkflow->up($migration, $args); @@ -50,7 +50,7 @@ public function up(InputArgs $args = new InputArgs()): void } #[Override] - public function down(InputArgs $args = new InputArgs()): void + public function down(InputOptions $args = new InputOptions()): void { foreach ($this->selectDb($args) as $migration) { $this->actionWorkflow->down($migration, $args); @@ -58,14 +58,14 @@ public function down(InputArgs $args = new InputArgs()): void } #[Override] - public function redo(InputArgs $args = new InputArgs()): void + public function redo(InputOptions $args = new InputOptions()): void { $this->down($args); $this->up($args->withResetLimit()); } #[Override] - public function verify(InputArgs $args = new InputArgs()): void + public function verify(InputOptions $args = new InputOptions()): void { foreach ($this->selectDb($args) as $migration) { $version = $this->actionWorkflow->up( @@ -83,7 +83,7 @@ public function verify(InputArgs $args = new InputArgs()): void } #[Override] - public function fixture(InputArgs $args = new InputArgs()): void + public function fixture(InputOptions $args = new InputOptions()): void { foreach ($this->selectDb($args) as $migration) { $this->actionWorkflow->fixture($migration, $args); @@ -91,7 +91,7 @@ public function fixture(InputArgs $args = new InputArgs()): void } #[Override] - public function create(InputArgs $args = new InputArgs()): void + public function create(InputOptions $args = new InputOptions()): void { if ($args->dbName === null) { throw new ConfigurationException( @@ -114,7 +114,7 @@ public function create(InputArgs $args = new InputArgs()): void * @return iterable * @throws ConfigurationException */ - private function selectDb(InputArgs $args): iterable + private function selectDb(InputOptions $args): iterable { if ($args->dbName === null) { return $this->collection; diff --git a/src/MigratorInterface.php b/src/MigratorInterface.php index 3ad32a0..1e597dc 100644 --- a/src/MigratorInterface.php +++ b/src/MigratorInterface.php @@ -24,7 +24,7 @@ public function init(): void; * @throws ConnectionException * @throws InitializationException initialization step is required */ - public function up(InputArgs $args = new InputArgs()): void; + public function up(InputOptions $args = new InputOptions()): void; /** * @throws ActionException @@ -32,7 +32,7 @@ public function up(InputArgs $args = new InputArgs()): void; * @throws ConnectionException * @throws InitializationException initialization step is required */ - public function down(InputArgs $args = new InputArgs()): void; + public function down(InputOptions $args = new InputOptions()): void; /** * @throws ActionException @@ -40,7 +40,7 @@ public function down(InputArgs $args = new InputArgs()): void; * @throws ConnectionException * @throws InitializationException initialization step is required */ - public function redo(InputArgs $args = new InputArgs()): void; + public function redo(InputOptions $args = new InputOptions()): void; /** * @throws ActionException @@ -48,17 +48,17 @@ public function redo(InputArgs $args = new InputArgs()): void; * @throws ConnectionException * @throws InitializationException initialization step is required */ - public function verify(InputArgs $args = new InputArgs()): void; + public function verify(InputOptions $args = new InputOptions()): void; /** * @throws ActionException * @throws ConfigurationException If the driver is not implemented * @throws ConnectionException */ - public function fixture(InputArgs $args = new InputArgs()): void; + public function fixture(InputOptions $args = new InputOptions()): void; /** * @throws ConfigurationException */ - public function create(InputArgs $args = new InputArgs()): void; + public function create(InputOptions $args = new InputOptions()): void; } diff --git a/src/internal/action/Workflow.php b/src/internal/action/Workflow.php index 7c7e766..211c847 100644 --- a/src/internal/action/Workflow.php +++ b/src/internal/action/Workflow.php @@ -21,7 +21,7 @@ use kuaukutsu\poc\migration\internal\command\CommandInterface; use kuaukutsu\poc\migration\internal\filesystem; use kuaukutsu\poc\migration\Context; -use kuaukutsu\poc\migration\InputArgs; +use kuaukutsu\poc\migration\InputOptions; use kuaukutsu\poc\migration\Migration; /** @@ -40,14 +40,14 @@ public function __construct(private EventDispatcher $eventDispatcher) * @throws ConnectionException * @throws InitializationException */ - public function up(Migration $migration, InputArgs $args): int + public function up(Migration $migration, InputOptions $options): int { $command = $this->makeCommand($migration); - $appliedMigrations = $this->getAppliedMigrations($migration, $command, new command\Args()); + $appliedMigrations = $this->getAppliedMigrations($migration, $command, new command\Options()); $fsHandler = static fn(filesystem\Action $fs): Iterator => $fs->up( $appliedMigrations, - filesystem\Args::makeFromInput($args) + filesystem\Options::makeFromInput($options) ); $version = generateVersion(); @@ -60,20 +60,20 @@ public function up(Migration $migration, InputArgs $args): int filename: $filename, query: $query, version: $version, - dryRun: $args->dryRun, + dryRun: $options->dryRun, ), EventAction::up, ); } catch (ActionException $exception) { - if ($args->exactlyAll) { - $this->down($migration, new InputArgs(version: $version)); + if ($options->exactlyAll) { + $this->down($migration, new InputOptions(version: $version)); } throw $exception; } } - if ($args->hasRepeatable()) { + if ($options->hasRepeatable()) { $this->repeatable($migration, $command, $version); } @@ -86,11 +86,18 @@ public function up(Migration $migration, InputArgs $args): int * @throws ConnectionException * @throws InitializationException */ - public function down(Migration $migration, InputArgs $args): void + public function down(Migration $migration, InputOptions $options): void { $command = $this->makeCommand($migration); - $appliedMigrations = $this->getAppliedMigrations($migration, $command, command\Args::makeFromInput($args)); + $commandOptions = command\Options::makeFromInput($options); + if ($options->hasApplyLatestVersion()) { + $commandOptions = $commandOptions->withVersion( + $this->getLastVersion($migration, $command) + ); + } + + $appliedMigrations = $this->getAppliedMigrations($migration, $command, $commandOptions); $fsHandler = static fn(filesystem\Action $fs): Iterator => $fs->down($appliedMigrations); foreach ($this->iteratorHandler($migration, $fsHandler) as $filename => $query) { @@ -101,7 +108,7 @@ public function down(Migration $migration, InputArgs $args): void filename: $filename, query: $query, version: $appliedMigrations[$filename], - dryRun: $args->dryRun, + dryRun: $options->dryRun, ), EventAction::down, ); @@ -113,12 +120,12 @@ public function down(Migration $migration, InputArgs $args): void * @throws ConfigurationException * @throws ConnectionException */ - public function fixture(Migration $migration, InputArgs $args): void + public function fixture(Migration $migration, InputOptions $options): void { $command = $this->makeCommand($migration); $fsHandler = static fn(filesystem\Action $fs): Iterator => $fs->fixture( - filesystem\Args::makeFromInput($args) + filesystem\Options::makeFromInput($options) ); foreach ($this->iteratorHandler($migration, $fsHandler) as $filename => $query) { @@ -128,7 +135,7 @@ public function fixture(Migration $migration, InputArgs $args): void dbName: $migration->getName(), filename: $filename, query: $query, - dryRun: $args->dryRun, + dryRun: $options->dryRun, ), EventAction::fixture, ); @@ -217,17 +224,13 @@ private function repeatable(Migration $migration, CommandInterface $command, int * @return array * @throws InitializationException */ - private function getAppliedMigrations(Migration $migration, CommandInterface $command, command\Args $args): array - { + private function getAppliedMigrations( + Migration $migration, + CommandInterface $command, + command\Options $options, + ): array { try { - if ($args->applyLatestVersion) { - $appliedMigrations = $command->fetchApplied(new command\Args(limit: 1)); - if (count($appliedMigrations) === 1) { - $args = $args->withVersion(current($appliedMigrations)); - } - } - - return $command->fetchApplied($args); + return $command->fetchApplied($options); } catch (Throwable $exception) { $this->eventDispatcher->trigger( Event::InitializationError, @@ -238,6 +241,20 @@ private function getAppliedMigrations(Migration $migration, CommandInterface $co } } + /** + * @return non-negative-int + * @throws InitializationException + */ + private function getLastVersion(Migration $migration, CommandInterface $command): int + { + $appliedMigrations = $this->getAppliedMigrations($migration, $command, new command\Options(limit: 1)); + if (count($appliedMigrations) === 1) { + return current($appliedMigrations); + } + + return 0; + } + /** * @param callable(Context $context):bool $handler * @throws ActionException diff --git a/src/internal/command/Command.php b/src/internal/command/Command.php index 7063d9e..95ef9a2 100644 --- a/src/internal/command/Command.php +++ b/src/internal/command/Command.php @@ -21,21 +21,36 @@ public function __construct( } #[Override] - public function fetchApplied(Args $args = new Args()): array + public function fetchApplied(Options $options = new Options()): array { $params = []; - $query = sprintf('SELECT name, version FROM %s', $this->params->table); - if ($args->version > 0) { - $query .= ' WHERE version=:version'; - $params['version'] = $args->version; + $where = ''; + if ($options->version > 0) { + $where = 'WHERE version=:version'; + $params['version'] = $options->version; } - $query .= ' ORDER BY atime DESC, name DESC'; - if ($args->limit > 0) { - $query .= ' LIMIT ' . $args->limit; + $limit = ''; + if ($options->limit > 0) { + $limit = 'LIMIT ' . $options->limit; } + /** @var non-empty-string $query */ + $query = str_replace( + [ + ':table', + '[WHERE]', + '[LIMIT]', + ], + [ + $this->params->table, + $where, + $limit, + ], + 'SELECT name, version FROM :table [WHERE] ORDER BY atime DESC, name DESC [LIMIT]', + ); + return $this->connection->fetchRecord($query, $params); } diff --git a/src/internal/command/CommandInterface.php b/src/internal/command/CommandInterface.php index 10507c4..9715c85 100644 --- a/src/internal/command/CommandInterface.php +++ b/src/internal/command/CommandInterface.php @@ -13,7 +13,7 @@ interface CommandInterface /** * @return array */ - public function fetchApplied(command\Args $args = new command\Args()): array; + public function fetchApplied(command\Options $options = new command\Options()): array; /** * @return bool true: request completed; false: request rejected diff --git a/src/internal/command/Args.php b/src/internal/command/Options.php similarity index 75% rename from src/internal/command/Args.php rename to src/internal/command/Options.php index 221ce57..3287a5e 100644 --- a/src/internal/command/Args.php +++ b/src/internal/command/Options.php @@ -4,12 +4,12 @@ namespace kuaukutsu\poc\migration\internal\command; -use kuaukutsu\poc\migration\InputArgs; +use kuaukutsu\poc\migration\InputOptions; /** * @psalm-internal kuaukutsu\poc\migration */ -final readonly class Args +final readonly class Options { /** * @param non-negative-int $limit @@ -18,18 +18,16 @@ public function __construct( public int $limit = 0, public int $version = 0, - public bool $applyLatestVersion = false, ) { assert($this->limit >= 0); assert($this->version >= 0); } - public static function makeFromInput(InputArgs $args): self + public static function makeFromInput(InputOptions $args): self { return new self( limit: $args->limit, version: $args->version, - applyLatestVersion: $args->hasApplyLatestVersion(), ); } diff --git a/src/internal/filesystem/Action.php b/src/internal/filesystem/Action.php index 4616928..20f2be2 100644 --- a/src/internal/filesystem/Action.php +++ b/src/internal/filesystem/Action.php @@ -32,7 +32,7 @@ public function __construct(string $path) * @return Iterator * @throws ConfigurationException */ - public function up(array $listExcluded, Args $args = new Args()): Iterator + public function up(array $listExcluded, Options $options = new Options()): Iterator { $iternum = 0; foreach ($this->makeIterator($this->path) as $matchFilename) { @@ -42,7 +42,7 @@ public function up(array $listExcluded, Args $args = new Args()): Iterator continue; } - if ($args->limit > 0 && $iternum >= $args->limit) { + if ($options->limit > 0 && $iternum >= $options->limit) { return; } @@ -61,11 +61,11 @@ public function up(array $listExcluded, Args $args = new Args()): Iterator * @return Iterator * @throws ConfigurationException */ - public function down(array $listApplied, Args $args = new Args()): Iterator + public function down(array $listApplied, Options $options = new Options()): Iterator { $iternum = 0; foreach ($listApplied as $filename => $_) { - if ($args->limit > 0 && $iternum >= $args->limit) { + if ($options->limit > 0 && $iternum >= $options->limit) { return; } @@ -82,11 +82,11 @@ public function down(array $listApplied, Args $args = new Args()): Iterator * @return Iterator * @throws ConfigurationException */ - public function fixture(Args $args = new Args()): Iterator + public function fixture(Options $options = new Options()): Iterator { $iternum = 0; foreach ($this->makeIterator(joinBasename($this->path, '-fixture')) as $matchFilename) { - if ($args->limit > 0 && $iternum >= $args->limit) { + if ($options->limit > 0 && $iternum >= $options->limit) { return; } diff --git a/src/internal/filesystem/Args.php b/src/internal/filesystem/Options.php similarity index 74% rename from src/internal/filesystem/Args.php rename to src/internal/filesystem/Options.php index 0e39703..8708404 100644 --- a/src/internal/filesystem/Args.php +++ b/src/internal/filesystem/Options.php @@ -4,12 +4,12 @@ namespace kuaukutsu\poc\migration\internal\filesystem; -use kuaukutsu\poc\migration\InputArgs; +use kuaukutsu\poc\migration\InputOptions; /** * @psalm-internal kuaukutsu\poc\migration */ -final readonly class Args +final readonly class Options { /** * @param non-negative-int $limit @@ -20,7 +20,7 @@ public function __construct( assert($this->limit >= 0); } - public static function makeFromInput(InputArgs $args): self + public static function makeFromInput(InputOptions $args): self { return new self( limit: $args->limit, diff --git a/src/template/Factory.php b/src/template/Factory.php index e715038..d2e82d3 100644 --- a/src/template/Factory.php +++ b/src/template/Factory.php @@ -20,12 +20,12 @@ public function makeName(string $name): string #[Override] public function makeBody(): string { - return << 1, ]; - $iterator = $this->fs->down($savedFilenames, new Args(limit: 1)); + $iterator = $this->fs->down($savedFilenames, new Options(limit: 1)); self::assertTrue($iterator->valid()); $files = []; @@ -52,7 +52,7 @@ public function testLimit(): void self::assertCount(1, $files); self::assertEquals('202501011024_entity_create.sql', $files[0]); - $iterator = $this->fs->up([], new Args(limit: 2)); + $iterator = $this->fs->up([], new Options(limit: 2)); self::assertTrue($iterator->valid()); $files = []; @@ -69,7 +69,7 @@ public function testLimitSkipZero(): void { $savedFilenames = ['202501011024_entity_create.sql' => 1]; - $iterator = $this->fs->down($savedFilenames, new Args(limit: 0)); + $iterator = $this->fs->down($savedFilenames, new Options(limit: 0)); self::assertTrue($iterator->valid()); self::assertNotEmpty(iterator_count($iterator)); } diff --git a/tests/internal/FilesFixtureTest.php b/tests/internal/FilesFixtureTest.php index 7a1e8bd..d7a11ef 100644 --- a/tests/internal/FilesFixtureTest.php +++ b/tests/internal/FilesFixtureTest.php @@ -8,7 +8,7 @@ use PHPUnit\Framework\TestCase; use kuaukutsu\poc\migration\exception\ConfigurationException; use kuaukutsu\poc\migration\internal\filesystem\Action; -use kuaukutsu\poc\migration\internal\filesystem\Args; +use kuaukutsu\poc\migration\internal\filesystem\Options; final class FilesFixtureTest extends TestCase { @@ -34,7 +34,7 @@ public function testFixture(): void public function testLimit(): void { - $iterator = $this->fs->fixture(new Args(limit: 1)); + $iterator = $this->fs->fixture(new Options(limit: 1)); self::assertTrue($iterator->valid()); $files = []; @@ -45,7 +45,7 @@ public function testLimit(): void self::assertCount(1, $files); self::assertEquals('202501011024_entity_base.sql', $files[0]); - $iterator = $this->fs->fixture(new Args(limit: 2)); + $iterator = $this->fs->fixture(new Options(limit: 2)); self::assertTrue($iterator->valid()); $files = []; @@ -60,7 +60,7 @@ public function testLimit(): void public function testLimitSkipZero(): void { - $iterator = $this->fs->fixture(new Args(limit: 0)); + $iterator = $this->fs->fixture(new Options(limit: 0)); self::assertTrue($iterator->valid()); self::assertNotEmpty(iterator_count($iterator)); } diff --git a/tests/internal/FilesUpTest.php b/tests/internal/FilesUpTest.php index d35cfd5..2d93800 100644 --- a/tests/internal/FilesUpTest.php +++ b/tests/internal/FilesUpTest.php @@ -8,7 +8,7 @@ use PHPUnit\Framework\TestCase; use kuaukutsu\poc\migration\exception\ConfigurationException; use kuaukutsu\poc\migration\internal\filesystem\Action; -use kuaukutsu\poc\migration\internal\filesystem\Args; +use kuaukutsu\poc\migration\internal\filesystem\Options; final class FilesUpTest extends TestCase { @@ -34,7 +34,7 @@ public function testUp(): void public function testLimit(): void { - $iterator = $this->fs->up([], new Args(limit: 1)); + $iterator = $this->fs->up([], new Options(limit: 1)); self::assertTrue($iterator->valid()); $files = []; @@ -45,7 +45,7 @@ public function testLimit(): void self::assertCount(1, $files); self::assertEquals('202501011024_entity_create.sql', $files[0]); - $iterator = $this->fs->up([], new Args(limit: 2)); + $iterator = $this->fs->up([], new Options(limit: 2)); self::assertTrue($iterator->valid()); $files = []; @@ -60,7 +60,7 @@ public function testLimit(): void public function testLimitSkipZero(): void { - $iterator = $this->fs->up([], new Args(limit: 0)); + $iterator = $this->fs->up([], new Options(limit: 0)); self::assertTrue($iterator->valid()); self::assertNotEmpty(iterator_count($iterator)); } diff --git a/tests/stub/TestCommand.php b/tests/stub/TestCommand.php index 0128c17..d3248fa 100644 --- a/tests/stub/TestCommand.php +++ b/tests/stub/TestCommand.php @@ -17,10 +17,10 @@ public function __construct( } #[Override] - public function fetchApplied(command\Args $args = new command\Args()): array + public function fetchApplied(command\Options $options = new command\Options()): array { - if ($args->limit > 0) { - return array_slice($this->storage->getMigration(), 0, $args->limit); + if ($options->limit > 0) { + return array_slice($this->storage->getMigration(), 0, $options->limit); } return $this->storage->getMigration(); diff --git a/tests/workflow/ArgumentsTest.php b/tests/workflow/ArgumentsTest.php index a7087bf..a9a9496 100644 --- a/tests/workflow/ArgumentsTest.php +++ b/tests/workflow/ArgumentsTest.php @@ -12,7 +12,7 @@ use kuaukutsu\poc\migration\internal\connection\PDO\Driver; use kuaukutsu\poc\migration\tests\MigratorFactory; use kuaukutsu\poc\migration\MigratorInterface; -use kuaukutsu\poc\migration\InputArgs; +use kuaukutsu\poc\migration\InputOptions; /** * Верхнеуровневая работа приложения. @@ -40,12 +40,12 @@ public function testUpWithLimit(): void $data = $this->command->fetchApplied(); self::assertEmpty($data); - $this->migrator->up(new InputArgs(limit: 1)); + $this->migrator->up(new InputOptions(limit: 1)); $data = $this->command->fetchApplied(); self::assertCount(1, $data); self::assertNotEmpty($data['202501011024_entity_create.sql']); - $this->migrator->up(new InputArgs(limit: 2)); + $this->migrator->up(new InputOptions(limit: 2)); $data = $this->command->fetchApplied(); self::assertCount(3, $data); @@ -64,11 +64,11 @@ public function testDownWithLimit(): void $this->migrator->up(); - $this->migrator->down(new InputArgs(limit: 1)); + $this->migrator->down(new InputOptions(limit: 1)); $data = $this->command->fetchApplied(); self::assertCount(2, $data); - $this->migrator->down(new InputArgs(limit: 2)); + $this->migrator->down(new InputOptions(limit: 2)); $data = $this->command->fetchApplied(); self::assertEmpty($data); } @@ -83,7 +83,7 @@ public function testWithDryRun(): void $data = $this->command->fetchApplied(); self::assertCount(3, $data); - $this->migrator->down(new InputArgs(dryRun: true)); + $this->migrator->down(new InputOptions(dryRun: true)); $data = $this->command->fetchApplied(); self::assertCount(3, $data); @@ -98,7 +98,7 @@ public function testWithDb(): void $data = $this->command->fetchApplied(); self::assertEmpty($data); - $this->migrator->up(new InputArgs(limit: 2, dbName: 'sqlite/memory')); + $this->migrator->up(new InputOptions(limit: 2, dbName: 'sqlite/memory')); $data = $this->command->fetchApplied(); self::assertCount(2, $data); } @@ -108,7 +108,7 @@ public function testWithUnknownDb(): void $this->migrator->init(); $this->expectException(ConfigurationException::class); - $this->migrator->up(new InputArgs(dbName: 'sqlite/unknown')); + $this->migrator->up(new InputOptions(dbName: 'sqlite/unknown')); } public function testDownWithVersion(): void @@ -117,7 +117,7 @@ public function testDownWithVersion(): void $data = $this->command->fetchApplied(); self::assertEmpty($data); - $this->migrator->up(new InputArgs(limit: 2)); + $this->migrator->up(new InputOptions(limit: 2)); $data = $this->command->fetchApplied(); self::assertCount(2, $data); @@ -125,12 +125,12 @@ public function testDownWithVersion(): void self::assertGreaterThan(0, $version); // not found version - $this->migrator->down(new InputArgs(version: 2)); + $this->migrator->down(new InputOptions(version: 2)); $data = $this->command->fetchApplied(); self::assertCount(2, $data); // down with version - $this->migrator->down(new InputArgs(version: $version)); + $this->migrator->down(new InputOptions(version: $version)); $data = $this->command->fetchApplied(); self::assertEmpty($data); } @@ -141,7 +141,7 @@ public function testDownWithLatestVersion(): void $data = $this->command->fetchApplied(); self::assertEmpty($data); - $args = new InputArgs(limit: 1); + $args = new InputOptions(limit: 1); self::assertFalse($args->hasApplyLatestVersion()); $this->migrator->up($args); @@ -158,16 +158,16 @@ public function testDownWithLatestVersion(): void $data = $this->command->fetchApplied(); self::assertCount(3, $data); - $args = new InputArgs(); + $args = new InputOptions(); self::assertFalse($args->hasApplyLatestVersion()); - $args = new InputArgs(version: 111, applyLatestVersion: true); + $args = new InputOptions(version: 111, applyLatestVersion: true); self::assertFalse($args->hasApplyLatestVersion()); - $args = new InputArgs(limit: 1, applyLatestVersion: true); + $args = new InputOptions(limit: 1, applyLatestVersion: true); self::assertFalse($args->hasApplyLatestVersion()); - $args = new InputArgs(applyLatestVersion: true); + $args = new InputOptions(applyLatestVersion: true); self::assertTrue($args->hasApplyLatestVersion()); // down with latest version diff --git a/tests/workflow/CommandTest.php b/tests/workflow/CommandTest.php index ae62aff..f475743 100644 --- a/tests/workflow/CommandTest.php +++ b/tests/workflow/CommandTest.php @@ -10,7 +10,7 @@ use PDOException; use PHPUnit\Framework\Attributes\Depends; use PHPUnit\Framework\TestCase; -use kuaukutsu\poc\migration\internal\command\Args; +use kuaukutsu\poc\migration\internal\command\Options; use kuaukutsu\poc\migration\internal\command\Command; use kuaukutsu\poc\migration\internal\command\CommandInterface; use kuaukutsu\poc\migration\internal\command\Params; @@ -96,13 +96,13 @@ public function testFetchLimit(): void $this->execUp('table3'); $data = $this->command->fetchApplied( - new Args(limit: 1) + new Options(limit: 1) ); self::assertCount(1, $data); self::assertNotEmpty($data['test-table3']); $data = $this->command->fetchApplied( - new Args(limit: 2) + new Options(limit: 2) ); self::assertCount(2, $data); @@ -125,21 +125,21 @@ public function testFetchVersion(): void $this->execUp('table3', 222); $data = $this->command->fetchApplied( - new Args(version: 111) + new Options(version: 111) ); self::assertCount(2, $data); self::assertNotEmpty($data['test-table1']); self::assertNotEmpty($data['test-table2']); $data = $this->command->fetchApplied( - new Args(version: 222) + new Options(version: 222) ); self::assertCount(1, $data); self::assertNotEmpty($data['test-table3']); // сомнительный кейс, но допускаем $data = $this->command->fetchApplied( - new Args(limit: 1, version: 111) + new Options(limit: 1, version: 111) ); self::assertCount(1, $data); self::assertNotEmpty($data['test-table2']); diff --git a/tests/workflow/ConfigurationTest.php b/tests/workflow/ConfigurationTest.php index a7c0687..faf71a3 100644 --- a/tests/workflow/ConfigurationTest.php +++ b/tests/workflow/ConfigurationTest.php @@ -12,7 +12,7 @@ use kuaukutsu\poc\migration\internal\command\Params; use kuaukutsu\poc\migration\internal\connection\PDO\Driver; use kuaukutsu\poc\migration\tests\MigratorFactory; -use kuaukutsu\poc\migration\InputArgs; +use kuaukutsu\poc\migration\InputOptions; final class ConfigurationTest extends TestCase { @@ -71,7 +71,7 @@ public function testActionNotExceptionDryRun(): void $migrator->init(); - $migrator->up(new InputArgs(dryRun: true)); + $migrator->up(new InputOptions(dryRun: true)); $data = $command->fetchApplied(); self::assertEmpty($data); } @@ -114,6 +114,6 @@ public function testCreateMigrationNameException(): void $this->expectException(ConfigurationException::class); $this->expectExceptionMessage('Migration Name must be declared.'); - $migrator->create(new InputArgs(dbName: 'test')); + $migrator->create(new InputOptions(dbName: 'test')); } } diff --git a/tests/workflow/CreateTest.php b/tests/workflow/CreateTest.php index 7cbe617..e3b3995 100644 --- a/tests/workflow/CreateTest.php +++ b/tests/workflow/CreateTest.php @@ -10,7 +10,7 @@ use kuaukutsu\poc\migration\internal\connection\PDO\Driver; use kuaukutsu\poc\migration\exception\ConfigurationException; use kuaukutsu\poc\migration\tests\MigratorFactory; -use kuaukutsu\poc\migration\InputArgs; +use kuaukutsu\poc\migration\InputOptions; final class CreateTest extends TestCase { @@ -29,8 +29,8 @@ public function testCreate(): void $migrator->up(); $countMigration = count($command->fetchApplied()); - $migrator->create(new InputArgs(dbName: 'sqlite/memory', migrationName: 'test')); - $migrator->create(new InputArgs(dbName: 'sqlite/memory', migrationName: '2test')); + $migrator->create(new InputOptions(dbName: 'sqlite/memory', migrationName: 'test')); + $migrator->create(new InputOptions(dbName: 'sqlite/memory', migrationName: '2test')); $migrator->up(); self::assertCount($countMigration + 2, $command->fetchApplied()); @@ -48,7 +48,7 @@ public function testCreateException(): void $this->expectException(ConfigurationException::class); $this->expectExceptionMessageMatches('/^the dir .+ is not writable or does not exist.$/i'); - $migrator->create(new InputArgs(dbName: 'sqlite/memory', migrationName: 'test')); + $migrator->create(new InputOptions(dbName: 'sqlite/memory', migrationName: 'test')); } #[Override] diff --git a/tests/workflow/EventTest.php b/tests/workflow/EventTest.php index 68b4753..5b0df19 100644 --- a/tests/workflow/EventTest.php +++ b/tests/workflow/EventTest.php @@ -8,7 +8,7 @@ use PHPUnit\Framework\TestCase; use kuaukutsu\poc\migration\internal\connection\PDO\Driver; use kuaukutsu\poc\migration\event\Event; -use kuaukutsu\poc\migration\InputArgs; +use kuaukutsu\poc\migration\InputOptions; use kuaukutsu\poc\migration\tests\MigratorFactory; use kuaukutsu\poc\migration\tests\stub\TestSubscriber; @@ -28,7 +28,7 @@ public function testSelectDatabaseError(): void try { $migrator->init(); - $migrator->up(new InputArgs(dbName: 'test')); + $migrator->up(new InputOptions(dbName: 'test')); } catch (Throwable) { } @@ -131,7 +131,7 @@ public function testMigrationDryRun(): void $migrator->init(); try { - $migrator->up(new InputArgs(dryRun: true)); + $migrator->up(new InputOptions(dryRun: true)); } catch (Throwable) { } @@ -160,7 +160,7 @@ public function testMigrationFilesystemNotice(): void ); $migrator->init(); - $migrator->up(new InputArgs(limit: 1, hasRepeatable: true)); + $migrator->up(new InputOptions(limit: 1, hasRepeatable: true)); // event-repeatable: does not exist self::assertStringContainsString( @@ -256,7 +256,7 @@ public function testCreateDirectoryDoesNotExistError(): void $migrator->init(); try { - $migrator->create(new InputArgs(dbName: 'sqlite/memory', migrationName: 'test')); + $migrator->create(new InputOptions(dbName: 'sqlite/memory', migrationName: 'test')); } catch (Throwable) { } diff --git a/tests/workflow/MigrationTest.php b/tests/workflow/MigrationTest.php index 2de1f77..a5bc0cc 100644 --- a/tests/workflow/MigrationTest.php +++ b/tests/workflow/MigrationTest.php @@ -12,7 +12,7 @@ use kuaukutsu\poc\migration\internal\connection\PDO\Driver; use kuaukutsu\poc\migration\tests\MigratorFactory; use kuaukutsu\poc\migration\MigratorInterface; -use kuaukutsu\poc\migration\InputArgs; +use kuaukutsu\poc\migration\InputOptions; /** * Верхнеуровневая работа приложения. @@ -106,7 +106,7 @@ public function testVerify(): void { $this->migrator->init(); - $this->migrator->up(new InputArgs(limit: 1)); + $this->migrator->up(new InputOptions(limit: 1)); $data = $this->command->fetchApplied(); self::assertCount(1, $data); diff --git a/tests/workflow/UpExactlyTest.php b/tests/workflow/UpExactlyTest.php index f72fed7..24b8ef7 100644 --- a/tests/workflow/UpExactlyTest.php +++ b/tests/workflow/UpExactlyTest.php @@ -12,7 +12,7 @@ use kuaukutsu\poc\migration\internal\command\Params; use kuaukutsu\poc\migration\tests\MigratorFactory; use kuaukutsu\poc\migration\MigratorInterface; -use kuaukutsu\poc\migration\InputArgs; +use kuaukutsu\poc\migration\InputOptions; final class UpExactlyTest extends TestCase { @@ -37,7 +37,7 @@ public function testUpExactlyAll(): void $data = $this->command->fetchApplied(); self::assertEmpty($data); - $this->migrator->up(new InputArgs(limit: 1)); + $this->migrator->up(new InputOptions(limit: 1)); $data = $this->command->fetchApplied(); self::assertCount(1, $data); @@ -59,7 +59,7 @@ public function testUpExactlyAll(): void self::assertEmpty($data); try { - $this->migrator->up(new InputArgs(exactlyAll: true)); + $this->migrator->up(new InputOptions(exactlyAll: true)); } catch (ActionException) { } @@ -77,7 +77,7 @@ public function testUpExactlyAllException(): void 'SQLSTATE[HY000]: General error: 1 no such table:' ); - $this->migrator->up(new InputArgs(exactlyAll: true)); + $this->migrator->up(new InputOptions(exactlyAll: true)); } public function testVerify(): void @@ -86,7 +86,7 @@ public function testVerify(): void $data = $this->command->fetchApplied(); self::assertEmpty($data); - $this->migrator->up(new InputArgs(limit: 1)); + $this->migrator->up(new InputOptions(limit: 1)); $data = $this->command->fetchApplied(); self::assertCount(1, $data); diff --git a/tests/workflow/WorkflowTest.php b/tests/workflow/WorkflowTest.php index 0f74388..c5ad699 100644 --- a/tests/workflow/WorkflowTest.php +++ b/tests/workflow/WorkflowTest.php @@ -11,7 +11,7 @@ use kuaukutsu\poc\migration\tests\stub\TestDriver; use kuaukutsu\poc\migration\tests\stub\TestStorage; use kuaukutsu\poc\migration\tests\MigratorFactory; -use kuaukutsu\poc\migration\InputArgs; +use kuaukutsu\poc\migration\InputOptions; use kuaukutsu\poc\migration\MigratorInterface; /** @@ -72,7 +72,7 @@ public function testUp(): void public function testUpWithRepeatable(): void { - $this->migrator->up(new InputArgs(hasRepeatable: true)); + $this->migrator->up(new InputOptions(hasRepeatable: true)); // repeatable self::assertStringContainsString( From 7699cd80c86cce06f5103cdbf1e0f7d4dc07eb71 Mon Sep 17 00:00:00 2001 From: Dmitriy Krivopalov Date: Sun, 8 Mar 2026 19:22:13 +0300 Subject: [PATCH 2/3] command correction --- src/Migration.php | 4 ++-- .../command/CommandInterface.php | 5 ++--- src/{internal => }/command/Options.php | 5 +---- src/connection/DriverInterface.php | 4 ++-- src/internal/action/Workflow.php | 21 ++++++++----------- src/internal/command/Command.php | 2 ++ src/internal/connection/PDO/Driver.php | 4 ++-- tests/stub/TestCommand.php | 6 +++--- tests/stub/TestDriver.php | 4 ++-- tests/workflow/ArgumentsTest.php | 2 +- tests/workflow/CommandTest.php | 4 ++-- tests/workflow/MigrationTest.php | 2 +- tests/workflow/PdoDriverTest.php | 8 +++---- tests/workflow/UpExactlyTest.php | 4 ++-- 14 files changed, 35 insertions(+), 40 deletions(-) rename src/{internal => }/command/CommandInterface.php (78%) rename src/{internal => }/command/Options.php (88%) diff --git a/src/Migration.php b/src/Migration.php index a1cc0df..20699b8 100644 --- a/src/Migration.php +++ b/src/Migration.php @@ -4,9 +4,9 @@ namespace kuaukutsu\poc\migration; +use kuaukutsu\poc\migration\command\CommandInterface; use kuaukutsu\poc\migration\connection\DriverInterface; use kuaukutsu\poc\migration\exception\ConnectionException; -use kuaukutsu\poc\migration\internal\command\CommandInterface; use kuaukutsu\poc\migration\internal\command\Params; /** @@ -29,7 +29,7 @@ public function __construct( public string $table = 'migration', public template\FactoryInterface $templFactory = new template\Factory(), ) { - $this->name = $this->driver->getName() . '/' . $this->driver->getDbName(); + $this->name = $this->driver->getName() . '/' . $this->driver->getSourceName(); } /** diff --git a/src/internal/command/CommandInterface.php b/src/command/CommandInterface.php similarity index 78% rename from src/internal/command/CommandInterface.php rename to src/command/CommandInterface.php index 9715c85..510a5ff 100644 --- a/src/internal/command/CommandInterface.php +++ b/src/command/CommandInterface.php @@ -2,10 +2,9 @@ declare(strict_types=1); -namespace kuaukutsu\poc\migration\internal\command; +namespace kuaukutsu\poc\migration\command; use Throwable; -use kuaukutsu\poc\migration\internal\command; use kuaukutsu\poc\migration\Context; interface CommandInterface @@ -13,7 +12,7 @@ interface CommandInterface /** * @return array */ - public function fetchApplied(command\Options $options = new command\Options()): array; + public function fetchApplied(Options $options = new Options()): array; /** * @return bool true: request completed; false: request rejected diff --git a/src/internal/command/Options.php b/src/command/Options.php similarity index 88% rename from src/internal/command/Options.php rename to src/command/Options.php index 3287a5e..a7b4343 100644 --- a/src/internal/command/Options.php +++ b/src/command/Options.php @@ -2,13 +2,10 @@ declare(strict_types=1); -namespace kuaukutsu\poc\migration\internal\command; +namespace kuaukutsu\poc\migration\command; use kuaukutsu\poc\migration\InputOptions; -/** - * @psalm-internal kuaukutsu\poc\migration - */ final readonly class Options { /** diff --git a/src/connection/DriverInterface.php b/src/connection/DriverInterface.php index eddae4b..afd70ea 100644 --- a/src/connection/DriverInterface.php +++ b/src/connection/DriverInterface.php @@ -4,8 +4,8 @@ namespace kuaukutsu\poc\migration\connection; +use kuaukutsu\poc\migration\command\CommandInterface; use kuaukutsu\poc\migration\exception\ConnectionException; -use kuaukutsu\poc\migration\internal\command\CommandInterface; use kuaukutsu\poc\migration\internal\command\Params; interface DriverInterface @@ -18,7 +18,7 @@ public function getName(): string; /** * @return non-empty-lowercase-string */ - public function getDbName(): string; + public function getSourceName(): string; /** * @return non-empty-string diff --git a/src/internal/action/Workflow.php b/src/internal/action/Workflow.php index 211c847..d647f4c 100644 --- a/src/internal/action/Workflow.php +++ b/src/internal/action/Workflow.php @@ -4,21 +4,21 @@ namespace kuaukutsu\poc\migration\internal\action; -use Iterator; use Throwable; use DateTimeImmutable; -use kuaukutsu\poc\migration\event\ExceptionEvent; +use Iterator; +use kuaukutsu\poc\migration\command\CommandInterface; +use kuaukutsu\poc\migration\command\Options; use kuaukutsu\poc\migration\event\Event; use kuaukutsu\poc\migration\event\EventAction; use kuaukutsu\poc\migration\event\EventDispatcher; +use kuaukutsu\poc\migration\event\ExceptionEvent; use kuaukutsu\poc\migration\event\MigrateErrorEvent; use kuaukutsu\poc\migration\event\MigrateSuccessEvent; use kuaukutsu\poc\migration\exception\ActionException; use kuaukutsu\poc\migration\exception\ConfigurationException; use kuaukutsu\poc\migration\exception\ConnectionException; use kuaukutsu\poc\migration\exception\InitializationException; -use kuaukutsu\poc\migration\internal\command; -use kuaukutsu\poc\migration\internal\command\CommandInterface; use kuaukutsu\poc\migration\internal\filesystem; use kuaukutsu\poc\migration\Context; use kuaukutsu\poc\migration\InputOptions; @@ -44,7 +44,7 @@ public function up(Migration $migration, InputOptions $options): int { $command = $this->makeCommand($migration); - $appliedMigrations = $this->getAppliedMigrations($migration, $command, new command\Options()); + $appliedMigrations = $this->getAppliedMigrations($migration, $command, new Options()); $fsHandler = static fn(filesystem\Action $fs): Iterator => $fs->up( $appliedMigrations, filesystem\Options::makeFromInput($options) @@ -90,7 +90,7 @@ public function down(Migration $migration, InputOptions $options): void { $command = $this->makeCommand($migration); - $commandOptions = command\Options::makeFromInput($options); + $commandOptions = Options::makeFromInput($options); if ($options->hasApplyLatestVersion()) { $commandOptions = $commandOptions->withVersion( $this->getLastVersion($migration, $command) @@ -224,11 +224,8 @@ private function repeatable(Migration $migration, CommandInterface $command, int * @return array * @throws InitializationException */ - private function getAppliedMigrations( - Migration $migration, - CommandInterface $command, - command\Options $options, - ): array { + private function getAppliedMigrations(Migration $migration, CommandInterface $command, Options $options): array + { try { return $command->fetchApplied($options); } catch (Throwable $exception) { @@ -247,7 +244,7 @@ private function getAppliedMigrations( */ private function getLastVersion(Migration $migration, CommandInterface $command): int { - $appliedMigrations = $this->getAppliedMigrations($migration, $command, new command\Options(limit: 1)); + $appliedMigrations = $this->getAppliedMigrations($migration, $command, new Options(limit: 1)); if (count($appliedMigrations) === 1) { return current($appliedMigrations); } diff --git a/src/internal/command/Command.php b/src/internal/command/Command.php index 95ef9a2..81fd4ec 100644 --- a/src/internal/command/Command.php +++ b/src/internal/command/Command.php @@ -6,6 +6,8 @@ use Override; use Throwable; +use kuaukutsu\poc\migration\command\CommandInterface; +use kuaukutsu\poc\migration\command\Options; use kuaukutsu\poc\migration\connection\ConnectionInterface; use kuaukutsu\poc\migration\Context; diff --git a/src/internal/connection/PDO/Driver.php b/src/internal/connection/PDO/Driver.php index cd3f188..2ca1946 100644 --- a/src/internal/connection/PDO/Driver.php +++ b/src/internal/connection/PDO/Driver.php @@ -8,12 +8,12 @@ use Closure; use PDO; use PDOException; +use kuaukutsu\poc\migration\command\CommandInterface; use kuaukutsu\poc\migration\connection\ConnectionInterface; use kuaukutsu\poc\migration\connection\DriverInterface; use kuaukutsu\poc\migration\exception\ConfigurationException; use kuaukutsu\poc\migration\exception\ConnectionException; use kuaukutsu\poc\migration\internal\command\Command; -use kuaukutsu\poc\migration\internal\command\CommandInterface; use kuaukutsu\poc\migration\internal\command\Params; final class Driver implements DriverInterface @@ -62,7 +62,7 @@ public function getName(): string } #[Override] - public function getDbName(): string + public function getSourceName(): string { return $this->dbname; } diff --git a/tests/stub/TestCommand.php b/tests/stub/TestCommand.php index d3248fa..74befd5 100644 --- a/tests/stub/TestCommand.php +++ b/tests/stub/TestCommand.php @@ -5,8 +5,8 @@ namespace kuaukutsu\poc\migration\tests\stub; use Override; -use kuaukutsu\poc\migration\internal\command; -use kuaukutsu\poc\migration\internal\command\CommandInterface; +use kuaukutsu\poc\migration\command\CommandInterface; +use kuaukutsu\poc\migration\command\Options; use kuaukutsu\poc\migration\Context; final readonly class TestCommand implements CommandInterface @@ -17,7 +17,7 @@ public function __construct( } #[Override] - public function fetchApplied(command\Options $options = new command\Options()): array + public function fetchApplied(Options $options = new Options()): array { if ($options->limit > 0) { return array_slice($this->storage->getMigration(), 0, $options->limit); diff --git a/tests/stub/TestDriver.php b/tests/stub/TestDriver.php index 2792323..a2ccc85 100644 --- a/tests/stub/TestDriver.php +++ b/tests/stub/TestDriver.php @@ -5,8 +5,8 @@ namespace kuaukutsu\poc\migration\tests\stub; use Override; +use kuaukutsu\poc\migration\command\CommandInterface; use kuaukutsu\poc\migration\connection\DriverInterface; -use kuaukutsu\poc\migration\internal\command\CommandInterface; use kuaukutsu\poc\migration\internal\command\Params; final readonly class TestDriver implements DriverInterface @@ -21,7 +21,7 @@ public function getName(): string return 'test'; } - public function getDbName(): string + public function getSourceName(): string { return 'storage'; } diff --git a/tests/workflow/ArgumentsTest.php b/tests/workflow/ArgumentsTest.php index a9a9496..c74097d 100644 --- a/tests/workflow/ArgumentsTest.php +++ b/tests/workflow/ArgumentsTest.php @@ -6,8 +6,8 @@ use Override; use PHPUnit\Framework\TestCase; +use kuaukutsu\poc\migration\command\CommandInterface; use kuaukutsu\poc\migration\exception\ConfigurationException; -use kuaukutsu\poc\migration\internal\command\CommandInterface; use kuaukutsu\poc\migration\internal\command\Params; use kuaukutsu\poc\migration\internal\connection\PDO\Driver; use kuaukutsu\poc\migration\tests\MigratorFactory; diff --git a/tests/workflow/CommandTest.php b/tests/workflow/CommandTest.php index f475743..0c1a77a 100644 --- a/tests/workflow/CommandTest.php +++ b/tests/workflow/CommandTest.php @@ -10,9 +10,9 @@ use PDOException; use PHPUnit\Framework\Attributes\Depends; use PHPUnit\Framework\TestCase; -use kuaukutsu\poc\migration\internal\command\Options; +use kuaukutsu\poc\migration\command\CommandInterface; +use kuaukutsu\poc\migration\command\Options; use kuaukutsu\poc\migration\internal\command\Command; -use kuaukutsu\poc\migration\internal\command\CommandInterface; use kuaukutsu\poc\migration\internal\command\Params; use kuaukutsu\poc\migration\internal\connection\PDO\Connection; use kuaukutsu\poc\migration\internal\connection\PDO\Type; diff --git a/tests/workflow/MigrationTest.php b/tests/workflow/MigrationTest.php index a5bc0cc..d5c8dc7 100644 --- a/tests/workflow/MigrationTest.php +++ b/tests/workflow/MigrationTest.php @@ -7,7 +7,7 @@ use Override; use PHPUnit\Framework\Attributes\Depends; use PHPUnit\Framework\TestCase; -use kuaukutsu\poc\migration\internal\command\CommandInterface; +use kuaukutsu\poc\migration\command\CommandInterface; use kuaukutsu\poc\migration\internal\command\Params; use kuaukutsu\poc\migration\internal\connection\PDO\Driver; use kuaukutsu\poc\migration\tests\MigratorFactory; diff --git a/tests/workflow/PdoDriverTest.php b/tests/workflow/PdoDriverTest.php index bd4230f..2d71136 100644 --- a/tests/workflow/PdoDriverTest.php +++ b/tests/workflow/PdoDriverTest.php @@ -17,28 +17,28 @@ public function testTypeDriver(): void ); self::assertEquals('pgsql', $driver->getName()); - self::assertEquals('main', $driver->getDbName()); + self::assertEquals('main', $driver->getSourceName()); $driver = new Driver( dsn: 'MYSQL:host=mysql;dbname=copyDb', ); self::assertEquals('mysql', $driver->getName()); - self::assertEquals('copydb', $driver->getDbName()); + self::assertEquals('copydb', $driver->getSourceName()); $driver = new Driver( dsn: 'sqlite::memory:', ); self::assertEquals('sqlite', $driver->getName()); - self::assertEquals('memory', $driver->getDbName()); + self::assertEquals('memory', $driver->getSourceName()); $driver = new Driver( dsn: 'sqlite:tests/data/sqlite/db.sqlite3', ); self::assertEquals('sqlite', $driver->getName()); - self::assertEquals('db', $driver->getDbName()); + self::assertEquals('db', $driver->getSourceName()); } public function testConfigurationException(): void diff --git a/tests/workflow/UpExactlyTest.php b/tests/workflow/UpExactlyTest.php index 24b8ef7..c84d1bb 100644 --- a/tests/workflow/UpExactlyTest.php +++ b/tests/workflow/UpExactlyTest.php @@ -6,10 +6,10 @@ use Override; use PHPUnit\Framework\TestCase; -use kuaukutsu\poc\migration\internal\connection\PDO\Driver; +use kuaukutsu\poc\migration\command\CommandInterface; use kuaukutsu\poc\migration\exception\ActionException; -use kuaukutsu\poc\migration\internal\command\CommandInterface; use kuaukutsu\poc\migration\internal\command\Params; +use kuaukutsu\poc\migration\internal\connection\PDO\Driver; use kuaukutsu\poc\migration\tests\MigratorFactory; use kuaukutsu\poc\migration\MigratorInterface; use kuaukutsu\poc\migration\InputOptions; From aac492e9078b44e7ba280f169973a3a684409205 Mon Sep 17 00:00:00 2001 From: Dmitriy Krivopalov Date: Sun, 8 Mar 2026 19:43:53 +0300 Subject: [PATCH 3/3] config correction --- README.md | 10 ++-- src/Config.php | 20 +++++++ src/Migration.php | 7 +-- src/Migrator.php | 56 ++++++++++---------- src/MigratorInterface.php | 18 +++---- src/connection/DriverInterface.php | 4 +- src/internal/{command => action}/Command.php | 11 ++-- src/internal/action/Workflow.php | 6 +-- src/internal/command/Params.php | 19 ------- src/internal/connection/PDO/Driver.php | 8 +-- tests/stub/TestDriver.php | 4 +- tests/workflow/ArgumentsTest.php | 6 +-- tests/workflow/CommandTest.php | 6 +-- tests/workflow/ConfigurationTest.php | 6 +-- tests/workflow/CreateTest.php | 6 +-- tests/workflow/MigrationTest.php | 6 +-- tests/workflow/UpExactlyTest.php | 6 +-- 17 files changed, 101 insertions(+), 98 deletions(-) create mode 100644 src/Config.php rename src/internal/{command => action}/Command.php (92%) delete mode 100644 src/internal/command/Params.php diff --git a/README.md b/README.md index b03d0bf..a8250ce 100644 --- a/README.md +++ b/README.md @@ -74,17 +74,19 @@ DROP TABLE IF EXISTS public.entity; ```php use DI\Container; -use kuaukutsu\poc\migration\Migration; -use kuaukutsu\poc\migration\MigrationCollection; use kuaukutsu\poc\migration\internal\connection\PDO\Driver; use kuaukutsu\poc\migration\example\presentation\DownCommand; +use kuaukutsu\poc\migration\example\presentation\CreateCommand; use kuaukutsu\poc\migration\example\presentation\FixtureCommand; +use kuaukutsu\poc\migration\example\presentation\VerifyCommand; use kuaukutsu\poc\migration\example\presentation\InitCommand; use kuaukutsu\poc\migration\example\presentation\RedoCommand; use kuaukutsu\poc\migration\example\presentation\UpCommand; +use kuaukutsu\poc\migration\tools\PrettyConsoleOutput; +use kuaukutsu\poc\migration\Migration; +use kuaukutsu\poc\migration\MigrationCollection; use kuaukutsu\poc\migration\Migrator; use kuaukutsu\poc\migration\MigratorInterface; -use kuaukutsu\poc\migration\tools\PrettyConsoleOutput; use Symfony\Component\Console\Application; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\CommandLoader\ContainerCommandLoader; @@ -120,7 +122,9 @@ $console->setCommandLoader( 'migrate:up' => UpCommand::class, 'migrate:down' => DownCommand::class, 'migrate:redo' => RedoCommand::class, + 'migrate:verify' => VerifyCommand::class, 'migrate:fixture' => FixtureCommand::class, + 'migrate:create' => CreateCommand::class, ], ) ); diff --git a/src/Config.php b/src/Config.php new file mode 100644 index 0000000..6a1df00 --- /dev/null +++ b/src/Config.php @@ -0,0 +1,20 @@ +name = $this->driver->getName() . '/' . $this->driver->getSourceName(); } @@ -53,6 +50,6 @@ public function getSetupPath(): string */ public function getCommand(): CommandInterface { - return $this->driver->makeCommand(new Params(table: $this->table)); + return $this->driver->makeCommand($this->config); } } diff --git a/src/Migrator.php b/src/Migrator.php index bf46b52..26e2c44 100644 --- a/src/Migrator.php +++ b/src/Migrator.php @@ -41,6 +41,26 @@ public function init(): void } } + #[Override] + public function create(InputOptions $args = new InputOptions()): void + { + if ($args->dbName === null) { + throw new ConfigurationException( + "DBName must be declared." + ); + } + + if ($args->migrationName === null) { + throw new ConfigurationException( + "Migration Name must be declared." + ); + } + + foreach ($this->selectDb($args) as $migration) { + $this->actionWorkflow->create($migration, $args->migrationName); + } + } + #[Override] public function up(InputOptions $args = new InputOptions()): void { @@ -57,6 +77,14 @@ public function down(InputOptions $args = new InputOptions()): void } } + #[Override] + public function fixture(InputOptions $args = new InputOptions()): void + { + foreach ($this->selectDb($args) as $migration) { + $this->actionWorkflow->fixture($migration, $args); + } + } + #[Override] public function redo(InputOptions $args = new InputOptions()): void { @@ -82,34 +110,6 @@ public function verify(InputOptions $args = new InputOptions()): void } } - #[Override] - public function fixture(InputOptions $args = new InputOptions()): void - { - foreach ($this->selectDb($args) as $migration) { - $this->actionWorkflow->fixture($migration, $args); - } - } - - #[Override] - public function create(InputOptions $args = new InputOptions()): void - { - if ($args->dbName === null) { - throw new ConfigurationException( - "DBName must be declared." - ); - } - - if ($args->migrationName === null) { - throw new ConfigurationException( - "Migration Name must be declared." - ); - } - - foreach ($this->selectDb($args) as $migration) { - $this->actionWorkflow->create($migration, $args->migrationName); - } - } - /** * @return iterable * @throws ConfigurationException diff --git a/src/MigratorInterface.php b/src/MigratorInterface.php index 1e597dc..d193468 100644 --- a/src/MigratorInterface.php +++ b/src/MigratorInterface.php @@ -18,6 +18,11 @@ interface MigratorInterface */ public function init(): void; + /** + * @throws ConfigurationException + */ + public function create(InputOptions $args = new InputOptions()): void; + /** * @throws ActionException * @throws ConfigurationException If the driver is not implemented @@ -38,9 +43,8 @@ public function down(InputOptions $args = new InputOptions()): void; * @throws ActionException * @throws ConfigurationException If the driver is not implemented * @throws ConnectionException - * @throws InitializationException initialization step is required */ - public function redo(InputOptions $args = new InputOptions()): void; + public function fixture(InputOptions $args = new InputOptions()): void; /** * @throws ActionException @@ -48,17 +52,13 @@ public function redo(InputOptions $args = new InputOptions()): void; * @throws ConnectionException * @throws InitializationException initialization step is required */ - public function verify(InputOptions $args = new InputOptions()): void; + public function redo(InputOptions $args = new InputOptions()): void; /** * @throws ActionException * @throws ConfigurationException If the driver is not implemented * @throws ConnectionException + * @throws InitializationException initialization step is required */ - public function fixture(InputOptions $args = new InputOptions()): void; - - /** - * @throws ConfigurationException - */ - public function create(InputOptions $args = new InputOptions()): void; + public function verify(InputOptions $args = new InputOptions()): void; } diff --git a/src/connection/DriverInterface.php b/src/connection/DriverInterface.php index afd70ea..39515f0 100644 --- a/src/connection/DriverInterface.php +++ b/src/connection/DriverInterface.php @@ -6,7 +6,7 @@ use kuaukutsu\poc\migration\command\CommandInterface; use kuaukutsu\poc\migration\exception\ConnectionException; -use kuaukutsu\poc\migration\internal\command\Params; +use kuaukutsu\poc\migration\Config; interface DriverInterface { @@ -28,5 +28,5 @@ public function getSetupPath(): string; /** * @throws ConnectionException */ - public function makeCommand(Params $params): CommandInterface; + public function makeCommand(Config $config): CommandInterface; } diff --git a/src/internal/command/Command.php b/src/internal/action/Command.php similarity index 92% rename from src/internal/command/Command.php rename to src/internal/action/Command.php index 81fd4ec..3f412d6 100644 --- a/src/internal/command/Command.php +++ b/src/internal/action/Command.php @@ -2,13 +2,14 @@ declare(strict_types=1); -namespace kuaukutsu\poc\migration\internal\command; +namespace kuaukutsu\poc\migration\internal\action; use Override; use Throwable; use kuaukutsu\poc\migration\command\CommandInterface; use kuaukutsu\poc\migration\command\Options; use kuaukutsu\poc\migration\connection\ConnectionInterface; +use kuaukutsu\poc\migration\Config; use kuaukutsu\poc\migration\Context; /** @@ -18,7 +19,7 @@ { public function __construct( private ConnectionInterface $connection, - private Params $params, + private Config $config, ) { } @@ -46,7 +47,7 @@ public function fetchApplied(Options $options = new Options()): array '[LIMIT]', ], [ - $this->params->table, + $this->config->table, $where, $limit, ], @@ -70,7 +71,7 @@ public function up(Context $context): bool $transaction->exec( sprintf( 'INSERT INTO %s (name, version, atime) VALUES (\'%s\', %d, \'%s\')', - $this->params->table, + $this->config->table, $context->filename, $context->version, gmdate('Y-m-d H:i:s'), @@ -99,7 +100,7 @@ public function down(Context $context): bool $transaction->exec( sprintf( 'DELETE FROM %s WHERE name=\'%s\'', - $this->params->table, + $this->config->table, $context->filename, ) ); diff --git a/src/internal/action/Workflow.php b/src/internal/action/Workflow.php index d647f4c..2651815 100644 --- a/src/internal/action/Workflow.php +++ b/src/internal/action/Workflow.php @@ -152,7 +152,7 @@ public function initialization(Migration $migration): void $command = $this->makeCommand($migration); try { - $files = (new filesystem\Setup($migration->getSetupPath(), $migration->table))->all(); + $files = (new filesystem\Setup($migration->getSetupPath(), $migration->config->table))->all(); } catch (ConfigurationException $exception) { $this->eventDispatcher->trigger( Event::FilesystemError, @@ -183,8 +183,8 @@ public function create(Migration $migration, string $name): void { try { (new filesystem\Action($migration->path))->create( - $migration->templFactory->makeName($name), - $migration->templFactory->makeBody(), + $migration->config->templFactory->makeName($name), + $migration->config->templFactory->makeBody(), ); } catch (ConfigurationException $exception) { $this->eventDispatcher->trigger( diff --git a/src/internal/command/Params.php b/src/internal/command/Params.php deleted file mode 100644 index a6816b6..0000000 --- a/src/internal/command/Params.php +++ /dev/null @@ -1,19 +0,0 @@ -makeConnection(), $params); + return new Command($this->makeConnection(), $config); } /** diff --git a/tests/stub/TestDriver.php b/tests/stub/TestDriver.php index a2ccc85..11408a4 100644 --- a/tests/stub/TestDriver.php +++ b/tests/stub/TestDriver.php @@ -7,7 +7,7 @@ use Override; use kuaukutsu\poc\migration\command\CommandInterface; use kuaukutsu\poc\migration\connection\DriverInterface; -use kuaukutsu\poc\migration\internal\command\Params; +use kuaukutsu\poc\migration\Config; final readonly class TestDriver implements DriverInterface { @@ -33,7 +33,7 @@ public function getSetupPath(): string } #[Override] - public function makeCommand(Params $params): CommandInterface + public function makeCommand(Config $config): CommandInterface { return new TestCommand($this->storage); } diff --git a/tests/workflow/ArgumentsTest.php b/tests/workflow/ArgumentsTest.php index c74097d..0590d4b 100644 --- a/tests/workflow/ArgumentsTest.php +++ b/tests/workflow/ArgumentsTest.php @@ -8,11 +8,11 @@ use PHPUnit\Framework\TestCase; use kuaukutsu\poc\migration\command\CommandInterface; use kuaukutsu\poc\migration\exception\ConfigurationException; -use kuaukutsu\poc\migration\internal\command\Params; use kuaukutsu\poc\migration\internal\connection\PDO\Driver; use kuaukutsu\poc\migration\tests\MigratorFactory; -use kuaukutsu\poc\migration\MigratorInterface; +use kuaukutsu\poc\migration\Config; use kuaukutsu\poc\migration\InputOptions; +use kuaukutsu\poc\migration\MigratorInterface; /** * Верхнеуровневая работа приложения. @@ -31,7 +31,7 @@ protected function setUp(): void ); $this->migrator = MigratorFactory::makeFromDriver($driver); - $this->command = $driver->makeCommand(new Params(table: 'migration')); + $this->command = $driver->makeCommand(new Config(table: 'migration')); } public function testUpWithLimit(): void diff --git a/tests/workflow/CommandTest.php b/tests/workflow/CommandTest.php index 0c1a77a..8d47605 100644 --- a/tests/workflow/CommandTest.php +++ b/tests/workflow/CommandTest.php @@ -12,10 +12,10 @@ use PHPUnit\Framework\TestCase; use kuaukutsu\poc\migration\command\CommandInterface; use kuaukutsu\poc\migration\command\Options; -use kuaukutsu\poc\migration\internal\command\Command; -use kuaukutsu\poc\migration\internal\command\Params; +use kuaukutsu\poc\migration\internal\action\Command; use kuaukutsu\poc\migration\internal\connection\PDO\Connection; use kuaukutsu\poc\migration\internal\connection\PDO\Type; +use kuaukutsu\poc\migration\Config; use kuaukutsu\poc\migration\Context; final class CommandTest extends TestCase @@ -27,7 +27,7 @@ protected function setUp(): void { $this->command = new Command( new Connection(new PDO(dsn: 'sqlite::memory:'), Type::PDO_SQLITE), - new Params(table: 'migration'), + new Config(table: 'migration'), ); } diff --git a/tests/workflow/ConfigurationTest.php b/tests/workflow/ConfigurationTest.php index faf71a3..eb6d0bd 100644 --- a/tests/workflow/ConfigurationTest.php +++ b/tests/workflow/ConfigurationTest.php @@ -6,12 +6,12 @@ use PHPUnit\Framework\TestCase; use kuaukutsu\poc\migration\exception\ActionException; -use kuaukutsu\poc\migration\exception\ConnectionException; use kuaukutsu\poc\migration\exception\ConfigurationException; +use kuaukutsu\poc\migration\exception\ConnectionException; use kuaukutsu\poc\migration\exception\InitializationException; -use kuaukutsu\poc\migration\internal\command\Params; use kuaukutsu\poc\migration\internal\connection\PDO\Driver; use kuaukutsu\poc\migration\tests\MigratorFactory; +use kuaukutsu\poc\migration\Config; use kuaukutsu\poc\migration\InputOptions; final class ConfigurationTest extends TestCase @@ -67,7 +67,7 @@ public function testActionNotExceptionDryRun(): void { $driver = new Driver(dsn: 'sqlite::memory:'); $migrator = MigratorFactory::makeFromEvent($driver); - $command = $driver->makeCommand(new Params(table: 'migration')); + $command = $driver->makeCommand(new Config(table: 'migration')); $migrator->init(); diff --git a/tests/workflow/CreateTest.php b/tests/workflow/CreateTest.php index e3b3995..4b084f0 100644 --- a/tests/workflow/CreateTest.php +++ b/tests/workflow/CreateTest.php @@ -6,10 +6,10 @@ use Override; use PHPUnit\Framework\TestCase; -use kuaukutsu\poc\migration\internal\command\Params; -use kuaukutsu\poc\migration\internal\connection\PDO\Driver; use kuaukutsu\poc\migration\exception\ConfigurationException; +use kuaukutsu\poc\migration\internal\connection\PDO\Driver; use kuaukutsu\poc\migration\tests\MigratorFactory; +use kuaukutsu\poc\migration\Config; use kuaukutsu\poc\migration\InputOptions; final class CreateTest extends TestCase @@ -22,7 +22,7 @@ public function testCreate(): void $path = dirname(__DIR__) . '/migration/sqlite/memory'; $migrator = MigratorFactory::makeFromEvent(driver: $driver, path: $path); - $command = $driver->makeCommand(new Params(table: 'migration')); + $command = $driver->makeCommand(new Config(table: 'migration')); $migrator->init(); diff --git a/tests/workflow/MigrationTest.php b/tests/workflow/MigrationTest.php index d5c8dc7..1b7780a 100644 --- a/tests/workflow/MigrationTest.php +++ b/tests/workflow/MigrationTest.php @@ -8,11 +8,11 @@ use PHPUnit\Framework\Attributes\Depends; use PHPUnit\Framework\TestCase; use kuaukutsu\poc\migration\command\CommandInterface; -use kuaukutsu\poc\migration\internal\command\Params; use kuaukutsu\poc\migration\internal\connection\PDO\Driver; use kuaukutsu\poc\migration\tests\MigratorFactory; -use kuaukutsu\poc\migration\MigratorInterface; +use kuaukutsu\poc\migration\Config; use kuaukutsu\poc\migration\InputOptions; +use kuaukutsu\poc\migration\MigratorInterface; /** * Верхнеуровневая работа приложения. @@ -31,7 +31,7 @@ protected function setUp(): void ); $this->migrator = MigratorFactory::makeFromDriver($driver); - $this->command = $driver->makeCommand(new Params(table: 'migration')); + $this->command = $driver->makeCommand(new Config(table: 'migration')); } public function testInit(): void diff --git a/tests/workflow/UpExactlyTest.php b/tests/workflow/UpExactlyTest.php index c84d1bb..09ea4d3 100644 --- a/tests/workflow/UpExactlyTest.php +++ b/tests/workflow/UpExactlyTest.php @@ -8,11 +8,11 @@ use PHPUnit\Framework\TestCase; use kuaukutsu\poc\migration\command\CommandInterface; use kuaukutsu\poc\migration\exception\ActionException; -use kuaukutsu\poc\migration\internal\command\Params; use kuaukutsu\poc\migration\internal\connection\PDO\Driver; use kuaukutsu\poc\migration\tests\MigratorFactory; -use kuaukutsu\poc\migration\MigratorInterface; +use kuaukutsu\poc\migration\Config; use kuaukutsu\poc\migration\InputOptions; +use kuaukutsu\poc\migration\MigratorInterface; final class UpExactlyTest extends TestCase { @@ -28,7 +28,7 @@ protected function setUp(): void ); $this->migrator = MigratorFactory::makeFromEvent($driver); - $this->command = $driver->makeCommand(new Params(table: 'migration')); + $this->command = $driver->makeCommand(new Config(table: 'migration')); } public function testUpExactlyAll(): void