diff --git a/src/Contracts/TableSchema.php b/src/Contracts/TableSchema.php index f1b2361..e7ffcdc 100644 --- a/src/Contracts/TableSchema.php +++ b/src/Contracts/TableSchema.php @@ -16,5 +16,5 @@ public function getColumns(): Collection; /** * @return Collection */ - public function getPrimaryKey(): Collection; + public function getPrimaryKeys(): Collection; } diff --git a/src/ErdFinder.php b/src/ErdFinder.php index 59b9d65..f95fc45 100644 --- a/src/ErdFinder.php +++ b/src/ErdFinder.php @@ -85,8 +85,8 @@ private function findByModels(Collection $models, array $excludes = []): Collect ->map(function (Collection $relations) use ($excludes) { return $relations ->reject(fn (Relation $relation) => $relation->excludes($excludes)) - ->sortBy(fn (Relation $relation) => $relation->unique()) - ->unique(fn (Relation $relation) => $relation->unique()) + ->sortBy(fn (Relation $relation) => $relation->sortByKeys()) + ->unique(fn (Relation $relation) => $relation->sortByKeys()) ->groupBy(fn (Relation $relation) => $relation->localTable()); }); diff --git a/src/Relation.php b/src/Relation.php index a46eb63..07cbb44 100644 --- a/src/Relation.php +++ b/src/Relation.php @@ -127,21 +127,29 @@ public function relatedRelation(): Relation ])); } - public function order(): int + public function sortByRelation(): int { - $orders = [ + $relationGroups = [ [BelongsTo::class, HasOne::class, MorphOne::class], [HasMany::class, MorphMany::class], ]; $type = $this->type(); - foreach ($orders as $index => $order) { - if (in_array($type, $order, true)) { + foreach ($relationGroups as $index => $relations) { + if (in_array($type, $relations, true)) { return $index + 1; } } - return count($orders); + return count($relationGroups); + } + + /** + * @return string[] + */ + public function sortByKeys(): array + { + return [$this->type(), $this->localKey(), $this->foreignKey()]; } public function uniqueId(): string @@ -154,12 +162,4 @@ public function uniqueId(): string return implode('::', $sortBy); } - - /** - * @return string[] - */ - public function unique(): array - { - return [$this->type(), $this->localKey(), $this->foreignKey()]; - } } diff --git a/src/Schema/DBAL/TableSchema.php b/src/Schema/DBAL/TableSchema.php index f524f22..e470afa 100644 --- a/src/Schema/DBAL/TableSchema.php +++ b/src/Schema/DBAL/TableSchema.php @@ -30,7 +30,7 @@ public function getColumns(): Collection return collect($this->table->getColumns())->map(fn (DBALColumn $column) => new ColumnSchema($column)); } - public function getPrimaryKey(): Collection + public function getPrimaryKeys(): Collection { return collect($this->table->getIndexes()) ->filter(fn (Index $index) => $index->isPrimary()) diff --git a/src/Schema/Laravel/TableSchema.php b/src/Schema/Laravel/TableSchema.php index f7952cb..7f6dece 100644 --- a/src/Schema/Laravel/TableSchema.php +++ b/src/Schema/Laravel/TableSchema.php @@ -28,7 +28,7 @@ public function getColumns(): Collection return collect($this->builder->getColumns($this->name))->map(fn (array $column) => new ColumnSchema($column)); } - public function getPrimaryKey(): Collection + public function getPrimaryKeys(): Collection { return collect($this->builder->getIndexes($this->name)) ->filter(fn (array $column) => $column['primary'] === true) diff --git a/src/Table.php b/src/Table.php index 77e7371..d09e745 100644 --- a/src/Table.php +++ b/src/Table.php @@ -34,9 +34,9 @@ public function getColumns(): Collection return $this->schema->getColumns(); } - public function getPrimaryKey(): Collection + public function getPrimaryKeys(): Collection { - return $this->schema->getPrimaryKey(); + return $this->schema->getPrimaryKeys(); } /** diff --git a/src/Template/DDL.php b/src/Template/DDL.php index 8d6bf75..8211082 100644 --- a/src/Template/DDL.php +++ b/src/Template/DDL.php @@ -55,7 +55,7 @@ private function renderColumn(Table $table): string */ private function renderPrimaryKeys(Table $table): array { - $primaryKeys = $table->getPrimaryKey()->implode(', '); + $primaryKeys = $table->getPrimaryKeys()->implode(', '); return $primaryKeys ? ["PRIMARY KEY({$primaryKeys})"] : []; } diff --git a/src/Template/Er.php b/src/Template/Er.php index 55d8978..673358e 100644 --- a/src/Template/Er.php +++ b/src/Template/Er.php @@ -21,7 +21,7 @@ class Er implements Template { /** @var string[] */ - private static array $relations = [ + private static array $relationships = [ BelongsTo::class => '1--*', MorphTo::class => '1--*', HasOne::class => '1--1', @@ -74,7 +74,7 @@ public function save(Collection $tables, string $path, array $options = []): int private function renderTable(Table $table): string { - $primaryKeys = $table->getPrimaryKey(); + $primaryKeys = $table->getPrimaryKeys(); $indexes = $table ->getRelations() ->flatMap(fn (Relation $relation) => [$relation->localColumn(), $relation->morphColumn()]) @@ -105,7 +105,7 @@ private function renderRelation(Relation $relation): string return sprintf( '%s %s %s', $relation->localTable(), - self::$relations[$relation->type()], + self::$relationships[$relation->type()], $relation->foreignTable() ); } diff --git a/tests/RelationFinderTest.php b/tests/RelationFinderTest.php index e0ae959..d833ef0 100644 --- a/tests/RelationFinderTest.php +++ b/tests/RelationFinderTest.php @@ -460,7 +460,7 @@ private function draw(Collection $relations, $method): array { return $relations ->get($method) - ->sortBy(fn (Relation $relation) => $relation->order()) + ->sortBy(fn (Relation $relation) => $relation->sortByRelation()) ->map(fn (Relation $relation) => $this->renderRelationship($relation)) ->toArray(); }