From e839d216adbe3c9ce1669dcaf361bd32ebe731d4 Mon Sep 17 00:00:00 2001 From: Robin Krahnen Date: Wed, 20 Apr 2022 17:36:31 +0200 Subject: [PATCH] revised code --- Classes/Domain/Model/SequenceEntry.php | 35 +++------------------- Classes/Service/SequenceGenerator.php | 3 +- Migrations/Mysql/Version20210922110814.php | 6 ++-- Tests/Functional/SequenceTest.php | 10 +++---- composer.json | 2 +- 5 files changed, 14 insertions(+), 42 deletions(-) diff --git a/Classes/Domain/Model/SequenceEntry.php b/Classes/Domain/Model/SequenceEntry.php index ee164a9..ee8c39f 100644 --- a/Classes/Domain/Model/SequenceEntry.php +++ b/Classes/Domain/Model/SequenceEntry.php @@ -8,48 +8,21 @@ use Doctrine\ORM\Mapping as ORM; use Neos\Flow\Annotations as Flow; /** + * This class is only here to set up the table. We never create an instance of this class. + * * @Flow\Entity - * @ORM\Table(indexes={ - * @ORM\Index(columns={"type"}) - * }, uniqueConstraints={ - * @ORM\UniqueConstraint(columns={"type", "number"}) - * }) */ class SequenceEntry { /** + * @ORM\Id * @var string */ protected string $type; /** + * @ORM\Id * @var int */ protected int $number; - - /** - * @param string $type - * @param int $number - */ - public function __construct(string $type, int $number) - { - $this->type = $type; - $this->number = $number; - } - - /** - * @return string - */ - public function getType(): string - { - return $this->type; - } - - /** - * @return int - */ - public function getNumber(): int - { - return $this->number; - } } diff --git a/Classes/Service/SequenceGenerator.php b/Classes/Service/SequenceGenerator.php index 45bbfac..a9ed1c7 100644 --- a/Classes/Service/SequenceGenerator.php +++ b/Classes/Service/SequenceGenerator.php @@ -10,7 +10,6 @@ use Doctrine\DBAL\Driver\Exception as DoctrineDBALDriverException; use Doctrine\DBAL\Exception as DoctrineDBALException; use Doctrine\ORM\EntityManagerInterface; use Neos\Flow\Annotations as Flow; -use Neos\Flow\Utility\Algorithms; use Neos\Utility\TypeHandling; use Psr\Log\LoggerInterface; @@ -67,7 +66,7 @@ class SequenceGenerator try { $this->entityManager->getConnection()->insert( $this->entityManager->getClassMetadata(SequenceEntry::class)->getTableName(), - ['persistence_object_identifier' => Algorithms::generateUUID(), 'number' => $number, 'type' => $type] + ['type' => $type, 'number' => $number] ); return true; diff --git a/Migrations/Mysql/Version20210922110814.php b/Migrations/Mysql/Version20210922110814.php index e74c465..7623464 100644 --- a/Migrations/Mysql/Version20210922110814.php +++ b/Migrations/Mysql/Version20210922110814.php @@ -18,8 +18,8 @@ class Version20210922110814 extends AbstractMigration { $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on "mysql".'); - $this->addSql('CREATE TABLE digicomp_sequence_domain_model_sequenceentry (persistence_object_identifier VARCHAR(40) NOT NULL, type VARCHAR(255) NOT NULL, number INT NOT NULL, INDEX IDX_F6ADC8568CDE5729 (type), UNIQUE INDEX UNIQ_F6ADC8568CDE572996901F54 (type, number), PRIMARY KEY(persistence_object_identifier))'); - $this->addSql('INSERT INTO digicomp_sequence_domain_model_sequenceentry (persistence_object_identifier, type, number) SELECT UUID(), i.type, i.number FROM digicomp_sequence_domain_model_insert AS i'); + $this->addSql('CREATE TABLE digicomp_sequence_domain_model_sequenceentry (type VARCHAR(255) NOT NULL, number INT NOT NULL, PRIMARY KEY(type, number))'); + $this->addSql('INSERT INTO digicomp_sequence_domain_model_sequenceentry (type, number) SELECT i.type, MAX(i.number) FROM digicomp_sequence_domain_model_insert AS i GROUP BY i.type'); $this->addSql('DROP TABLE digicomp_sequence_domain_model_insert'); } @@ -33,7 +33,7 @@ class Version20210922110814 extends AbstractMigration $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on "mysql".'); $this->addSql('CREATE TABLE digicomp_sequence_domain_model_insert (number INT NOT NULL, type VARCHAR(255) NOT NULL, INDEX type_idx (type), PRIMARY KEY(number, type))'); - $this->addSql('INSERT INTO digicomp_sequence_domain_model_insert (number, type) SELECT se.number, se.type FROM digicomp_sequence_domain_model_sequenceentry AS se'); + $this->addSql('INSERT INTO digicomp_sequence_domain_model_insert (number, type) SELECT MAX(se.number), se.type FROM digicomp_sequence_domain_model_sequenceentry AS se GROUP BY se.type'); $this->addSql('DROP TABLE digicomp_sequence_domain_model_sequenceentry'); } } diff --git a/Tests/Functional/SequenceTest.php b/Tests/Functional/SequenceTest.php index 1a5cd0c..542095e 100644 --- a/Tests/Functional/SequenceTest.php +++ b/Tests/Functional/SequenceTest.php @@ -27,8 +27,8 @@ class SequenceTest extends FunctionalTestCase { $sequenceGenerator = $this->objectManager->get(SequenceGenerator::class); - $this->assertEquals(0, $sequenceGenerator->getLastNumberFor($sequenceGenerator)); - $this->assertEquals(1, $sequenceGenerator->getNextNumberFor($sequenceGenerator)); + self::assertEquals(0, $sequenceGenerator->getLastNumberFor($sequenceGenerator)); + self::assertEquals(1, $sequenceGenerator->getNextNumberFor($sequenceGenerator)); $pIds = []; for ($i = 0; $i < 10; $i++) { @@ -49,7 +49,7 @@ class SequenceTest extends FunctionalTestCase \pcntl_waitpid($pId, $status); } - $this->assertEquals(101, $sequenceGenerator->getLastNumberFor($sequenceGenerator)); + self::assertEquals(101, $sequenceGenerator->getLastNumberFor($sequenceGenerator)); } /** @@ -63,7 +63,7 @@ class SequenceTest extends FunctionalTestCase $sequenceGenerator = $this->objectManager->get(SequenceGenerator::class); $sequenceGenerator->setLastNumberFor($sequenceGenerator, 100); - $this->assertEquals(100, $sequenceGenerator->getLastNumberFor($sequenceGenerator)); - $this->assertEquals(0, $sequenceGenerator->getLastNumberFor('otherSequence')); + self::assertEquals(100, $sequenceGenerator->getLastNumberFor($sequenceGenerator)); + self::assertEquals(0, $sequenceGenerator->getLastNumberFor('otherSequence')); } } diff --git a/composer.json b/composer.json index 2d12076..d955c14 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ }, "require-dev": { "ext-pcntl": "*", - "phpunit/phpunit": "3.7.*" + "phpunit/phpunit": "~8.5" }, "autoload": { "psr-4": {