Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  specify next release
  typo
  allow to add if not exists statement to create table queries
  • Loading branch information
Baptouuuu committed Oct 2, 2024
2 parents 9dd02a1 + 24f53a2 commit 2382c48
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 3.4.0 - 2024-10-02

### Added

- `Formal\ORM\Adapter\SQL\ShowCreateTable::ifNotExists()`

## 3.3.0 - 2024-09-29

### Added
Expand Down
3 changes: 3 additions & 0 deletions documentation/adapters/sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ CREATE TABLE `user` (`id` varchar(36) NOT NULL COMMENT 'UUID', `name` varchar(
CREATE TABLE `user_addresses` (`aggregateId` varchar(36) NOT NULL COMMENT 'UUID', `street` longtext NOT NULL COMMENT 'TODO adjust the type depending on your use case', `zipCode` longtext NOT NULL COMMENT 'TODO adjust the type depending on your use case', `city` longtext NOT NULL COMMENT 'TODO adjust the type depending on your use case', CONSTRAINT `FK_user_addresses` FOREIGN KEY (`aggregateId`) REFERENCES `user`(`id`) ON DELETE CASCADE);
```

??? tip
You can call `#!php $show->ifNotExists()(User::class)` to instead generate `CREATE TABLE IF NOT EXISTS` queries.

Instead of printing the queries you can execute them directly like this:

```php title="show_create_tables.php" hl_lines="8 15-17 19"
Expand Down
2 changes: 1 addition & 1 deletion documentation/specifications/custom.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Depending of the kind of entity you'd use this like this:

=== "Entity colleciton"
```php
use Formal\ORM\Specification\Just;
use Formal\ORM\Specification\Child;

Child::of('addresses', SearchByCity::of('Paris'));
```
Expand Down
27 changes: 27 additions & 0 deletions proofs/adapter/sql/showCreateTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,33 @@ static function($assert) {
SQL,
])
->same($queries);

$queries = $show->ifNotExists()(User::class)
->map(static fn($query) => $query->sql(Driver::mysql))
->toList();

$assert
->expected([
<<<SQL
CREATE TABLE IF NOT EXISTS `user` (`id` char(36) NOT NULL COMMENT 'UUID', `createdAt` char(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 IF NOT EXISTS `user_mainAddress` (`aggregateId` char(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 IF NOT EXISTS `user_billingAddress` (`aggregateId` char(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 IF NOT EXISTS `user_sibling` (`aggregateId` char(36) NOT NULL COMMENT 'UUID', `id` char(36) NOT NULL COMMENT 'UUID', CONSTRAINT `FK_user_sibling` FOREIGN KEY (`aggregateId`) REFERENCES `user`(`id`) ON DELETE CASCADE, UNIQUE (`aggregateId`))
SQL,
<<<SQL
CREATE TABLE IF NOT EXISTS `user_addresses` (`aggregateId` char(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 IF NOT EXISTS `user_roles` (`aggregateId` char(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)
SQL,
])
->same($queries);
},
)->tag(Storage::sql);

Expand Down
53 changes: 45 additions & 8 deletions src/Adapter/SQL/ShowCreateTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,28 @@
use Formal\AccessLayer\{
Query,
Query\Constraint\ForeignKey,
Table\Name,
Table\Column,
};
use Innmind\Immutable\Sequence;

final class ShowCreateTable
{
private Aggregates $aggregates;
private MapType $mapType;
private bool $ifNotExists;

private function __construct(Aggregates $aggregates)
{
/**
* @psalm-mutation-free
*/
private function __construct(
Aggregates $aggregates,
MapType $mapType,
bool $ifNotExists,
) {
$this->aggregates = $aggregates;
$this->mapType = MapType::new();
$this->mapType = $mapType;
$this->ifNotExists = $ifNotExists;
}

/**
Expand All @@ -30,10 +40,19 @@ public function __invoke(string $class): Sequence
{
$definition = $this->aggregates->get($class);
$mainTable = MainTable::of($definition);
/** @psalm-suppress NamedArgumentNotAllowed */
$create = match ($this->ifNotExists) {
true => static fn(Name $name, Column $first, Column ...$rest) => Query\CreateTable::ifNotExists(
$name, $first, ...$rest,
),
false => static fn(Name $name, Column $first, Column ...$rest) => Query\CreateTable::named(
$name, $first, ...$rest,
),
};

$entities = $mainTable
->entities()
->map(fn($entity) => Query\CreateTable::named(
->map(fn($entity) => $create(
$entity->name()->name(),
$entity->primaryKey(),
...$entity
Expand All @@ -54,7 +73,7 @@ public function __invoke(string $class): Sequence
->toList();
$optionals = $mainTable
->optionals()
->map(fn($optional) => Query\CreateTable::named(
->map(fn($optional) => $create(
$optional->name()->name(),
$optional->primaryKey(),
...$optional
Expand All @@ -77,7 +96,7 @@ public function __invoke(string $class): Sequence
$collections = $mainTable
->collections()
->map(
fn($collection) => Query\CreateTable::named(
fn($collection) => $create(
$collection->name()->name(),
$collection->foreignKey(),
...$collection
Expand All @@ -96,7 +115,7 @@ public function __invoke(string $class): Sequence
)
->toList();

$main = Query\CreateTable::named(
$main = $create(
$mainTable->name()->name(),
$mainTable->primaryKey(),
...$mainTable
Expand All @@ -114,6 +133,24 @@ public function __invoke(string $class): Sequence

public static function of(Aggregates $aggregates): self
{
return new self($aggregates);
return new self(
$aggregates,
MapType::new(),
false,
);
}

/**
* This will add the "IF NOT EXIST" to the sql queries
*
* @psalm-mutation-free
*/
public function ifNotExists(): self
{
return new self(
$this->aggregates,
$this->mapType,
true,
);
}
}

0 comments on commit 2382c48

Please sign in to comment.