Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change count generator #21

Merged
merged 7 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.26.1@d747f6500b38ac4f7dfc5edbcae6e4b637d7add0">
<file src="src/Changes/Collector.php">
<MixedArrayAccess>
<code><![CDATA[$column[0]]]></code>
<code><![CDATA[$fk[0]]]></code>
<code><![CDATA[$index[0]]]></code>
</MixedArrayAccess>
<MixedAssignment>
<code><![CDATA[$column]]></code>
<code><![CDATA[$fk]]></code>
<code><![CDATA[$index]]></code>
</MixedAssignment>
<MixedMethodCall>
<code><![CDATA[getName]]></code>
<code><![CDATA[getName]]></code>
<code><![CDATA[getName]]></code>
</MixedMethodCall>
<MixedReturnTypeCoercion>
<code><![CDATA[$result]]></code>
<code><![CDATA[array<array{ChangeType, non-empty-string}>]]></code>
</MixedReturnTypeCoercion>
</file>
<file src="src/GenerateMigrations.php">
<ArgumentTypeCoercion>
<code><![CDATA[$database]]></code>
Expand All @@ -25,26 +46,4 @@
<code><![CDATA[$this->database]]></code>
</NullableReturnStatement>
</file>
<file src="src/NameBasedOnChangesGenerator.php">
<MixedArrayAccess>
<code><![CDATA[$column[0]]]></code>
<code><![CDATA[$fk[0]]]></code>
<code><![CDATA[$index[0]]]></code>
</MixedArrayAccess>
<MixedAssignment>
<code><![CDATA[$column]]></code>
<code><![CDATA[$fk]]></code>
<code><![CDATA[$index]]></code>
</MixedAssignment>
<MixedMethodCall>
<code><![CDATA[getName]]></code>
<code><![CDATA[getName]]></code>
<code><![CDATA[getName]]></code>
</MixedMethodCall>
<MixedOperand>
<code><![CDATA[$column[0]->getName()]]></code>
<code><![CDATA[$fk[0]->getName()]]></code>
<code><![CDATA[$index[0]->getName()]]></code>
</MixedOperand>
</file>
</files>
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorBaseline="psalm-baseline.xml"
errorLevel="1"
findUnusedBaselineEntry="true"
>
<projectFiles>
<directory name="src" />
Expand Down
25 changes: 25 additions & 0 deletions src/Changes/ChangeType.php
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;
}
78 changes: 78 additions & 0 deletions src/Changes/Collector.php
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;
}
}
62 changes: 62 additions & 0 deletions src/ChangesCountNameGenerator.php
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',
};
}
}
92 changes: 32 additions & 60 deletions src/NameBasedOnChangesGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,44 @@

namespace Cycle\Schema\Generator\Migrations;

use Cycle\Database\Schema\AbstractTable;
use Cycle\Migrations\Atomizer\Atomizer;
use Cycle\Schema\Generator\Migrations\Changes\ChangeType;
use Cycle\Schema\Generator\Migrations\Changes\Collector;

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();
}
}
$collector = new Collector();
return \implode(
'_',
\array_map(
fn(array $pair): string => $this->changeToString($pair[0], $pair[1]),
$collector->collect($atomizer),
),
);
}

return \implode('_', $name);
private function changeToString(ChangeType $change, string $name): string
{
return sprintf(
'%s_%s',
match ($change) {
ChangeType::CreateTable => 'create',
ChangeType::DropTable => 'drop',
ChangeType::RenameTable => 'rename',
ChangeType::ChangeTable => 'change',
ChangeType::AddColumn => 'add',
ChangeType::DropColumn => 'rm',
ChangeType::AlterColumn => 'alter',
ChangeType::AddIndex => 'add_index',
ChangeType::DropIndex => 'rm_index',
ChangeType::AlterIndex => 'alter_index',
ChangeType::AddFk => 'add_fk',
ChangeType::DropFk => 'rm_fk',
ChangeType::AlterFk => 'alter_fk',
},
$name,
);
}
}
Loading
Loading