-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
566 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Schema\Generator\Migrations\Exception; | ||
|
||
final class GeneratorException extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Schema\Generator\Migrations; | ||
|
||
use Cycle\Database\Schema\AbstractTable; | ||
use Cycle\Migrations\Atomizer\Atomizer; | ||
|
||
final class NameBasedOnChangesGenerator implements NameGeneratorInterface | ||
{ | ||
public function generate(Atomizer $atomizer): string | ||
{ | ||
$name = []; | ||
|
||
foreach ($atomizer->getTables() as $table) { | ||
if ($table->getStatus() === AbstractTable::STATUS_NEW) { | ||
$name[] = 'create_' . $table->getName(); | ||
continue; | ||
} | ||
|
||
if ($table->getStatus() === AbstractTable::STATUS_DECLARED_DROPPED) { | ||
$name[] = 'drop_' . $table->getName(); | ||
continue; | ||
} | ||
|
||
if ($table->getComparator()->isRenamed()) { | ||
$name[] = 'rename_' . $table->getInitialName(); | ||
continue; | ||
} | ||
|
||
$name[] = 'change_' . $table->getName(); | ||
|
||
$comparator = $table->getComparator(); | ||
|
||
foreach ($comparator->addedColumns() as $column) { | ||
$name[] = 'add_' . $column->getName(); | ||
} | ||
|
||
foreach ($comparator->droppedColumns() as $column) { | ||
$name[] = 'rm_' . $column->getName(); | ||
} | ||
|
||
foreach ($comparator->alteredColumns() as $column) { | ||
$name[] = 'alter_' . $column[0]->getName(); | ||
} | ||
|
||
foreach ($comparator->addedIndexes() as $index) { | ||
$name[] = 'add_index_' . $index->getName(); | ||
} | ||
|
||
foreach ($comparator->droppedIndexes() as $index) { | ||
$name[] = 'rm_index_' . $index->getName(); | ||
} | ||
|
||
foreach ($comparator->alteredIndexes() as $index) { | ||
$name[] = 'alter_index_' . $index[0]->getName(); | ||
} | ||
|
||
foreach ($comparator->addedForeignKeys() as $fk) { | ||
$name[] = 'add_fk_' . $fk->getName(); | ||
} | ||
|
||
foreach ($comparator->droppedForeignKeys() as $fk) { | ||
$name[] = 'rm_fk_' . $fk->getName(); | ||
} | ||
|
||
foreach ($comparator->alteredForeignKeys() as $fk) { | ||
$name[] = 'alter_fk_' . $fk[0]->getName(); | ||
} | ||
} | ||
|
||
return \implode('_', $name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Schema\Generator\Migrations; | ||
|
||
use Cycle\Migrations\Atomizer\Atomizer; | ||
|
||
interface NameGeneratorInterface | ||
{ | ||
public function generate(Atomizer $atomizer): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Schema\Generator\Migrations\Strategy; | ||
|
||
use Cycle\Database\Schema\AbstractTable; | ||
use Cycle\Schema\Generator\Migrations\MigrationImage; | ||
|
||
interface GeneratorStrategyInterface | ||
{ | ||
/** | ||
* @param non-empty-string $database | ||
* @param array<AbstractTable> $tables | ||
* | ||
* @return array<MigrationImage> | ||
*/ | ||
public function generate(string $database, array $tables): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Schema\Generator\Migrations\Strategy; | ||
|
||
use Cycle\Database\Schema\AbstractTable; | ||
use Cycle\Migrations\Atomizer\Atomizer; | ||
use Cycle\Migrations\Atomizer\Renderer; | ||
use Cycle\Migrations\Atomizer\TableSorter; | ||
use Cycle\Migrations\Config\MigrationConfig; | ||
use Cycle\Schema\Generator\Migrations\MigrationImage; | ||
use Cycle\Schema\Generator\Migrations\NameGeneratorInterface; | ||
|
||
final class MultipleFilesStrategy implements GeneratorStrategyInterface | ||
{ | ||
private static int $sec = 0; | ||
|
||
public function __construct( | ||
private readonly MigrationConfig $migrationConfig, | ||
private readonly NameGeneratorInterface $nameGenerator, | ||
private readonly TableSorter $tableSorter = new TableSorter() | ||
) { | ||
} | ||
|
||
/** | ||
* @param non-empty-string $database | ||
* @param array<AbstractTable> $tables | ||
* | ||
* @return array<MigrationImage> | ||
*/ | ||
public function generate(string $database, array $tables): array | ||
{ | ||
$atomizer = new Atomizer(new Renderer()); | ||
|
||
$images = []; | ||
foreach ($this->tableSorter->sort($tables) as $table) { | ||
if ($table->getComparator()->hasChanges()) { | ||
$atomizer->setTables([$table]); | ||
|
||
$image = new MigrationImage($this->migrationConfig, $database); | ||
$image->setName($this->nameGenerator->generate($atomizer)); | ||
$image->fileNamePattern = self::$sec++ . '_{database}_{name}'; | ||
|
||
$atomizer->declareChanges($image->getClass()->getMethod('up')); | ||
$atomizer->revertChanges($image->getClass()->getMethod('down')); | ||
|
||
$images[] = $image; | ||
} | ||
} | ||
|
||
return $images; | ||
} | ||
} |
Oops, something went wrong.