Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  specify next release
  update changelog
  fix invalid cast of false value
  use multiple inserts queries to insert a collection to prevent any silent failures
  add property that make the collection insertion fail
  use unsorted shortcut
  require immutable 5.4
  • Loading branch information
Baptouuuu committed May 29, 2024
2 parents 7f2089b + 9a41693 commit 98bd217
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 40 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## 2.0.2 - 2024-05-29

### Changed

- Requires `innmind/immutable:~5.4`
- Requires `formal/access-layer:~2.17`

### Fixed

- `false` values not being persisted
- Silent insert failures of entities inside collections

## 2.0.1 - 2024-05-22

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
},
"require": {
"php": "~8.2",
"innmind/immutable": "~4.15|~5.0",
"innmind/immutable": "~5.4",
"innmind/specification": "~3.0",
"ramsey/uuid": "~4.7",
"innmind/reflection": "~5.1",
"innmind/filesystem": "~7.4",
"innmind/json": "~1.3",
"innmind/time-continuum": "~3.3",
"innmind/type": "~1.2",
"formal/access-layer": "~2.15",
"formal/access-layer": "~2.17",
"innmind/http-transport": "~7.2",
"innmind/url-template": "~3.1",
"innmind/validation": "~1.4"
Expand Down
2 changes: 1 addition & 1 deletion fixtures/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public function addAddress(string $address): self
$this->name,
$this->mainAddress,
$this->billingAddress,
($this->addresses)(User\Address::new($address)),
($this->addresses)(User\Address::new($address)->disable()),
$this->role,
$this->roles,
);
Expand Down
16 changes: 14 additions & 2 deletions fixtures/User/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,28 @@ final class Address
*/
private Sortable $sortable;
private ?int $id = null;
/**
* This property with a `false` value exist to showcase 2 bugs:
* - when not specifying the parameter type for SQL it fails to correctly coalesce it to `0`
* - when failing to SQL insert an entity inside a collection it silently fails
*/
private bool $enabled;

private function __construct(string $value)
private function __construct(string $value, bool $enabled)
{
$this->value = $value;
$this->sortable = new Sortable($value);
$this->enabled = $enabled;
}

public static function new(string $value): self
{
return new self($value);
return new self($value, true);
}

public function disable(): self
{
return new self($this->value, false);
}

public function toString(): string
Expand Down
9 changes: 9 additions & 0 deletions proofs/adapter/elasticsearch/mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ static function($assert) {
'id' => [
'type' => 'long',
],
'enabled' => [
'type' => 'boolean',
],
],
],
'billingAddress' => [
Expand All @@ -57,6 +60,9 @@ static function($assert) {
'id' => [
'type' => 'long',
],
'enabled' => [
'type' => 'boolean',
],
],
],
'addresses' => [
Expand All @@ -68,6 +74,9 @@ static function($assert) {
'id' => [
'type' => 'long',
],
'enabled' => [
'type' => 'boolean',
],
],
],
'roles' => [
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', `role` longtext DEFAULT NULL COMMENT 'TODO adjust the type depending on your use case', PRIMARY KEY (`id`))
SQL,
<<<SQL
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', `id` bigint DEFAULT NULL COMMENT 'TODO Adjust the size depending on your use case', CONSTRAINT `FK_user_mainAddress` FOREIGN KEY (`aggregateId`) REFERENCES `user`(`id`) ON DELETE CASCADE, UNIQUE (`aggregateId`))
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', `id` bigint DEFAULT NULL COMMENT 'TODO Adjust the size depending on your use case', `enabled` tinyint(1) NOT NULL COMMENT 'Boolean', CONSTRAINT `FK_user_mainAddress` FOREIGN KEY (`aggregateId`) REFERENCES `user`(`id`) ON DELETE CASCADE, UNIQUE (`aggregateId`))
SQL,
<<<SQL
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', `id` bigint DEFAULT NULL COMMENT 'TODO Adjust the size depending on your use case', CONSTRAINT `FK_user_billingAddress` FOREIGN KEY (`aggregateId`) REFERENCES `user`(`id`) ON DELETE CASCADE, UNIQUE (`aggregateId`))
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', `id` bigint DEFAULT NULL COMMENT 'TODO Adjust the size depending on your use case', `enabled` tinyint(1) NOT NULL COMMENT 'Boolean', CONSTRAINT `FK_user_billingAddress` FOREIGN KEY (`aggregateId`) REFERENCES `user`(`id`) ON DELETE CASCADE, UNIQUE (`aggregateId`))
SQL,
<<<SQL
CREATE TABLE `user_addresses` (`aggregateId` varchar(36) NOT NULL COMMENT 'UUID', `value` longtext NOT NULL COMMENT 'TODO adjust the type depending on your use case', `id` bigint DEFAULT NULL COMMENT 'TODO Adjust the size depending on your use case', CONSTRAINT `FK_user_addresses` FOREIGN KEY (`aggregateId`) REFERENCES `user`(`id`) ON DELETE CASCADE)
CREATE TABLE `user_addresses` (`aggregateId` varchar(36) NOT NULL COMMENT 'UUID', `value` longtext NOT NULL COMMENT 'TODO adjust the type depending on your use case', `id` bigint DEFAULT NULL COMMENT 'TODO Adjust the size depending on your use case', `enabled` tinyint(1) NOT NULL COMMENT 'Boolean', CONSTRAINT `FK_user_addresses` FOREIGN KEY (`aggregateId`) REFERENCES `user`(`id`) ON DELETE CASCADE)
SQL,
<<<SQL
CREATE TABLE `user_roles` (`aggregateId` varchar(36) NOT NULL COMMENT 'UUID', `name` longtext NOT NULL COMMENT 'TODO adjust the type depending on your use case', CONSTRAINT `FK_user_roles` FOREIGN KEY (`aggregateId`) REFERENCES `user`(`id`) ON DELETE CASCADE)
Expand Down
4 changes: 3 additions & 1 deletion src/Adapter/Elasticsearch/Encode.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public function __invoke(Diff|Aggregate $data): Content
$collections = $data
->collections()
->map(fn($collection) => [
$collection->name() => Sequence::of(...$collection->entities()->toList())
$collection->name() => $collection
->entities()
->unsorted()
->map(fn($entity) => $this->properties($entity->properties()))
->toList(),
])
Expand Down
5 changes: 3 additions & 2 deletions src/Adapter/Filesystem/Encode.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
Name,
};
use Innmind\Json\Json;
use Innmind\Immutable\Sequence;

