Improve command for creating new migrations

Use required parameter instead of optional option
[MAILPOET-5416]
This commit is contained in:
Rostislav Wolny
2023-07-24 15:08:37 +02:00
committed by Aschepikov
parent b4d0b35a1d
commit ec8b5260dd
4 changed files with 26 additions and 4 deletions

View File

@ -448,12 +448,13 @@ class RoboFile extends \Robo\Tasks {
}
/**
* Creates a new migration file. Creates Db level migration. Use --level=App to create an application level migration.
* @param $opts array{level: string}
* Creates a new migration file. Use `migrations:new db` for a Db level migration or `migrations:new app` for App level migration.
* @param $level string - db or app
*/
public function migrationsNew($opts = ['level' => \MailPoet\Migrator\Repository::MIGRATIONS_LEVEL_DB]) {
public function migrationsNew($level) {
$generator = new \MailPoet\Migrator\Repository();
$result = $generator->create($opts['level']);
$level = ucfirst(strtolower($level));
$result = $generator->create($level);
$path = realpath($result['path']);
$this->output->writeln('MAILPOET DATABASE MIGRATIONS');
$this->output->writeln("============================\n");

View File

@ -12,6 +12,12 @@ class MigratorException extends InvalidStateException {
);
}
public static function invalidMigrationLevel(string $level): self {
return self::create()->withMessage(
sprintf('Migration level "%s" is not supported! Use "App" of "Db".', $level)
);
}
public static function migrationFileWriteFailed(string $path): self {
return self::create()->withMessage(
sprintf('Could not write migration file "%s".', $path)

View File

@ -27,6 +27,9 @@ class Repository {
/** @return array{name: string, path: string} */
public function create(string $level): array {
if (!in_array($level, [self::MIGRATIONS_LEVEL_APP, self::MIGRATIONS_LEVEL_DB], true)) {
throw MigratorException::invalidMigrationLevel($level);
}
$templateFile = str_replace('{level}', $level, $this->templateFile);
$template = @file_get_contents($templateFile);
if (!$template) {

View File

@ -47,6 +47,18 @@ class RepositoryTest extends MailPoetUnitTest {
$repository->create(Repository::MIGRATIONS_LEVEL_DB);
}
public function testItFailsCreatingLevelIsInvalid(): void {
$migrationsDir = __DIR__ . '/TestMigrations';
$repository = $this->make(Repository::class, [
'migrationsDir' => $migrationsDir,
'templateFile' => self::TEMPLATE_FILE,
]);
$this->expectException(MigratorException::class);
$this->expectExceptionMessage(sprintf('Migration level "%s" is not supported! Use "App" of "Db".', 'abc'));
$repository->create('abc');
}
public function testItLoadsMigrationFiles(): void {
$repository = $this->make(Repository::class, [
'migrationsDir' => __DIR__ . '/TestMigrations',