Skip to content

Commit

Permalink
Merge pull request #12 from formal-php/fix-id-collisions
Browse files Browse the repository at this point in the history
Allow users to use an id property in their entities
  • Loading branch information
Baptouuuu authored Feb 11, 2024
2 parents 9d8fc31 + 1bc4a2c commit f9275d5
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- (Optional) Entities id column with the `SQL` adapter now use the Aggregate id as a value, the columns in the Aggregate column referencing these columns have been removed
- `Formal\ORM\Raw\Aggregate\Collection::properties()` has been renamed `::entities()`
- Collection tables now have an `entityReference` column
- Collection, entities and optional entities table column `id` has been renamed `aggregateId`

## 1.2.0 - 2024-01-15

Expand Down
6 changes: 3 additions & 3 deletions proofs/adapter/sql/showCreateTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ static function($assert) {
CREATE TABLE `user` (`id` varchar(36) NOT NULL COMMENT 'UUID', `createdAt` varchar(32) NOT NULL COMMENT 'Date with timezone down to the microsecond', `name` longtext DEFAULT NULL COMMENT 'TODO adjust the type depending on your use case', `nameStr` longtext DEFAULT NULL COMMENT 'TODO adjust the type depending on your use case', PRIMARY KEY (`id`))
SQL,
<<<SQL
CREATE TABLE `user_mainAddress` (`id` varchar(36) NOT NULL COMMENT 'UUID', `value` longtext NOT NULL COMMENT 'TODO adjust the type depending on your use case', CONSTRAINT `FK_user_mainAddress` FOREIGN KEY (`id`) REFERENCES `user`(`id`) ON DELETE CASCADE, UNIQUE (`id`))
CREATE TABLE `user_mainAddress` (`aggregateId` varchar(36) NOT NULL COMMENT 'UUID', `value` longtext NOT NULL COMMENT 'TODO adjust the type depending on your use case', CONSTRAINT `FK_user_mainAddress` FOREIGN KEY (`aggregateId`) REFERENCES `user`(`id`) ON DELETE CASCADE, UNIQUE (`aggregateId`))
SQL,
<<<SQL
CREATE TABLE `user_billingAddress` (`id` varchar(36) NOT NULL COMMENT 'UUID', `value` longtext NOT NULL COMMENT 'TODO adjust the type depending on your use case', CONSTRAINT `FK_user_billingAddress` FOREIGN KEY (`id`) REFERENCES `user`(`id`) ON DELETE CASCADE, UNIQUE (`id`))
CREATE TABLE `user_billingAddress` (`aggregateId` varchar(36) NOT NULL COMMENT 'UUID', `value` longtext NOT NULL COMMENT 'TODO adjust the type depending on your use case', CONSTRAINT `FK_user_billingAddress` FOREIGN KEY (`aggregateId`) REFERENCES `user`(`id`) ON DELETE CASCADE, UNIQUE (`aggregateId`))
SQL,
<<<SQL
CREATE TABLE `user_addresses` (`entityReference` varchar(36) NOT NULL COMMENT 'UUID', `id` varchar(36) NOT NULL COMMENT 'UUID', `value` longtext NOT NULL COMMENT 'TODO adjust the type depending on your use case', PRIMARY KEY (`entityReference`), CONSTRAINT `FK_user_addresses` FOREIGN KEY (`id`) REFERENCES `user`(`id`) ON DELETE CASCADE)
CREATE TABLE `user_addresses` (`entityReference` varchar(36) NOT NULL COMMENT 'UUID', `aggregateId` varchar(36) NOT NULL COMMENT 'UUID', `value` longtext NOT NULL COMMENT 'TODO adjust the type depending on your use case', PRIMARY KEY (`entityReference`), CONSTRAINT `FK_user_addresses` FOREIGN KEY (`aggregateId`) REFERENCES `user`(`id`) ON DELETE CASCADE)
SQL,
])
->same($queries);
Expand Down
6 changes: 3 additions & 3 deletions src/Adapter/SQL/CollectionTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private function __construct(
->in($this->name)
->as($definition->name().'_'.$property->name()),
);
$this->id = Column\Name::of('id')->in($this->name);
$this->id = Column\Name::of('aggregateId')->in($this->name);
$this->reference = Column\Name::of('entityReference')->in($this->name);
$this->select = Select::from($this->name)->columns(
$this->id,
Expand Down Expand Up @@ -92,7 +92,7 @@ public function primaryKey(): Table\Column
public function foreignKey(): Table\Column
{
return Table\Column::of(
Table\Column\Name::of('id'),
Table\Column\Name::of('aggregateId'),
Table\Column\Type::varchar(36)->comment('UUID'),
);
}
Expand Down Expand Up @@ -161,7 +161,7 @@ public function insert(Id $id, Set $collection): Maybe
->map(
static fn($entity) => new Row(
new Row\Value(
Column\Name::of('id')->in($table),
Column\Name::of('aggregateId')->in($table),
$id->value(),
),
new Row\Value(
Expand Down
6 changes: 3 additions & 3 deletions src/Adapter/SQL/EntityTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static function of(
public function primaryKey(): Table\Column
{
return Table\Column::of(
Table\Column\Name::of('id'),
Table\Column\Name::of('aggregateId'),
Table\Column\Type::varchar(36)->comment('UUID'),
);
}
Expand Down Expand Up @@ -116,7 +116,7 @@ public function insert(Id $id, Set $properties): Query
$table,
new Row(
new Row\Value(
Column\Name::of('id')->in($table),
Column\Name::of('aggregateId')->in($table),
$id->value(),
),
...$properties
Expand Down Expand Up @@ -152,7 +152,7 @@ public function update(Id $id, Set $properties): Maybe
->toList(),
),
)->where(Specification\Property::of(
'id',
'aggregateId',
Sign::equality,
$id->value(),
)),
Expand Down
4 changes: 2 additions & 2 deletions src/Adapter/SQL/MainTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private function __construct(Definition $definition)
fn(Select $select, $_, $table) => $select->join(
Join::left($table->name())->on(
Column\Name::of($this->definition->id()->property())->in($this->name),
Column\Name::of('id')->in($table->name()),
$table->primaryKey()->name()->in($table->name()),
),
),
);
Expand Down Expand Up @@ -129,7 +129,7 @@ private function __construct(Definition $definition)
fn(Select $select, $_, $table) => $select->join(
Join::left($table->name())->on(
Column\Name::of($this->definition->id()->property())->in($this->name),
Column\Name::of('id')->in($table->name()),
$table->primaryKey()->name()->in($table->name()),
),
),
);
Expand Down
12 changes: 6 additions & 6 deletions src/Adapter/SQL/OptionalTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private function __construct(
->as($definition->name().'_'.$property->name()),
);
$this->select = Select::onDemand($this->name)->columns(
Column\Name::of('id')->in($this->name),
Column\Name::of('aggregateId')->in($this->name),
...$this->columns->toList(),
);
}
Expand All @@ -79,7 +79,7 @@ public static function of(
public function primaryKey(): Table\Column
{
return Table\Column::of(
Table\Column\Name::of('id'),
Table\Column\Name::of('aggregateId'),
Table\Column\Type::varchar(36)->comment('UUID'),
);
}
Expand Down Expand Up @@ -117,7 +117,7 @@ public function columns(): Set
public function select(Id $id): Select
{
return $this->select->where(PropertySpecification::of(
'id',
'aggregateId',
Sign::equality,
$id->value(),
));
Expand All @@ -136,7 +136,7 @@ public function insert(Id $id, Set $properties): Query
$table,
new Row(
new Row\Value(
Column\Name::of('id')->in($table),
Column\Name::of('aggregateId')->in($table),
$id->value(),
),
...$properties
Expand Down Expand Up @@ -183,14 +183,14 @@ public function update(Id $id, Optional|Optional\BrandNew $optional): Sequence
),
)
->where(PropertySpecification::of(
'id',
'aggregateId',
Sign::equality,
$id->value(),
)),
),
fn() => Sequence::of(
Delete::from($this->name)->where(PropertySpecification::of(
'id',
'aggregateId',
Sign::equality,
$id->value(),
)),
Expand Down

0 comments on commit f9275d5

Please sign in to comment.