/**
* @internal
Expand Down Expand Up @@ -100,7 +99,9 @@ public function __invoke(Aggregate|Diff $data): Directory
static fn($collection) => File::named(
$collection->name(),
Content::ofString(Json::encode(
Sequence::of(...$collection->entities()->toList())
$collection
->entities()
->unsorted()
->map(
static fn($entity) => $entity
->properties()
Expand Down
45 changes: 18 additions & 27 deletions src/Adapter/SQL/CollectionTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Innmind\Specification\Specification;
use Innmind\Immutable\{
Set,
Maybe,
Sequence,
};

Expand Down Expand Up @@ -134,37 +133,30 @@ public function select(Id $id): Query
*
* @param Set<Entity> $collection
*
* @return Maybe<Query>
* @return Sequence<Query>
*/
public function insert(Id $id, Set $collection): Maybe
public function insert(Id $id, Set $collection): Sequence
{
$table = $this->name->name();

/** @psalm-suppress InvalidScalarArgument Psalm doesn't understand the !empty() */
return Maybe::just($collection)
->filter(static fn($collection) => !$collection->empty())
->map(
static fn($collection) => Query\Insert::into(
$table,
...$collection
->map(
static fn($entity) => new Row(
new Row\Value(
Column\Name::of('aggregateId')->in($table),
$id->value(),
),
...$entity
->properties()
->map(static fn($property) => new Row\Value(
Column\Name::of($property->name())->in($table),
$property->value(),
))
->toList(),
),
)
return $collection
->unsorted()
->map(static fn($entity) => Query\Insert::into(
$table,
new Row(
new Row\Value(
Column\Name::of('aggregateId')->in($table),
$id->value(),
),
...$entity
->properties()
->map(static fn($property) => new Row\Value(
Column\Name::of($property->name())->in($table),
$property->value(),
))
->toList(),
),
);
));
}

/**
Expand All @@ -190,7 +182,6 @@ public function update(
)),
...$this
->insert($id, $entities)
->toSequence()
->toList(),
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Adapter/SQL/Encode.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ private function collections(Aggregate $data): Sequence
fn($collection) => $this
->mainTable
->collection($collection->name())
->toSequence()
->flatMap(
static fn($table) => $table->insert(
$data->id(),
$collection->entities(),
),
)
->toSequence(),
),
);
}
}

0 comments on commit 98bd217

Please sign in to comment.