Skip to content

Commit

Permalink
Merge pull request #124 from ucan-lab/topic-123
Browse files Browse the repository at this point in the history
[4.0]Migration file prefix date specification option
  • Loading branch information
ucan-lab authored Jan 23, 2021
2 parents 5f87e31 + e369ff9 commit 2e27fc1
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 10 deletions.
13 changes: 12 additions & 1 deletion src/Console/DacapoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace UcanLab\LaravelDacapo\Console;

use DateTime;
use Exception;
use Illuminate\Console\ConfirmableTrait;
use UcanLab\LaravelDacapo\Dacapo\UseCase\Console\DacapoCommandUseCase;

Expand All @@ -22,6 +24,7 @@ class DacapoCommand extends Command
{--no-migrate : Do not migrate}
{--seed : Seed the database with records}
{--refresh : Migrate refresh (for debug)}
{--prefix=1970-01-01 : Prefix date of the output migration file}
';

/**
Expand Down Expand Up @@ -51,7 +54,15 @@ public function handle(): void
{
$this->call('dacapo:clear', ['--force' => true]);

$fileList = $this->useCase->handle();
try {
$prefixDate = new DateTime($this->option('prefix'));
} catch (Exception $exception) {
$this->error('Error: Set the --prefix option to a value that can be converted to a date type.');

return;
}

$fileList = $this->useCase->handle($prefixDate);

foreach ($fileList as $file) {
$this->line(sprintf('<fg=green>Generated:</> %s', $file->getName()));
Expand Down
6 changes: 4 additions & 2 deletions src/Dacapo/UseCase/Console/DacapoCommandUseCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace UcanLab\LaravelDacapo\Dacapo\UseCase\Console;

use DateTime;
use UcanLab\LaravelDacapo\Dacapo\Domain\ValueObject\Migration\MigrationFileList;
use UcanLab\LaravelDacapo\Dacapo\UseCase\Generator\MigrationGenerator;
use UcanLab\LaravelDacapo\Dacapo\UseCase\Port\SchemaListRepository;
Expand All @@ -23,10 +24,11 @@ public function __construct(SchemaListRepository $repository, MigrationGenerator
}

/**
* @param DateTime $prefixDate
* @return MigrationFileList
*/
public function handle(): MigrationFileList
public function handle(DateTime $prefixDate): MigrationFileList
{
return $this->generator->generate($this->repository->get());
return $this->generator->generate($prefixDate, $this->repository->get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace UcanLab\LaravelDacapo\Dacapo\UseCase\Converter;

use DateTime;
use Illuminate\Support\Str;
use UcanLab\LaravelDacapo\Dacapo\Domain\Entity\Schema;
use UcanLab\LaravelDacapo\Dacapo\Domain\Entity\SchemaList;
Expand All @@ -11,7 +12,16 @@
class SchemaToConstraintForeignKeyMigrationConverter
{
const MIGRATION_COLUMN_INDENT = ' ';
protected Schema $schema;
protected DateTime $prefixDate;

/**
* SchemaToConstraintForeignKeyMigrationConverter constructor.
* @param DateTime $prefixDate
*/
public function __construct(DateTime $prefixDate)
{
$this->prefixDate = $prefixDate;
}

/**
* @param SchemaList $schemaList
Expand All @@ -30,6 +40,17 @@ public function convertList(SchemaList $schemaList): MigrationFileList
return $fileList;
}

/**
* @param DateTime $prefixDate
* @return $this
*/
public function setPrefixDate(DateTime $prefixDate): self
{
$this->prefixDate = $prefixDate;

return $this;
}

/**
* @param Schema $schema
* @return MigrationFile
Expand All @@ -45,7 +66,7 @@ public function convert(Schema $schema): MigrationFile
*/
protected function makeMigrationFileName(Schema $schema): string
{
return sprintf('1970_01_01_000003_%s.php', $this->makeMigrationName($schema));
return sprintf('%s_000003_%s.php', $this->prefixDate->format('Y_m_d'), $this->makeMigrationName($schema));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace UcanLab\LaravelDacapo\Dacapo\UseCase\Converter;

use DateTime;
use Illuminate\Support\Str;
use UcanLab\LaravelDacapo\Dacapo\Domain\Entity\Schema;
use UcanLab\LaravelDacapo\Dacapo\Domain\Entity\SchemaList;
Expand All @@ -11,7 +12,27 @@
class SchemaToCreateIndexMigrationConverter
{
const MIGRATION_COLUMN_INDENT = ' ';
protected Schema $schema;
protected DateTime $prefixDate;

/**
* SchemaToCreateIndexMigrationConverter constructor.
* @param DateTime $prefixDate
*/
public function __construct(DateTime $prefixDate)
{
$this->prefixDate = $prefixDate;
}

/**
* @param DateTime $prefixDate
* @return $this
*/
public function setPrefixDate(DateTime $prefixDate): self
{
$this->prefixDate = $prefixDate;

return $this;
}

/**
* @param SchemaList $schemaList
Expand Down Expand Up @@ -45,7 +66,7 @@ public function convert(Schema $schema): MigrationFile
*/
protected function makeMigrationFileName(Schema $schema): string
{
return sprintf('1970_01_01_000002_%s.php', $this->makeMigrationName($schema));
return sprintf('%s_000002_%s.php', $this->prefixDate->format('Y_m_d'), $this->makeMigrationName($schema));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace UcanLab\LaravelDacapo\Dacapo\UseCase\Converter;

use DateTime;
use Illuminate\Support\Str;
use UcanLab\LaravelDacapo\Dacapo\Domain\Entity\Schema;
use UcanLab\LaravelDacapo\Dacapo\Domain\Entity\SchemaList;
Expand All @@ -14,10 +15,28 @@ class SchemaToCreateTableMigrationConverter
const MIGRATION_COLUMN_INDENT = ' ';

protected DatabaseBuilder $databaseBuilder;
protected DateTime $prefixDate;

public function __construct(DatabaseBuilder $databaseBuilder)
/**
* SchemaToCreateTableMigrationConverter constructor.
* @param DatabaseBuilder $databaseBuilder
* @param DateTime $prefixDate
*/
public function __construct(DatabaseBuilder $databaseBuilder, DateTime $prefixDate)
{
$this->databaseBuilder = $databaseBuilder;
$this->prefixDate = $prefixDate;
}

/**
* @param DateTime $prefixDate
* @return $this
*/
public function setPrefixDate(DateTime $prefixDate): self
{
$this->prefixDate = $prefixDate;

return $this;
}

/**
Expand Down Expand Up @@ -52,7 +71,7 @@ public function convert(Schema $schema): MigrationFile
*/
protected function makeMigrationFileName(Schema $schema): string
{
return sprintf('1970_01_01_000001_%s.php', $this->makeMigrationName($schema));
return sprintf('%s_000001_%s.php', $this->prefixDate->format('Y_m_d'), $this->makeMigrationName($schema));
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/Dacapo/UseCase/Generator/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace UcanLab\LaravelDacapo\Dacapo\UseCase\Generator;

use DateTime;
use UcanLab\LaravelDacapo\Dacapo\Domain\Entity\SchemaList;
use UcanLab\LaravelDacapo\Dacapo\Domain\ValueObject\Migration\MigrationFileList;
use UcanLab\LaravelDacapo\Dacapo\UseCase\Converter\SchemaToConstraintForeignKeyMigrationConverter;
Expand Down Expand Up @@ -36,11 +37,16 @@ public function __construct(
}

/**
* @param DateTime $prefixDate
* @param SchemaList $schemaList
* @return MigrationFileList
*/
public function generate(SchemaList $schemaList): MigrationFileList
public function generate(DateTime $prefixDate, SchemaList $schemaList): MigrationFileList
{
$this->schemaToCreateTableMigrationConverter->setPrefixDate($prefixDate);
$this->schemaToCreateIndexMigrationConverter->setPrefixDate($prefixDate);
$this->schemaToConstraintForeignKeyMigrationConverter->setPrefixDate($prefixDate);

$tableFileList = $this->generateCreateTable($schemaList);
$indexFileList = $this->generateCreateIndex($schemaList);
$foreignKeyFileList = $this->generateConstraintForeignKey($schemaList);
Expand Down
2 changes: 2 additions & 0 deletions tests/Console/DacapoClearCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace UcanLab\LaravelDacapo\Test\App\UseCase\Console;

use DateTime;
use UcanLab\LaravelDacapo\Dacapo\Domain\ValueObject\Migration\MigrationFile;
use UcanLab\LaravelDacapo\Dacapo\Domain\ValueObject\Migration\MigrationFileList;
use UcanLab\LaravelDacapo\Dacapo\Infra\Adapter\InMemoryMigrationListRepository;
Expand All @@ -14,6 +15,7 @@ class DacapoClearCommandTest extends TestCase
public function testResolve(): void
{
$this->app->register(ConsoleServiceProvider::class);
$this->instance(DateTime::class, new DateTime());
$this->instance(MigrationListRepository::class, new InMemoryMigrationListRepository(new MigrationFileList([
new MigrationFile('1970_01_01_000000_create_users_table.php', ''),
new MigrationFile('1970_01_01_000000_create_password_resets_table.php', ''),
Expand Down
3 changes: 3 additions & 0 deletions tests/Console/DacapoCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace UcanLab\LaravelDacapo\Test\App\UseCase\Console;

use DateTime;
use Illuminate\Support\Facades\File;
use UcanLab\LaravelDacapo\Dacapo\Domain\ValueObject\Migration\MigrationFile;
use UcanLab\LaravelDacapo\Dacapo\Domain\ValueObject\Migration\MigrationFileList;
Expand Down Expand Up @@ -29,6 +30,7 @@ public function testMysql(MigrationFileList $expectedMigrationFileList, SchemaFi
$this->app->register(ConsoleServiceProvider::class);

$actualMigrationFileList = new MigrationFileList();
$this->instance(DateTime::class, new DateTime());
$this->instance(DatabaseBuilder::class, new MysqlDatabaseBuilder());
$this->instance(SchemaListRepository::class, new InMemorySchemaListRepository($schemaFileList));
$this->instance(MigrationListRepository::class, new InMemoryMigrationListRepository($actualMigrationFileList));
Expand Down Expand Up @@ -81,6 +83,7 @@ public function testPostgresql(MigrationFileList $expectedMigrationFileList, Sch
$this->app->register(ConsoleServiceProvider::class);

$actualMigrationFileList = new MigrationFileList();
$this->instance(DateTime::class, new DateTime());
$this->instance(DatabaseBuilder::class, new PostgresqlDatabaseBuilder());
$this->instance(SchemaListRepository::class, new InMemorySchemaListRepository($schemaFileList));
$this->instance(MigrationListRepository::class, new InMemoryMigrationListRepository($actualMigrationFileList));
Expand Down
2 changes: 2 additions & 0 deletions tests/Console/DacapoInitCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace UcanLab\LaravelDacapo\Test\App\UseCase\Console;

use DateTime;
use UcanLab\LaravelDacapo\Dacapo\Domain\ValueObject\Schema\SchemaFileList;
use UcanLab\LaravelDacapo\Dacapo\Infra\Adapter\InMemorySchemaListRepository;
use UcanLab\LaravelDacapo\Dacapo\UseCase\Port\SchemaListRepository;
Expand All @@ -13,6 +14,7 @@ class DacapoInitCommandTest extends TestCase
public function testResolve(): void
{
$this->app->register(ConsoleServiceProvider::class);
$this->instance(DateTime::class, new DateTime());
$this->instance(SchemaListRepository::class, new InMemorySchemaListRepository(new SchemaFileList()));
$this->artisan('dacapo:init')->assertExitCode(0);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Console/DacapoUninstallCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace UcanLab\LaravelDacapo\Test\App\UseCase\Console;

use DateTime;
use UcanLab\LaravelDacapo\Dacapo\Domain\ValueObject\Schema\SchemaFileList;
use UcanLab\LaravelDacapo\Dacapo\Infra\Adapter\InMemorySchemaListRepository;
use UcanLab\LaravelDacapo\Dacapo\UseCase\Port\SchemaListRepository;
Expand All @@ -13,6 +14,7 @@ class DacapoUninstallCommandTest extends TestCase
public function testResolve(): void
{
$this->app->register(ConsoleServiceProvider::class);
$this->instance(DateTime::class, new DateTime());
$this->instance(SchemaListRepository::class, new InMemorySchemaListRepository(new SchemaFileList()));
$this->artisan('dacapo:uninstall')->assertExitCode(0);
}
Expand Down

0 comments on commit 2e27fc1

Please sign in to comment.