-
-
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.
Merge pull request #21: ChangesCountNameGenerator
- Loading branch information
Showing
8 changed files
with
380 additions
and
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Schema\Generator\Migrations\Changes; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
enum ChangeType | ||
{ | ||
case CreateTable; | ||
case DropTable; | ||
case RenameTable; | ||
case ChangeTable; | ||
case AddColumn; | ||
case DropColumn; | ||
case AlterColumn; | ||
case AddIndex; | ||
case DropIndex; | ||
case AlterIndex; | ||
case AddFk; | ||
case DropFk; | ||
case AlterFk; | ||
} |
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,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Schema\Generator\Migrations\Changes; | ||
|
||
use Cycle\Database\Schema\AbstractTable; | ||
use Cycle\Migrations\Atomizer\Atomizer; | ||
|
||
final class Collector | ||
{ | ||
/** | ||
* @return array<array{ChangeType, non-empty-string}> | ||
*/ | ||
public function collect(Atomizer $atomizer): array | ||
{ | ||
$result = []; | ||
|
||
foreach ($atomizer->getTables() as $table) { | ||
if ($table->getStatus() === AbstractTable::STATUS_NEW) { | ||
$result[] = [ChangeType::CreateTable, $table->getName()]; | ||
continue; | ||
} | ||
|
||
if ($table->getStatus() === AbstractTable::STATUS_DECLARED_DROPPED) { | ||
$result[] = [ChangeType::DropTable, $table->getName()]; | ||
continue; | ||
} | ||
|
||
if ($table->getComparator()->isRenamed()) { | ||
$result[] = [ChangeType::RenameTable, $table->getInitialName()]; | ||
continue; | ||
} | ||
|
||
$result[] = [ChangeType::ChangeTable, $table->getName()]; | ||
|
||
$comparator = $table->getComparator(); | ||
|
||
foreach ($comparator->addedColumns() as $column) { | ||
$result[] = [ChangeType::AddColumn, $column->getName()]; | ||
} | ||
|
||
foreach ($comparator->droppedColumns() as $column) { | ||
$result[] = [ChangeType::DropColumn, $column->getName()]; | ||
} | ||
|
||
foreach ($comparator->alteredColumns() as $column) { | ||
$result[] = [ChangeType::AlterColumn, $column[0]->getName()]; | ||
} | ||
|
||
foreach ($comparator->addedIndexes() as $index) { | ||
$result[] = [ChangeType::AddIndex, $index->getName()]; | ||
} | ||
|
||
foreach ($comparator->droppedIndexes() as $index) { | ||
$result[] = [ChangeType::DropIndex, $index->getName()]; | ||
} | ||
|
||
foreach ($comparator->alteredIndexes() as $index) { | ||
$result[] = [ChangeType::AlterIndex, $index[0]->getName()]; | ||
} | ||
|
||
foreach ($comparator->addedForeignKeys() as $fk) { | ||
$result[] = [ChangeType::AddFk, $fk->getName()]; | ||
} | ||
|
||
foreach ($comparator->droppedForeignKeys() as $fk) { | ||
$result[] = [ChangeType::DropFk, $fk->getName()]; | ||
} | ||
|
||
foreach ($comparator->alteredForeignKeys() as $fk) { | ||
$result[] = [ChangeType::AlterFk, $fk[0]->getName()]; | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
} |
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,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Schema\Generator\Migrations; | ||
|
||
use Cycle\Migrations\Atomizer\Atomizer; | ||
use Cycle\Schema\Generator\Migrations\Changes\ChangeType; | ||
use Cycle\Schema\Generator\Migrations\Changes\Collector; | ||
|
||
/** | ||
* Generates migration name based on changes count. | ||
* | ||
* Like: | ||
* ct1_dt1_t2_c3_i3_fk3 | ||
* There are: | ||
* - 1 create table | ||
* - 1 drop table | ||
* - 2 table changes | ||
* - 3 column changes | ||
* - 3 index changes | ||
* - 3 foreign key changes | ||
*/ | ||
final class ChangesCountNameGenerator implements NameGeneratorInterface | ||
{ | ||
public function generate(Atomizer $atomizer): string | ||
{ | ||
$collector = new Collector(); | ||
$map = []; | ||
foreach ($collector->collect($atomizer) as $pair) { | ||
$key = $this->changeToString($pair[0]); | ||
$map[$key] ??= 0; | ||
$map[$key]++; | ||
} | ||
|
||
$result = []; | ||
foreach ($map as $key => $cnt) { | ||
$result[] = "{$key}{$cnt}"; | ||
} | ||
|
||
return \implode('_', $result); | ||
} | ||
|
||
private function changeToString(ChangeType $change): string | ||
{ | ||
return match ($change) { | ||
ChangeType::CreateTable => 'ct', | ||
ChangeType::DropTable => 'dt', | ||
ChangeType::RenameTable, | ||
ChangeType::ChangeTable => 't', | ||
ChangeType::AddColumn, | ||
ChangeType::DropColumn, | ||
ChangeType::AlterColumn => 'c', | ||
ChangeType::AddIndex, | ||
ChangeType::DropIndex, | ||
ChangeType::AlterIndex => 'i', | ||
ChangeType::AddFk, | ||
ChangeType::DropFk, | ||
ChangeType::AlterFk => 'fk', | ||
}; | ||
} | ||
} |
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
Oops, something went wrong.