revised code

This commit is contained in:
Robin Krahnen 2022-04-20 17:36:31 +02:00
parent 8396fa0db7
commit e839d216ad
5 changed files with 14 additions and 42 deletions

View file

@ -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;
}
}

View file

@ -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;

View file

@ -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');
}
}

View file

@ -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'));
}
}

View file

@ -9,7 +9,7 @@
},
"require-dev": {
"ext-pcntl": "*",
"phpunit/phpunit": "3.7.*"
"phpunit/phpunit": "~8.5"
},
"autoload": {
"psr-4": {