33namespace N3XT0R \MigrationGenerator \Service \Generator \Normalization \Processors ;
44
55use N3XT0R \MigrationGenerator \Service \Generator \Definition \Entity \FieldEntity ;
6+ use N3XT0R \MigrationGenerator \Service \Generator \Definition \Entity \IndexEntity ;
67use N3XT0R \MigrationGenerator \Service \Generator \Definition \Entity \PrimaryKeyEntity ;
78use N3XT0R \MigrationGenerator \Service \Generator \Definition \Entity \ResultEntity ;
89use 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}
0 commit comments