Improve command for creating new migrations
Use required parameter instead of optional option [MAILPOET-5416]
This commit is contained in:
committed by
Aschepikov
parent
b4d0b35a1d
commit
ec8b5260dd
@@ -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.
|
* Creates a new migration file. Use `migrations:new db` for a Db level migration or `migrations:new app` for App level migration.
|
||||||
* @param $opts array{level: string}
|
* @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();
|
$generator = new \MailPoet\Migrator\Repository();
|
||||||
$result = $generator->create($opts['level']);
|
$level = ucfirst(strtolower($level));
|
||||||
|
$result = $generator->create($level);
|
||||||
$path = realpath($result['path']);
|
$path = realpath($result['path']);
|
||||||
$this->output->writeln('MAILPOET DATABASE MIGRATIONS');
|
$this->output->writeln('MAILPOET DATABASE MIGRATIONS');
|
||||||
$this->output->writeln("============================\n");
|
$this->output->writeln("============================\n");
|
||||||
|
@@ -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 {
|
public static function migrationFileWriteFailed(string $path): self {
|
||||||
return self::create()->withMessage(
|
return self::create()->withMessage(
|
||||||
sprintf('Could not write migration file "%s".', $path)
|
sprintf('Could not write migration file "%s".', $path)
|
||||||
|
@@ -27,6 +27,9 @@ class Repository {
|
|||||||
|
|
||||||
/** @return array{name: string, path: string} */
|
/** @return array{name: string, path: string} */
|
||||||
public function create(string $level): array {
|
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);
|
$templateFile = str_replace('{level}', $level, $this->templateFile);
|
||||||
$template = @file_get_contents($templateFile);
|
$template = @file_get_contents($templateFile);
|
||||||
if (!$template) {
|
if (!$template) {
|
||||||
|
@@ -47,6 +47,18 @@ class RepositoryTest extends MailPoetUnitTest {
|
|||||||
$repository->create(Repository::MIGRATIONS_LEVEL_DB);
|
$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 {
|
public function testItLoadsMigrationFiles(): void {
|
||||||
$repository = $this->make(Repository::class, [
|
$repository = $this->make(Repository::class, [
|
||||||
'migrationsDir' => __DIR__ . '/TestMigrations',
|
'migrationsDir' => __DIR__ . '/TestMigrations',
|
||||||
|
Reference in New Issue
Block a user