Skip to content

Commit

Permalink
feat: Migrate Node::status to NodeStatus enum, reduced int to smallin…
Browse files Browse the repository at this point in the history
…t when possible
  • Loading branch information
roadiz-ci committed Dec 4, 2024
1 parent 88e07f1 commit 29b53a5
Show file tree
Hide file tree
Showing 29 changed files with 287 additions and 297 deletions.
41 changes: 41 additions & 0 deletions migrations/Version20241204010215.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20241204010215 extends AbstractMigration
{
public function getDescription(): string
{
return 'Reduce type column size to SMALLINT';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE custom_form_fields CHANGE type type SMALLINT DEFAULT 0 NOT NULL');
$this->addSql('ALTER TABLE documents CHANGE imageWidth imageWidth SMALLINT DEFAULT 0 NOT NULL, CHANGE imageHeight imageHeight SMALLINT DEFAULT 0 NOT NULL');
$this->addSql('ALTER TABLE node_type_fields CHANGE type type SMALLINT DEFAULT 0 NOT NULL, CHANGE min_length min_length SMALLINT DEFAULT NULL, CHANGE max_length max_length SMALLINT DEFAULT NULL, CHANGE serialization_max_depth serialization_max_depth SMALLINT DEFAULT NULL');
$this->addSql('ALTER TABLE nodes CHANGE status status SMALLINT DEFAULT 10 NOT NULL');
$this->addSql('ALTER TABLE redirections CHANGE type type SMALLINT NOT NULL');
$this->addSql('ALTER TABLE settings CHANGE type type SMALLINT NOT NULL');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE custom_form_fields CHANGE type type INT NOT NULL');
$this->addSql('ALTER TABLE documents CHANGE imageWidth imageWidth INT DEFAULT 0 NOT NULL, CHANGE imageHeight imageHeight INT DEFAULT 0 NOT NULL');
$this->addSql('ALTER TABLE node_type_fields CHANGE serialization_max_depth serialization_max_depth INT DEFAULT NULL, CHANGE min_length min_length INT DEFAULT NULL, CHANGE max_length max_length INT DEFAULT NULL, CHANGE type type INT NOT NULL');
$this->addSql('ALTER TABLE nodes CHANGE status status INT NOT NULL');
$this->addSql('ALTER TABLE redirections CHANGE type type INT NOT NULL');
$this->addSql('ALTER TABLE settings CHANGE type type INT NOT NULL');
}
}
2 changes: 2 additions & 0 deletions src/Api/Controller/NodesSourcesSearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace RZ\Roadiz\CoreBundle\Api\Controller;

use Doctrine\Persistence\ManagerRegistry;
use RZ\Roadiz\CoreBundle\Api\ListManager\InvalidSearchQueryException;
use RZ\Roadiz\CoreBundle\Api\ListManager\SolrPaginator;
use RZ\Roadiz\CoreBundle\Api\ListManager\SolrSearchListManager;
use RZ\Roadiz\CoreBundle\Preview\PreviewResolverInterface;
Expand All @@ -13,6 +14,7 @@
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;

class NodesSourcesSearchController extends AbstractController
Expand Down
9 changes: 5 additions & 4 deletions src/Api/Extension/AttributeValueQueryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
use Doctrine\ORM\QueryBuilder;
use RZ\Roadiz\CoreBundle\Entity\AttributeValue;
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Enum\NodeStatus;
use RZ\Roadiz\CoreBundle\Preview\PreviewResolverInterface;

