From d3c91bf85547f401dda335a5a07e418a957d0257 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Tue, 6 Feb 2024 17:27:41 +0200 Subject: [PATCH 1/4] Add generated fields support --- composer.json | 2 +- src/Compiler.php | 13 +++++++ src/Definition/Field.php | 24 +++++++++++++ tests/Schema/CompilerTest.php | 34 +++++++++++++++++++ tests/Schema/Generator/TableGeneratorTest.php | 4 +++ 5 files changed, 76 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8a93f39..31a5e03 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "prefer-stable": true, "require": { "php": ">=8.0", - "cycle/orm": "^2.0", + "cycle/orm": "^2.7", "cycle/database": "^2.7.1", "yiisoft/friendly-exception": "^1.1" }, diff --git a/src/Compiler.php b/src/Compiler.php index 186b932..918bb49 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -84,6 +84,7 @@ private function compute(Registry $registry, Entity $entity): void Schema::FIND_BY_KEYS => $this->renderReferences($entity), Schema::TYPECAST => $this->renderTypecast($entity), Schema::RELATIONS => [], + Schema::GENERATED_FIELDS => $this->renderGeneratedFields($entity), ]; // For table inheritance we need to fill specific schema segments @@ -187,6 +188,18 @@ private function renderColumns(Entity $entity): array return $schema; } + private function renderGeneratedFields(Entity $entity): array + { + $schema = []; + foreach ($entity->getFields() as $name => $field) { + if ($field->getGenerated() !== null) { + $schema[$name] = $field->getGenerated(); + } + } + + return $schema; + } + private function renderTypecast(Entity $entity): array { $schema = []; diff --git a/src/Definition/Field.php b/src/Definition/Field.php index e21c540..164eca2 100644 --- a/src/Definition/Field.php +++ b/src/Definition/Field.php @@ -4,6 +4,7 @@ namespace Cycle\Schema\Definition; +use Cycle\ORM\SchemaInterface; use Cycle\Schema\Definition\Map\OptionMap; use Cycle\Schema\Exception\FieldException; @@ -32,6 +33,11 @@ final class Field */ private array|string|null $typecast = null; + /** + * @var positive-int|null + */ + private ?int $generated = null; + private bool $referenced = false; private ?string $entityClass = null; @@ -138,6 +144,24 @@ public function getTypecast(): array|string|null return $this->typecast; } + /** + * @param positive-int|null $type. Generating type {@see SchemaInterface::GENERATED_*} constants. + */ + public function setGenerated(int|null $type): self + { + $this->generated = $type; + + return $this; + } + + /** + * @return positive-int|null + */ + public function getGenerated(): ?int + { + return $this->generated; + } + public function setReferenced(bool $indexed): self { $this->referenced = $indexed; diff --git a/tests/Schema/CompilerTest.php b/tests/Schema/CompilerTest.php index 776653f..092dd1d 100644 --- a/tests/Schema/CompilerTest.php +++ b/tests/Schema/CompilerTest.php @@ -86,6 +86,40 @@ public function testRenderTypecast(mixed $expected, array $defaults, mixed $enti $this->assertSame($expected, $schema['author'][SchemaInterface::TYPECAST_HANDLER]); } + public function testRenderGeneratedFields(): void + { + $entity = new Entity(); + $entity->setRole('author')->setClass(Author::class); + $entity->getFields()->set('id', (new Field())->setType('primary')->setColumn('id')); + $entity->getFields()->set('name', (new Field())->setType('string')->setColumn('name')); + $entity->getFields()->set('createdAt', (new Field()) + ->setType('datetime') + ->setColumn('created_at') + ->setGenerated(SchemaInterface::GENERATED_PHP_INSERT) + ); + $entity->getFields()->set('updatedAt', (new Field()) + ->setType('datetime') + ->setColumn('created_at') + ->setGenerated(SchemaInterface::GENERATED_PHP_INSERT | SchemaInterface::GENERATED_PHP_UPDATE) + ); + $entity->getFields()->set('sequence', (new Field()) + ->setType('serial') + ->setColumn('some_sequence') + ->setGenerated(SchemaInterface::GENERATED_DB) + ); + + $r = new Registry($this->createMock(DatabaseProviderInterface::class)); + $r->register($entity); + + $schema = (new Compiler())->compile($r); + + $this->assertSame([ + 'createdAt' => SchemaInterface::GENERATED_PHP_INSERT, + 'updatedAt' => SchemaInterface::GENERATED_PHP_INSERT | SchemaInterface::GENERATED_PHP_UPDATE, + 'sequence' => SchemaInterface::GENERATED_DB, + ], $schema['author'][SchemaInterface::GENERATED_FIELDS]); + } + public static function renderTypecastDataProvider(): \Traversable { yield [null, []]; diff --git a/tests/Schema/Generator/TableGeneratorTest.php b/tests/Schema/Generator/TableGeneratorTest.php index c02a2ae..e26af39 100644 --- a/tests/Schema/Generator/TableGeneratorTest.php +++ b/tests/Schema/Generator/TableGeneratorTest.php @@ -67,6 +67,7 @@ public function testCompiled(): void Schema::TYPECAST => [], Schema::SCHEMA => [], Schema::TYPECAST_HANDLER => [Typecaster::class, 'default_typecaster'], + Schema::GENERATED_FIELDS => [], ], ], $schema); } @@ -103,6 +104,7 @@ public function testCompiledWithPassedDefaultTypecastHandler(): void Schema::TYPECAST => [], Schema::SCHEMA => [], Schema::TYPECAST_HANDLER => [Typecaster::class], + Schema::GENERATED_FIELDS => [], ], ], $schema); } @@ -171,6 +173,7 @@ public function testCompiledUser(): void Schema::TYPECAST_HANDLER => [ Typecaster::class, ], + Schema::GENERATED_FIELDS => [], ], ], $c->getSchema()); } @@ -203,6 +206,7 @@ public function testCompiledPost(): void Schema::TYPECAST => [], Schema::SCHEMA => [], Schema::TYPECAST_HANDLER => null, + Schema::GENERATED_FIELDS => [], ], ], $c->getSchema()); } From d649ba30bc30ee92e8313ece4a87e033d31e9208 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 6 Feb 2024 15:28:04 +0000 Subject: [PATCH 2/4] Apply fixes from StyleCI --- tests/Schema/CompilerTest.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/Schema/CompilerTest.php b/tests/Schema/CompilerTest.php index 092dd1d..167ba03 100644 --- a/tests/Schema/CompilerTest.php +++ b/tests/Schema/CompilerTest.php @@ -92,17 +92,23 @@ public function testRenderGeneratedFields(): void $entity->setRole('author')->setClass(Author::class); $entity->getFields()->set('id', (new Field())->setType('primary')->setColumn('id')); $entity->getFields()->set('name', (new Field())->setType('string')->setColumn('name')); - $entity->getFields()->set('createdAt', (new Field()) + $entity->getFields()->set( + 'createdAt', + (new Field()) ->setType('datetime') ->setColumn('created_at') ->setGenerated(SchemaInterface::GENERATED_PHP_INSERT) ); - $entity->getFields()->set('updatedAt', (new Field()) + $entity->getFields()->set( + 'updatedAt', + (new Field()) ->setType('datetime') ->setColumn('created_at') ->setGenerated(SchemaInterface::GENERATED_PHP_INSERT | SchemaInterface::GENERATED_PHP_UPDATE) ); - $entity->getFields()->set('sequence', (new Field()) + $entity->getFields()->set( + 'sequence', + (new Field()) ->setType('serial') ->setColumn('some_sequence') ->setGenerated(SchemaInterface::GENERATED_DB) From 01a40aaf6ce5ba7f3ecc25e135446cf68d7ab9c7 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Tue, 6 Feb 2024 19:41:43 +0200 Subject: [PATCH 3/4] Remove positive-int --- src/Definition/Field.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/Definition/Field.php b/src/Definition/Field.php index 164eca2..a7a4390 100644 --- a/src/Definition/Field.php +++ b/src/Definition/Field.php @@ -33,9 +33,6 @@ final class Field */ private array|string|null $typecast = null; - /** - * @var positive-int|null - */ private ?int $generated = null; private bool $referenced = false; @@ -145,7 +142,7 @@ public function getTypecast(): array|string|null } /** - * @param positive-int|null $type. Generating type {@see SchemaInterface::GENERATED_*} constants. + * @param int|null $type Generating type {@see SchemaInterface::GENERATED_*} constants. */ public function setGenerated(int|null $type): self { @@ -154,9 +151,6 @@ public function setGenerated(int|null $type): self return $this; } - /** - * @return positive-int|null - */ public function getGenerated(): ?int { return $this->generated; From 6ec2148f579f88c8d0a73dd08320db6a583fc5b4 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Thu, 8 Feb 2024 18:16:39 +0200 Subject: [PATCH 4/4] Change constant names --- src/Definition/Field.php | 4 ++-- tests/Schema/CompilerTest.php | 15 ++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Definition/Field.php b/src/Definition/Field.php index a7a4390..cf73391 100644 --- a/src/Definition/Field.php +++ b/src/Definition/Field.php @@ -4,7 +4,7 @@ namespace Cycle\Schema\Definition; -use Cycle\ORM\SchemaInterface; +use Cycle\ORM\Schema\GeneratedField; use Cycle\Schema\Definition\Map\OptionMap; use Cycle\Schema\Exception\FieldException; @@ -142,7 +142,7 @@ public function getTypecast(): array|string|null } /** - * @param int|null $type Generating type {@see SchemaInterface::GENERATED_*} constants. + * @param int|null $type Generating type {@see GeneratedField*} constants. */ public function setGenerated(int|null $type): self { diff --git a/tests/Schema/CompilerTest.php b/tests/Schema/CompilerTest.php index 167ba03..c55a815 100644 --- a/tests/Schema/CompilerTest.php +++ b/tests/Schema/CompilerTest.php @@ -6,6 +6,7 @@ use Cycle\Database\DatabaseProviderInterface; use Cycle\ORM\Parser\Typecast; +use Cycle\ORM\Schema\GeneratedField; use Cycle\ORM\SchemaInterface; use Cycle\Schema\Compiler; use Cycle\Schema\Definition\Entity; @@ -18,7 +19,7 @@ use Cycle\Schema\Tests\Fixtures\Typecaster; use PHPUnit\Framework\TestCase; -class CompilerTest extends TestCase +final class CompilerTest extends TestCase { public function testWrongGeneratorShouldThrowAnException(): void { @@ -97,21 +98,21 @@ public function testRenderGeneratedFields(): void (new Field()) ->setType('datetime') ->setColumn('created_at') - ->setGenerated(SchemaInterface::GENERATED_PHP_INSERT) + ->setGenerated(GeneratedField::BEFORE_INSERT) ); $entity->getFields()->set( 'updatedAt', (new Field()) ->setType('datetime') ->setColumn('created_at') - ->setGenerated(SchemaInterface::GENERATED_PHP_INSERT | SchemaInterface::GENERATED_PHP_UPDATE) + ->setGenerated(GeneratedField::BEFORE_INSERT | GeneratedField::BEFORE_UPDATE) ); $entity->getFields()->set( 'sequence', (new Field()) ->setType('serial') ->setColumn('some_sequence') - ->setGenerated(SchemaInterface::GENERATED_DB) + ->setGenerated(GeneratedField::ON_INSERT) ); $r = new Registry($this->createMock(DatabaseProviderInterface::class)); @@ -120,9 +121,9 @@ public function testRenderGeneratedFields(): void $schema = (new Compiler())->compile($r); $this->assertSame([ - 'createdAt' => SchemaInterface::GENERATED_PHP_INSERT, - 'updatedAt' => SchemaInterface::GENERATED_PHP_INSERT | SchemaInterface::GENERATED_PHP_UPDATE, - 'sequence' => SchemaInterface::GENERATED_DB, + 'createdAt' => GeneratedField::BEFORE_INSERT, + 'updatedAt' => GeneratedField::BEFORE_INSERT | GeneratedField::BEFORE_UPDATE, + 'sequence' => GeneratedField::ON_INSERT, ], $schema['author'][SchemaInterface::GENERATED_FIELDS]); }