Skip to content

Commit

Permalink
Apply merge after migrations are made (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
cable8mm authored Mar 19, 2024
1 parent 300fedb commit 47904bd
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/Command/GenerateMigrationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Cable8mm\Xeed\DB;
use Cable8mm\Xeed\Generators\MigrationGenerator;
use Cable8mm\Xeed\Mergers\MergerContainer;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -39,7 +40,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$tables = DB::getInstance()->attach()->getTables();

foreach ($tables as $table) {
MigrationGenerator::make($table)->run();
// MigrationGenerator::make($table)->run();
MigrationGenerator::make($table)->merging(
MergerContainer::getEngines()
)->run();
}

$output->writeln('Migrations have been generated.');
Expand Down
4 changes: 3 additions & 1 deletion src/Generators/DatabaseSeederGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ final class DatabaseSeederGenerator
*/
private string $stub;

public const intent = ' ';

private function __construct(
private array $tables,
private ?string $namespace = null,
Expand All @@ -38,7 +40,7 @@ public function run(): void
$seeder_classes = '';

foreach ($this->tables as $table) {
$seeder_classes .= ' '.$table->model().'Seeder::class,'.PHP_EOL;
$seeder_classes .= DatabaseSeederGenerator::intent.$table->model().'Seeder::class,'.PHP_EOL;
}

$seeder_classes = preg_replace('/\n$/', '', $seeder_classes);
Expand Down
4 changes: 3 additions & 1 deletion src/Generators/FactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ final class FactoryGenerator implements GeneratorInterface
*/
private string $stub;

public const intent = ' ';

private function __construct(
private Table $table,
private ?string $namespace = null,
Expand All @@ -36,7 +38,7 @@ public function run(): void
$fakers = '';

foreach ($this->table->getColumns() as $column) {
$fakers .= ' '.$column->fake().PHP_EOL;
$fakers .= FactoryGenerator::intent.$column->fake().PHP_EOL;
}

$fakers = preg_replace('/\n$/', '', $fakers);
Expand Down
33 changes: 31 additions & 2 deletions src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cable8mm\Xeed\Generators;

use Cable8mm\Xeed\Interfaces\GeneratorInterface;
use Cable8mm\Xeed\Mergers\MergerContainer;
use Cable8mm\Xeed\Support\Path;
use Cable8mm\Xeed\Table;

Expand All @@ -16,6 +17,15 @@ final class MigrationGenerator implements GeneratorInterface
*/
private string $stub;

public const intent = ' ';

/**
* Engines for MergerContainer.
*
* @var ?array<Merger>
*/
private ?array $mergerEngines = null;

private function __construct(
private Table $table,
private ?string $namespace = null,
Expand All @@ -36,7 +46,7 @@ public function run(): void
$fields = '';

foreach ($this->table->getColumns() as $column) {
$fields .= ' '.$column->migration().PHP_EOL;
$fields .= MigrationGenerator::intent.$column->migration().PHP_EOL;
}

$fields = preg_replace('/\n$/', '', $fields);
Expand All @@ -47,13 +57,32 @@ public function run(): void
$this->stub
);

// 2019_12_14_000001_create_personal_access_tokens_table.php
if (! is_null($this->mergerEngines)) {
$seederClass = MergerContainer::from(body : $seederClass)
->engines($this->mergerEngines)
->operating()
->verbose();
}

file_put_contents(
$this->dist.$this->table->migration(),
$seederClass
);
}

/**
* To set merger engines.
*
* @param array<Merger> $engines An array of merger engines.
* @return static The method returns the current instance that enables methods chaining.
*/
public function merging(array $engines): static
{
$this->mergerEngines = $engines;

return $this;
}

/**
* Factory method.
*
Expand Down
21 changes: 20 additions & 1 deletion src/Mergers/MergerContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cable8mm\Xeed\Mergers;

use Cable8mm\Xeed\Generators\MigrationGenerator;
use Cable8mm\Xeed\Interfaces\MergerInterface;
use InvalidArgumentException;
use LogicException;
Expand Down Expand Up @@ -91,7 +92,7 @@ public function operating(): static
$replace = $engine->start($this->lines[$key], $this->lines[$key + 1]);

if ($replace !== null) {
$this->lines[$key + 1] = $replace;
$this->lines[$key + 1] = MigrationGenerator::intent.$replace;
$this->lines[$key] = null;

break;
Expand Down Expand Up @@ -164,4 +165,22 @@ public static function from(?string $migration = null, ?string $body = null): st

throw new InvalidArgumentException('One of the arguments must be null');
}

/**
* To get all the engines.
*
* @return array The method returns an array of engines.
*/
public static function getEngines(): array
{
return [
new MorphsMerger(),
new NullableMorphsMerger(),
new NullableUlidMorphsMerger(),
new NullableUuidMorphsMerger(),
new TimestampsMerger(),
new UlidMorphsMerger(),
new UuidMorphsMerger(),
];
}
}
3 changes: 2 additions & 1 deletion tests/Unit/Mergers/MergerContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cable8mm\Xeed\Tests\Unit\Mergers;

use Cable8mm\Xeed\Generators\MigrationGenerator;
use Cable8mm\Xeed\Mergers\MergerContainer;
use Cable8mm\Xeed\Mergers\MorphsMerger;
use Cable8mm\Xeed\Mergers\NullableMorphsMerger;
Expand Down Expand Up @@ -116,7 +117,7 @@ public function test_it_can_be_operating_correctly(): void
$migrated = $container->engines($this->myEngines)->operating()->toArray();

$this->assertEquals(
'$table->morphs(\'morphs\');',
MigrationGenerator::intent.'$table->morphs(\'morphs\');',
reset($migrated)
);
}
Expand Down

0 comments on commit 47904bd

Please sign in to comment.