final class AttributeValueQueryExtension implements QueryItemExtensionInterface, QueryCollectionExtensionInterface
final readonly class AttributeValueQueryExtension implements QueryItemExtensionInterface, QueryCollectionExtensionInterface
{
public function __construct(
private readonly PreviewResolverInterface $previewResolver,
private PreviewResolverInterface $previewResolver,
) {
}

Expand Down Expand Up @@ -69,14 +70,14 @@ private function apply(
if ($this->previewResolver->isPreview()) {
$queryBuilder
->andWhere($queryBuilder->expr()->lte($joinAlias.'.status', ':status'))
->setParameter(':status', Node::PUBLISHED);
->setParameter(':status', NodeStatus::PUBLISHED);

return;
}

$queryBuilder
->andWhere($queryBuilder->expr()->eq($joinAlias.'.status', ':status'))
->setParameter(':status', Node::PUBLISHED);
->setParameter(':status', NodeStatus::PUBLISHED);

return;
}
Expand Down
9 changes: 5 additions & 4 deletions src/Api/Extension/NodeQueryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Enum\NodeStatus;
use RZ\Roadiz\CoreBundle\Preview\PreviewResolverInterface;

final class NodeQueryExtension implements QueryItemExtensionInterface, QueryCollectionExtensionInterface
final readonly class NodeQueryExtension implements QueryItemExtensionInterface, QueryCollectionExtensionInterface
{
public function __construct(
private readonly PreviewResolverInterface $previewResolver,
private PreviewResolverInterface $previewResolver,
) {
}

Expand All @@ -44,7 +45,7 @@ private function apply(
if ($this->previewResolver->isPreview()) {
$queryBuilder
->andWhere($queryBuilder->expr()->lte('o.status', ':status'))
->setParameter(':status', Node::PUBLISHED);
->setParameter(':status', NodeStatus::PUBLISHED);

return;
}
Expand All @@ -60,7 +61,7 @@ private function apply(
->andWhere($queryBuilder->expr()->lte($alias.'.publishedAt', ':lte_published_at'))
->andWhere($queryBuilder->expr()->eq('o.status', ':status'))
->setParameter(':lte_published_at', new \DateTime())
->setParameter(':status', Node::PUBLISHED);
->setParameter(':status', NodeStatus::PUBLISHED);

return;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Api/Extension/NodesSourcesQueryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
use ApiPlatform\Metadata\Operation;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
use RZ\Roadiz\CoreBundle\Enum\NodeStatus;
use RZ\Roadiz\CoreBundle\Preview\PreviewResolverInterface;

final class NodesSourcesQueryExtension implements QueryItemExtensionInterface, QueryCollectionExtensionInterface
final readonly class NodesSourcesQueryExtension implements QueryItemExtensionInterface, QueryCollectionExtensionInterface
{
public function __construct(
private readonly PreviewResolverInterface $previewResolver,
private readonly string $generatedEntityNamespacePattern = '#^App\\\GeneratedEntity\\\NS(?:[a-zA-Z]+)$#',
private PreviewResolverInterface $previewResolver,
private string $generatedEntityNamespacePattern = '#^App\\\GeneratedEntity\\\NS(?:[a-zA-Z]+)$#',
) {
}

Expand Down Expand Up @@ -71,7 +71,7 @@ private function apply(
);
$queryBuilder
->andWhere($queryBuilder->expr()->lte($alias.'.status', ':status'))
->setParameter(':status', Node::PUBLISHED);
->setParameter(':status', NodeStatus::PUBLISHED);

return;
}
Expand All @@ -87,7 +87,7 @@ private function apply(
->andWhere($queryBuilder->expr()->lte('o.publishedAt', ':lte_published_at'))
->andWhere($queryBuilder->expr()->eq($alias.'.status', ':status'))
->setParameter(':lte_published_at', new \DateTime())
->setParameter(':status', Node::PUBLISHED);
->setParameter(':status', NodeStatus::PUBLISHED);

return;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Api/Extension/NodesTagsQueryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\Operation;
use Doctrine\ORM\QueryBuilder;
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Entity\Tag;
use RZ\Roadiz\CoreBundle\Enum\NodeStatus;
use RZ\Roadiz\CoreBundle\Preview\PreviewResolverInterface;

final class NodesTagsQueryExtension implements QueryItemExtensionInterface, QueryCollectionExtensionInterface
Expand Down Expand Up @@ -74,14 +74,14 @@ private function apply(
if ($this->previewResolver->isPreview()) {
$queryBuilder
->andWhere($queryBuilder->expr()->lte($existingNodeJoin->getAlias().'.status', ':status'))
->setParameter(':status', Node::PUBLISHED);
->setParameter(':status', NodeStatus::PUBLISHED);

return;
}

$queryBuilder
->andWhere($queryBuilder->expr()->eq($existingNodeJoin->getAlias().'.status', ':status'))
->setParameter(':status', Node::PUBLISHED);
->setParameter(':status', NodeStatus::PUBLISHED);

return;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Api/ListManager/SolrSearchListManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
use RZ\Roadiz\CoreBundle\SearchEngine\SearchResultsInterface;
use Symfony\Component\DependencyInjection\Attribute\Exclude;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

#[Exclude]
final class SolrSearchListManager extends AbstractEntityListManager
{
private ?SearchResultsInterface $searchResults;
private ?SearchResultsInterface $searchResults = null;
private ?string $query = null;

public function __construct(
Expand All @@ -34,7 +35,7 @@ public function handle(bool $disabled = false): void
$this->handleRequestQuery($disabled);

if (null === $this->query) {
throw new \InvalidArgumentException('Cannot handle a NULL query.');
throw new BadRequestHttpException('Search param is required.');
}
/*
* Query must be longer than 3 chars or Solr might crash
Expand Down
7 changes: 5 additions & 2 deletions src/Console/NodesEmptyTrashCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\EntityHandler\HandlerFactory;
use RZ\Roadiz\CoreBundle\EntityHandler\NodeHandler;
use RZ\Roadiz\CoreBundle\Enum\NodeStatus;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -40,7 +41,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$em = $this->managerRegistry->getManagerForClass(Node::class);
$countQb = $this->createNodeQueryBuilder();
$countQuery = $countQb->select($countQb->expr()->count('n'))
->andWhere($countQb->expr()->eq('n.status', Node::DELETED))
->andWhere($countQb->expr()->eq('n.status', ':status'))
->setParameter('status', NodeStatus::DELETED)
->getQuery();
$emptiedCount = $countQuery->getSingleScalarResult();
if (0 == $emptiedCount) {
Expand All @@ -64,7 +66,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$qb = $this->createNodeQueryBuilder();
$q = $qb->select('n')
->andWhere($countQb->expr()->eq('n.status', Node::DELETED))
->andWhere($countQb->expr()->eq('n.status', ':status'))
->setParameter('status', NodeStatus::DELETED)
->getQuery();

foreach ($q->toIterable() as $row) {
Expand Down
7 changes: 4 additions & 3 deletions src/Entity/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use RZ\Roadiz\Core\AbstractEntities\AbstractDateTimed;
Expand Down Expand Up @@ -267,7 +268,7 @@ class Document extends AbstractDateTimed implements AdvancedDocumentInterface, H
#[Serializer\Groups(['document', 'document_private', 'nodes_sources', 'tag', 'attribute'])]
#[Serializer\Type('bool')]
private bool $private = false;
#[ORM\Column(name: 'imageWidth', type: 'integer', nullable: false, options: ['default' => 0])]
#[ORM\Column(name: 'imageWidth', type: Types::SMALLINT, nullable: false, options: ['default' => 0])]
#[SymfonySerializer\Groups(['document', 'document_display', 'nodes_sources', 'tag', 'attribute'])]
#[Serializer\Groups(['document', 'document_display', 'nodes_sources', 'tag', 'attribute'])]
#[Serializer\Type('int')]
Expand All @@ -276,7 +277,7 @@ class Document extends AbstractDateTimed implements AdvancedDocumentInterface, H
example: '1280',
)]
private int $imageWidth = 0;
#[ORM\Column(name: 'imageHeight', type: 'integer', nullable: false, options: ['default' => 0])]
#[ORM\Column(name: 'imageHeight', type: Types::SMALLINT, nullable: false, options: ['default' => 0])]
#[SymfonySerializer\Groups(['document', 'document_display', 'nodes_sources', 'tag', 'attribute'])]
#[Serializer\Groups(['document', 'document_display', 'nodes_sources', 'tag', 'attribute'])]
#[Serializer\Type('int')]
Expand All @@ -285,7 +286,7 @@ class Document extends AbstractDateTimed implements AdvancedDocumentInterface, H
example: '800',
)]
private int $imageHeight = 0;
#[ORM\Column(name: 'duration', type: 'integer', nullable: false, options: ['default' => 0])]
#[ORM\Column(name: 'duration', type: Types::INTEGER, nullable: false, options: ['default' => 0])]
#[SymfonySerializer\Groups(['document', 'document_display', 'nodes_sources', 'tag', 'attribute'])]
#[Serializer\Groups(['document', 'document_display', 'nodes_sources', 'tag', 'attribute'])]
#[Serializer\Type('int')]
Expand Down
Loading

0 comments on commit 29b53a5

Please sign in to comment.