Skip to content

Commit 1266ae0

Browse files
authored
Merge pull request #7 from N3XT0R/feature/7-schema-normalization
Feature/7 schema normalization
2 parents f3736a0 + 6de3c79 commit 1266ae0

File tree

16 files changed

+223
-31
lines changed

16 files changed

+223
-31
lines changed

src/Service/Generator/Compiler/Mapper/IndexMapper.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ protected function generateIndex(IndexEntity $index): string
4747
$method .= "'" . $columns[0] . "'";
4848
}
4949

50-
$method .= ", '" . $index->getName() . "')";
50+
$name = trim($index->getName());
51+
if ($name !== '') {
52+
$method .= ", '" . $name . "'";
53+
}
54+
55+
$method .= ')';
56+
5157
$methods = [$method];
5258

5359
return $this->chainMethodsToString($methods);

src/Service/Generator/MigrationGenerator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ public function generateMigrationForTable(
130130

131131
$normalizationManager = $this->getNormalizationManager();
132132
if ($normalizationManager) {
133-
//dd($schemaResult);
134133
$schemaResult = $normalizationManager->normalize($schemaResult);
135134
}
136135

src/Service/Generator/Normalization/Processors/PivotProcessor.php

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace N3XT0R\MigrationGenerator\Service\Generator\Normalization\Processors;
44

55
use N3XT0R\MigrationGenerator\Service\Generator\Definition\Entity\FieldEntity;
6+
use N3XT0R\MigrationGenerator\Service\Generator\Definition\Entity\IndexEntity;
67
use N3XT0R\MigrationGenerator\Service\Generator\Definition\Entity\PrimaryKeyEntity;
78
use N3XT0R\MigrationGenerator\Service\Generator\Definition\Entity\ResultEntity;
89
use N3XT0R\MigrationGenerator\Service\Generator\Normalization\Context\NormalizationContext;
@@ -21,26 +22,63 @@ public function process(NormalizationContext $context): ResultEntity
2122
$results = $result->getResults();
2223

2324
foreach ($results as $tableName => $definitions) {
25+
if (!isset($definitions['primaryKey'])) {
26+
continue;
27+
}
28+
29+
/** @var PrimaryKeyEntity|null $primary */
2430
$primary = current($definitions['primaryKey']);
25-
if ($primary instanceof PrimaryKeyEntity && count($primary->getColumns()) >= 2) {
26-
$idField = new FieldEntity();
27-
$idField->setTable($tableName);
28-
$idField->setType('bigInteger');
29-
$idField->setArguments(['autoIncrement' => true]);
30-
$idField->setOptions([
31-
'default' => null,
32-
'unsigned' => true,
33-
'nullable' => false
34-
]);
35-
36-
$idField->setColumnName('id');
37-
$results[$tableName]['table'] = ['id' => $idField, ...$definitions['table']];
38-
unset($results[$tableName]['primaryKey']);
31+
if (!$this->isCompositePrimaryKey($primary)) {
32+
continue;
3933
}
34+
35+
$results[$tableName] = $this->transformPivotTable($tableName, $definitions, $primary);
4036
}
4137

4238
$result->setResults($results);
4339
$context->update($result);
40+
4441
return $result;
4542
}
43+
44+
protected function isCompositePrimaryKey(?PrimaryKeyEntity $primary): bool
45+
{
46+
return $primary instanceof PrimaryKeyEntity && count($primary->getColumns()) >= 2;
47+
}
48+
49+
protected function transformPivotTable(string $tableName, array $definitions, PrimaryKeyEntity $primary): array
50+
{
51+
$definitions['table'] = $this->prependIdField($tableName, $definitions['table']);
52+
unset($definitions['primaryKey']);
53+
$definitions['index'][] = $this->createUniqueIndexFromPrimaryKey($primary);
54+
55+
return $definitions;
56+
}
57+
58+
protected function prependIdField(string $tableName, array $fields): array
59+
{
60+
$idField = new FieldEntity();
61+
$idField->setTable($tableName);
62+
$idField->setType('bigInteger');
63+
$idField->setArguments(['autoIncrement' => true]);
64+
$idField->setOptions([
65+
'default' => null,
66+
'unsigned' => true,
67+
'nullable' => false,
68+
]);
69+
$idField->setColumnName('id');
70+
71+
return ['id' => $idField] + $fields;
72+
}
73+
74+
protected function createUniqueIndexFromPrimaryKey(PrimaryKeyEntity $primary): IndexEntity
75+
{
76+
$index = new IndexEntity();
77+
$index->setType('unique');
78+
$index->setIndexType('index');
79+
$index->setColumns($primary->getColumns());
80+
$index->setName(implode('_', $primary->getColumns()) . '_unique');
81+
82+
return $index;
83+
}
4684
}

