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
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
],
)
);
Expand Down
6 changes: 3 additions & 3 deletions example/mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions example/presentation/CommandOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down Expand Up @@ -44,7 +44,7 @@ protected function getArguments(InputInterface $input): InputArgs
$options['migrationName'] = $this->getMigrationName($input);
}

return new InputArgs(...$options);
return new InputOptions(...$options);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion example/presentation/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion example/presentation/DownCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down
2 changes: 1 addition & 1 deletion example/presentation/FixtureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion example/presentation/RedoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down
2 changes: 1 addition & 1 deletion example/presentation/UpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down
2 changes: 1 addition & 1 deletion example/presentation/VerifyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down
20 changes: 20 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace kuaukutsu\poc\migration;

/**
* @api
*/
final readonly class Config
{
/**
* @param non-empty-string $table
*/
public function __construct(
public string $table = 'migration',
public template\FactoryInterface $templFactory = new template\Factory(),
) {
}
}
2 changes: 1 addition & 1 deletion src/InputArgs.php → src/InputOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @api
* @infection-ignore-all
*/
final readonly class InputArgs
final readonly class InputOptions
{
/**
* @param non-negative-int $limit
Expand Down
11 changes: 4 additions & 7 deletions src/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +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;

/**
* @api
Expand All @@ -21,15 +20,13 @@

/**
* @param non-empty-string $path
* @param non-empty-string $table
*/
public function __construct(
public string $path,
private DriverInterface $driver,
public string $table = 'migration',
public template\FactoryInterface $templFactory = new template\Factory(),
public Config $config = new Config(),
) {
$this->name = $this->driver->getName() . '/' . $this->driver->getDbName();
$this->name = $this->driver->getName() . '/' . $this->driver->getSourceName();
}

/**
Expand All @@ -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);
}
}
66 changes: 33 additions & 33 deletions src/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,58 @@ public function init(): void
}

#[Override]
public function up(InputArgs $args = new InputArgs()): void
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
{
foreach ($this->selectDb($args) as $migration) {
$this->actionWorkflow->up($migration, $args);
}
}

#[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);
}
}

#[Override]
public function redo(InputArgs $args = new InputArgs()): void
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
{
$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(
Expand All @@ -82,39 +110,11 @@ public function verify(InputArgs $args = new InputArgs()): void
}
}

#[Override]
public function fixture(InputArgs $args = new InputArgs()): void
{
foreach ($this->selectDb($args) as $migration) {
$this->actionWorkflow->fixture($migration, $args);
}
}

#[Override]
public function create(InputArgs $args = new InputArgs()): 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<Migration>
* @throws ConfigurationException
*/
private function selectDb(InputArgs $args): iterable
private function selectDb(InputOptions $args): iterable
{
if ($args->dbName === null) {
return $this->collection;
Expand Down
22 changes: 11 additions & 11 deletions src/MigratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,47 @@ 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
* @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
* @throws ConfigurationException If the driver is not implemented
* @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
* @throws ConfigurationException If the driver is not implemented
* @throws ConnectionException
* @throws InitializationException initialization step is required
*/
public function redo(InputArgs $args = new InputArgs()): void;
public function fixture(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 verify(InputArgs $args = new InputArgs()): 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(InputArgs $args = new InputArgs()): void;

/**
* @throws ConfigurationException
*/
public function create(InputArgs $args = new InputArgs()): void;
public function verify(InputOptions $args = new InputOptions()): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

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
{
/**
* @return array<non-empty-string, non-negative-int>
*/
public function fetchApplied(command\Args $args = new command\Args()): array;
public function fetchApplied(Options $options = new Options()): array;

/**
* @return bool true: request completed; false: request rejected
Expand Down
Loading