Skip to content
Open
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
8 changes: 6 additions & 2 deletions docs/guide/en/usage-standalone.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ use Yiisoft\Injector\Injector;
/** @var ConnectionInterface $database */
$migrator = new Migrator($database, new NullMigrationInformer());
$migrationService = new MigrationService($database, new Injector(), $migrator);
$migrationService->setSourcePaths([dirname(__DIR__, 2), 'migrations']);
$migrationService->setNewMigrationPath(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'migrations');
```

> [!NOTE]
> If `sourceNamespaces` and `sourcePaths` are not specified, `newMigrationNamespace` or `newMigrationPath` will be used
> to find migrations.

Then initialize the command for using without CLI. For example, for applying migrations it will be `UpdateCommand`:

```php
Expand All @@ -55,7 +59,7 @@ use Yiisoft\Db\Migration\Command\UpdateCommand;
use Yiisoft\Db\Migration\Runner\UpdateRunner;

$command = new UpdateCommand(new UpdateRunner($migrator), $migrationService, $migrator);
$command->setHelperSet(new HelperSet(['queestion' => new QuestionHelper()]));
$command->setHelperSet(new HelperSet(['question' => new QuestionHelper()]));
```

And, finally, run the command:
Expand Down
4 changes: 4 additions & 0 deletions docs/guide/en/usage-with-yii-console.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Add to `config/console/params.php`:
...
```

> [!NOTE]
> If `sourceNamespaces` and `sourcePaths` are not specified, `newMigrationNamespace` or `newMigrationPath` will be used
> to find migrations.

Execute `composer du` in console to rebuild the configuration.

Now we have the `yiisoft/db-migration` package configured and it can be called in the console.
Expand Down
8 changes: 6 additions & 2 deletions docs/guide/pt-BR/usage-standalone.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ use Yiisoft\Injector\Injector;
/** @var ConnectionInterface $database */
$migrator = new Migrator($database, new NullMigrationInformer());
$migrationService = new MigrationService($database, new Injector(), $migrator);
$migrationService->setSourcePaths([dirname(__DIR__, 2), 'migrations']);
$migrationService->setNewMigrationPath(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'migrations');
```

> [!NOTE]
> Se `sourceNamespaces` e `sourcePaths` não forem especificados, `newMigrationNamespace` ou `newMigrationPath` serão
> usados para encontrar as migrações.

Em seguida, inicialize o comando para usar sem CLI. Por exemplo, para aplicar migrações será `UpdateCommand`:

```php
Expand All @@ -55,7 +59,7 @@ use Yiisoft\Db\Migration\Command\UpdateCommand;
use Yiisoft\Db\Migration\Runner\UpdateRunner;

$command = new UpdateCommand(new UpdateRunner($migrator), $migrationService, $migrator);
$command->setHelperSet(new HelperSet(['queestion' => new QuestionHelper()]));
$command->setHelperSet(new HelperSet(['question' => new QuestionHelper()]));
```

E, por fim, execute o comando:
Expand Down
4 changes: 4 additions & 0 deletions docs/guide/pt-BR/usage-with-yii-console.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Adicione em `config/console/params.php`:
...
```

> [!NOTE]
> Se `sourceNamespaces` e `sourcePaths` não forem especificados, `newMigrationNamespace` ou `newMigrationPath` serão
> usados para encontrar as migrações.

Execute `composer du` no console para reconstruir a configuração.