tests/Unit/Generator/Compiler/Mapper/AbstractMapperTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use N3XT0R\MigrationGenerator\Service\Generator\Compiler\Mapper\AbstractMapper;
88
use PHPUnit\Framework\Attributes\DataProvider;
9-
use Tests\TestCase;
9+
use PHPUnit\Framework\TestCase;
1010

1111
class AbstractMapperTest extends TestCase
1212
{
@@ -45,8 +45,8 @@ public static function chainMethodProvider(): array
4545
}
4646

4747
/**
48-
* @param array $methods
49-
* @param string $expectedResult
48+
* @param array $methods
49+
* @param string $expectedResult
5050
*/
5151
#[DataProvider('chainMethodProvider')]
5252
public function testChainMethodsToStringWorks(array $methods, string $expectedResult): void

tests/Unit/Generator/Compiler/Mapper/FieldMapperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use N3XT0R\MigrationGenerator\Service\Generator\Compiler\Mapper\FieldMapper;
88
use N3XT0R\MigrationGenerator\Service\Generator\Definition\Entity\FieldEntity;
99
use PHPUnit\Framework\Attributes\DataProvider;
10-
use Tests\TestCase;
10+
use PHPUnit\Framework\TestCase;
1111

1212
class FieldMapperTest extends TestCase
1313
{

tests/Unit/Generator/Compiler/Mapper/ForeignKeyMapperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use N3XT0R\MigrationGenerator\Service\Generator\Compiler\Mapper\ForeignKeyMapper;
88
use N3XT0R\MigrationGenerator\Service\Generator\Definition\Entity\ForeignKeyEntity;
9-
use Tests\TestCase;
9+
use PHPUnit\Framework\TestCase;
1010

1111
class ForeignKeyMapperTest extends TestCase
1212
{

tests/Unit/Generator/Compiler/Mapper/IndexMapperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use N3XT0R\MigrationGenerator\Service\Generator\Compiler\Mapper\IndexMapper;
88
use N3XT0R\MigrationGenerator\Service\Generator\Definition\Entity\IndexEntity;
9-
use Tests\TestCase;
9+
use PHPUnit\Framework\TestCase;
1010

1111
class IndexMapperTest extends TestCase
1212
{

tests/Unit/Generator/Definition/AbstractDefinitionTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public function testSetAndGetResultAreSame(): void
4040
}
4141

4242
/**
43-
* @param string $attributeName
44-
* @param bool $expectedResult
43+
* @param string $attributeName
44+
* @param bool $expectedResult
4545
* @testWith ["test", true]
4646
* ["test2", false]
4747
*/
@@ -78,7 +78,7 @@ public function testSetAndGetSchemaAreSame(): void
7878
}
7979

8080
/**
81-
* @param bool $expectedResult
81+
* @param bool $expectedResult
8282
* @testWith [true]
8383
* [false]
8484
*/

tests/Unit/Generator/Definition/Entity/AbstractIndexEntityTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Generator\Definition\Entity;
44

55
use N3XT0R\MigrationGenerator\Service\Generator\Definition\Entity\AbstractIndexEntity;
6-
use Tests\TestCase;
6+
use PHPUnit\Framework\TestCase;
77

88
class AbstractIndexEntityTest extends TestCase
99
{

tests/Unit/Generator/Definition/Entity/FieldEntityTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
use N3XT0R\MigrationGenerator\Service\Generator\Definition\Entity\FieldEntity;
8-
use Tests\TestCase;
8+
use PHPUnit\Framework\TestCase;
99

1010
class FieldEntityTest extends TestCase
1111
{

0 commit comments

Comments
 (0)