diff --git a/docs/guide/en/usage-standalone.md b/docs/guide/en/usage-standalone.md index 4db372a0..8e683ee7 100644 --- a/docs/guide/en/usage-standalone.md +++ b/docs/guide/en/usage-standalone.md @@ -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 @@ -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: diff --git a/docs/guide/en/usage-with-yii-console.md b/docs/guide/en/usage-with-yii-console.md index a82e76d8..de282081 100644 --- a/docs/guide/en/usage-with-yii-console.md +++ b/docs/guide/en/usage-with-yii-console.md @@ -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. diff --git a/docs/guide/pt-BR/usage-standalone.md b/docs/guide/pt-BR/usage-standalone.md index 78fb7c1b..12218499 100644 --- a/docs/guide/pt-BR/usage-standalone.md +++ b/docs/guide/pt-BR/usage-standalone.md @@ -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 @@ -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: diff --git a/docs/guide/pt-BR/usage-with-yii-console.md b/docs/guide/pt-BR/usage-with-yii-console.md index 347797c0..90f3186a 100644 --- a/docs/guide/pt-BR/usage-with-yii-console.md +++ b/docs/guide/pt-BR/usage-with-yii-console.md @@ -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. diff --git a/src/Service/MigrationService.php b/src/Service/MigrationService.php index f6077f47..3e3d1983 100644 --- a/src/Service/MigrationService.php +++ b/src/Service/MigrationService.php @@ -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; @@ -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) { @@ -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)) { @@ -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 + */ + 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. * diff --git a/tests/Common/Command/AbstractUpdateCommandTest.php b/tests/Common/Command/AbstractUpdateCommandTest.php index 46226f72..444ae9c9 100644 --- a/tests/Common/Command/AbstractUpdateCommandTest.php +++ b/tests/Common/Command/AbstractUpdateCommandTest.php @@ -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']); @@ -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, ); }