Agora temos o pacote [`yiisoft/db-migration`](https://github.com/yiisoft/db-migration) configurado e ele pode ser chamado no console.
Expand Down
52 changes: 40 additions & 12 deletions src/Service/MigrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,13 @@ public function before(string $commandName): int
}
break;
case 'migrate:up':
if (empty($this->sourceNamespaces) && empty($this->sourcePaths)) {
if (empty($this->sourceNamespaces)
&& empty($this->sourcePaths)
&& empty($this->newMigrationNamespace)
&& empty($this->newMigrationPath)
) {
$this->io?->error(
'At least one of `sourceNamespaces` or `sourcePaths` should be specified.',
'At least one of `sourceNamespaces`, `sourcePaths`, `newMigrationNamespace` or `newMigrationPath` should be specified.',
);

return Command::INVALID;
Expand All @@ -124,15 +128,7 @@ public function getNewMigrations(): array
$applied[trim($class, '\\')] = true;
}

$migrationPaths = [];

foreach ($this->sourcePaths as $path) {
$migrationPaths[] = [$path, ''];
}

foreach ($this->sourceNamespaces as $namespace) {
$migrationPaths[] = [$this->getNamespacePath($namespace), $namespace];
}
$migrationPaths = $this->findSourcePaths();

$migrations = [];
foreach ($migrationPaths as $item) {
Expand Down Expand Up @@ -394,7 +390,7 @@ private function makeMigrationInstance(string $class): object

if (!str_contains($class, '\\')) {
$isIncluded = false;
foreach ($this->sourcePaths as $path) {
foreach ($this->findSourcePaths() as [$path]) {
$file = $path . DIRECTORY_SEPARATOR . $class . '.php';

if (is_file($file)) {
Expand All @@ -416,6 +412,38 @@ private function makeMigrationInstance(string $class): object
return $this->injector->make($class);
}

/**
* Returns the migration paths with namespaces if they are specified.
*
* @return array<array{0: string, 1: string}>
*/
private function findSourcePaths(): array
{
$paths = [];

foreach ($this->sourcePaths as $path) {
$paths[] = [$path, ''];
}

foreach ($this->sourceNamespaces as $namespace) {
$paths[] = [$this->getNamespacePath($namespace), $namespace];
}

if ($paths !== []) {
return $paths;
}

if ($this->newMigrationPath !== '') {
return [[$this->newMigrationPath, '']];
}

if ($this->newMigrationNamespace !== '') {
return [[$this->getNamespacePath($this->newMigrationNamespace), $this->newMigrationNamespace]];
}

return [];
}

/**
* Returns the file path matching the give namespace.
*
Expand Down
50 changes: 44 additions & 6 deletions tests/Common/Command/AbstractUpdateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,18 +274,56 @@ public function testWithoutSourcePath(): void
$exitCode = $command->execute([]);
$output = preg_replace('/(\R|\s)+/', ' ', $command->getDisplay(true));

$this->assertSame(Command::SUCCESS, $exitCode);
$this->assertStringContainsString('No new migrations found.', $output);
$this->assertStringContainsString('[OK] Your system is up-to-date.', $output);
}

public function testWithoutSourceNamespaces(): void
{
MigrationHelper::useMigrationsNamespace($this->container);

$this->container->get(MigrationService::class)->setSourceNamespaces([]);

$command = $this->createCommand($this->container);
$command->setInputs(['yes']);

$exitCode = $command->execute([]);
$output = preg_replace('/(\R|\s)+/', ' ', $command->getDisplay(true));

$this->assertSame(Command::SUCCESS, $exitCode);
$this->assertStringContainsString('No new migrations found.', $output);
$this->assertStringContainsString('[OK] Your system is up-to-date.', $output);
}

public function testWithoutMigrationPaths(): void
{
MigrationHelper::useMigrationsPath($this->container);

$migration = $this->container->get(MigrationService::class);
$migration->setSourcePaths([]);
$migration->setNewMigrationPath('');

$command = $this->createCommand($this->container);
$command->setInputs(['yes']);

$exitCode = $command->execute([]);
$output = preg_replace('/(\R|\s)+/', ' ', $command->getDisplay(true));

$this->assertSame(Command::INVALID, $exitCode);
$this->assertStringContainsStringCollapsingSpaces(
'At least one of `sourceNamespaces` or `sourcePaths` should be specified.',
$this->assertStringContainsString(
'At least one of `sourceNamespaces`, `sourcePaths`, `newMigrationNamespace` or `newMigrationPath` should be specified.',
$output,
);
}

public function testWithoutSourceNamespaces(): void
public function testWithoutMigrationNamespaces(): void
{
MigrationHelper::useMigrationsNamespace($this->container);

$this->container->get(MigrationService::class)->setSourceNamespaces([]);
$migration = $this->container->get(MigrationService::class);
$migration->setSourceNamespaces([]);
$migration->setNewMigrationNamespace('');

$command = $this->createCommand($this->container);
$command->setInputs(['yes']);
Expand All @@ -294,8 +332,8 @@ public function testWithoutSourceNamespaces(): void
$output = preg_replace('/(\R|\s)+/', ' ', $command->getDisplay(true));

$this->assertSame(Command::INVALID, $exitCode);
$this->assertStringContainsStringCollapsingSpaces(
'At least one of `sourceNamespaces` or `sourcePaths` should be specified.',
$this->assertStringContainsString(
'At least one of `sourceNamespaces`, `sourcePaths`, `newMigrationNamespace` or `newMigrationPath` should be specified.',
$output,
);
}
Expand Down
Loading