Skip to content

Commit

Permalink
feat: Reduce NodeTypeField name to maximum 50 characters long.
Browse files Browse the repository at this point in the history
  • Loading branch information
roadiz-ci committed Mar 18, 2024
1 parent 29954f4 commit 77a1bef
Show file tree
Hide file tree
Showing 24 changed files with 419 additions and 272 deletions.
34 changes: 34 additions & 0 deletions migrations/Version20240318184555.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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 Version20240318184555 extends AbstractMigration
{
public function getDescription(): string
{
return 'Create new fields to store node-type fields names in nodes_custom_forms, nodes_sources_documents and nodes_to_nodes tables.';
}

public function up(Schema $schema): void
{
/*
* FIRST CREATE NEW FIELDS
*/
$this->addSql('ALTER TABLE nodes_custom_forms ADD field_name VARCHAR(250)');
$this->addSql('ALTER TABLE nodes_sources_documents ADD field_name VARCHAR(250)');
$this->addSql('ALTER TABLE nodes_to_nodes ADD field_name VARCHAR(250)');
}

public function down(Schema $schema): void
{
$this->throwIrreversibleMigrationException('Cannot convert node-type fields name back to their identifiers');
}
}
68 changes: 68 additions & 0 deletions migrations/Version20240318184556.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?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 Version20240318184556 extends AbstractMigration
{
public function getDescription(): string
{
return 'Change node-type fields ID to their names in nodes_custom_forms, nodes_sources_documents and nodes_to_nodes tables.';
}

public function up(Schema $schema): void
{
/*
* Need to populate new field_name with node-type field names
*/
$this->connection->beginTransaction();
$this->connection->executeStatement('UPDATE nodes_custom_forms SET field_name = (SELECT name FROM node_type_fields WHERE id = node_type_field_id)');
$this->connection->executeStatement('UPDATE nodes_sources_documents SET field_name = (SELECT name FROM node_type_fields WHERE id = node_type_field_id)');
$this->connection->executeStatement('UPDATE nodes_to_nodes SET field_name = (SELECT name FROM node_type_fields WHERE id = node_type_field_id)');
$this->connection->commit();

$this->addSql('ALTER TABLE nodes_custom_forms DROP FOREIGN KEY FK_4D401A0C47705282');
$this->addSql('DROP INDEX IDX_4D401A0C47705282 ON nodes_custom_forms');
$this->addSql('DROP INDEX customform_node_field_position ON nodes_custom_forms');
$this->addSql('CREATE INDEX customform_node_field_position ON nodes_custom_forms (node_id, field_name, position)');
$this->addSql('ALTER TABLE nodes_sources_documents DROP FOREIGN KEY FK_1CD104F747705282');
$this->addSql('DROP INDEX IDX_1CD104F747705282 ON nodes_sources_documents');
$this->addSql('DROP INDEX nsdoc_field ON nodes_sources_documents');
$this->addSql('DROP INDEX nsdoc_field_position ON nodes_sources_documents');
$this->addSql('CREATE INDEX nsdoc_field ON nodes_sources_documents (ns_id, field_name)');
$this->addSql('CREATE INDEX nsdoc_field_position ON nodes_sources_documents (ns_id, field_name, position)');
$this->addSql('ALTER TABLE nodes_to_nodes DROP FOREIGN KEY FK_761F9A9147705282');
$this->addSql('DROP INDEX IDX_761F9A9147705282 ON nodes_to_nodes');
$this->addSql('DROP INDEX node_a_field ON nodes_to_nodes');
$this->addSql('DROP INDEX node_a_field_position ON nodes_to_nodes');
$this->addSql('DROP INDEX node_b_field ON nodes_to_nodes');
$this->addSql('DROP INDEX node_b_field_position ON nodes_to_nodes');
$this->addSql('CREATE INDEX node_a_field ON nodes_to_nodes (node_a_id, field_name)');
$this->addSql('CREATE INDEX node_a_field_position ON nodes_to_nodes (node_a_id, field_name, position)');
$this->addSql('CREATE INDEX node_b_field ON nodes_to_nodes (node_b_id, field_name)');
$this->addSql('CREATE INDEX node_b_field_position ON nodes_to_nodes (node_b_id, field_name, position)');

/*
* DESTRUCTIVE OPERATIONS
*/
$this->addSql('ALTER TABLE nodes_custom_forms CHANGE field_name field_name VARCHAR(250) NOT NULL');
$this->addSql('ALTER TABLE nodes_sources_documents CHANGE field_name field_name VARCHAR(250) NOT NULL');
$this->addSql('ALTER TABLE nodes_to_nodes CHANGE field_name field_name VARCHAR(250) NOT NULL');

$this->addSql('ALTER TABLE nodes_custom_forms DROP node_type_field_id');
$this->addSql('ALTER TABLE nodes_sources_documents DROP node_type_field_id');
$this->addSql('ALTER TABLE nodes_to_nodes DROP node_type_field_id');
}

public function down(Schema $schema): void
{
$this->throwIrreversibleMigrationException('Cannot convert node-type fields name back to their identifiers');
}
}
44 changes: 44 additions & 0 deletions migrations/Version20240318204224.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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 Version20240318204224 extends AbstractMigration
{
public function getDescription(): string
{
return 'Rename field_name length to 50 characters maximum.';
}

public function up(Schema $schema): void
{
$result = $this->connection->executeQuery('SELECT max(length(name)) FROM `node_type_fields`');
$maxLength = $result->fetchOne();

$this->skipIf(!is_numeric($maxLength), 'Cannot find node_type_fields name maximum length.');
$this->skipIf($maxLength >= 50, 'You have at least on node_type_field name that exceed 50 characters long.');

// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE node_type_fields CHANGE name name VARCHAR(50) NOT NULL');
$this->addSql('CREATE INDEX ntf_name ON node_type_fields (name)');
$this->addSql('ALTER TABLE nodes_custom_forms CHANGE field_name field_name VARCHAR(50) NOT NULL');
$this->addSql('ALTER TABLE nodes_sources_documents CHANGE field_name field_name VARCHAR(50) NOT NULL');
$this->addSql('ALTER TABLE nodes_to_nodes CHANGE field_name field_name VARCHAR(50) NOT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('DROP INDEX ntf_name ON node_type_fields');
$this->addSql('ALTER TABLE node_type_fields CHANGE name name VARCHAR(250) NOT NULL');
$this->addSql('ALTER TABLE nodes_custom_forms CHANGE field_name field_name VARCHAR(250) NOT NULL');
$this->addSql('ALTER TABLE nodes_sources_documents CHANGE field_name field_name VARCHAR(250) NOT NULL');
$this->addSql('ALTER TABLE nodes_to_nodes CHANGE field_name field_name VARCHAR(250) NOT NULL');
}
}
50 changes: 6 additions & 44 deletions src/Doctrine/ORM/Filter/ANodesFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ protected function getNodeJoinAlias(): string
return 'a_n';
}

/**
* @return string
*/
protected function getNodeFieldJoinAlias(): string
{
return 'a_n_f';
}

/**
* @param QueryBuilderBuildEvent $event
*/
Expand All @@ -72,24 +64,9 @@ public function onNodeQueryBuilderBuild(QueryBuilderBuildEvent $event): void
$this->getNodeJoinAlias()
);
}
if (str_contains($event->getProperty(), $this->getProperty() . '.field.')) {
if (
!$simpleQB->joinExists(
$simpleQB->getRootAlias(),
$this->getNodeFieldJoinAlias()
)
) {
$qb->innerJoin(
$this->getNodeJoinAlias() . '.field',
$this->getNodeFieldJoinAlias()
);
}
$prefix = $this->getNodeFieldJoinAlias() . '.';
$key = str_replace($this->getProperty() . '.field.', '', $event->getProperty());
} else {
$prefix = $this->getNodeJoinAlias() . '.';
$key = str_replace($this->getProperty() . '.', '', $event->getProperty());
}

$prefix = $this->getNodeJoinAlias() . '.';
$key = str_replace($this->getProperty() . '.', '', $event->getProperty());

$qb->andWhere($simpleQB->buildExpressionWithoutBinding($event->getValue(), $prefix, $key, $baseKey));
}
Expand Down Expand Up @@ -132,24 +109,9 @@ public function onNodesSourcesQueryBuilderBuild(QueryBuilderNodesSourcesBuildEve
$this->getNodeJoinAlias()
);
}
if (str_contains($event->getProperty(), 'node.' . $this->getProperty() . '.field.')) {
if (
!$simpleQB->joinExists(
$simpleQB->getRootAlias(),
$this->getNodeFieldJoinAlias()
)
) {
$qb->innerJoin(
$this->getNodeJoinAlias() . '.field',
$this->getNodeFieldJoinAlias()
);
}
$prefix = $this->getNodeFieldJoinAlias() . '.';
$key = str_replace('node.' . $this->getProperty() . '.field.', '', $event->getProperty());
} else {
$prefix = $this->getNodeJoinAlias() . '.';
$key = str_replace('node.' . $this->getProperty() . '.', '', $event->getProperty());
}

$prefix = $this->getNodeJoinAlias() . '.';
$key = str_replace('node.' . $this->getProperty() . '.', '', $event->getProperty());

$qb->andWhere($simpleQB->buildExpressionWithoutBinding($event->getValue(), $prefix, $key, $baseKey));
}
Expand Down
8 changes: 0 additions & 8 deletions src/Doctrine/ORM/Filter/BNodesFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,4 @@ protected function getNodeJoinAlias(): string
{
return 'b_n';
}

/**
* @return string
*/
protected function getNodeFieldJoinAlias(): string
{
return 'b_n_f';
}
}
44 changes: 44 additions & 0 deletions src/Entity/FieldAwareEntityTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use RZ\Roadiz\Contracts\NodeType\NodeTypeFieldInterface;
use Symfony\Component\Validator\Constraints as Assert;

trait FieldAwareEntityTrait
{
#[ORM\Column(name: 'field_name', length: 50, nullable: false)]
#[Assert\Length(max: 50)]
protected string $fieldName;

public function getFieldName(): string
{
return $this->fieldName;
}

public function setFieldName(string $fieldName): self
{
$this->fieldName = $fieldName;
return $this;
}

/**
* @deprecated Use setFieldName method instead
*/
public function setField(NodeTypeFieldInterface $field): self
{
$this->fieldName = $field->getName();
return $this;
}

protected function initializeFieldAwareEntityTrait(?NodeTypeFieldInterface $nodeTypeField = null): void
{
if (null === $nodeTypeField) {
return;
}
$this->fieldName = $nodeTypeField->getName();
}
}
13 changes: 7 additions & 6 deletions src/Entity/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Gedmo\Loggable\Loggable;
use Gedmo\Mapping\Annotation as Gedmo;
use JMS\Serializer\Annotation as Serializer;
use RZ\Roadiz\Contracts\NodeType\NodeTypeFieldInterface;
use RZ\Roadiz\Contracts\NodeType\NodeTypeInterface;
use RZ\Roadiz\Core\AbstractEntities\LeafInterface;
use RZ\Roadiz\Core\AbstractEntities\LeafTrait;
Expand Down Expand Up @@ -795,10 +796,10 @@ public function addNodeSources(NodesSources $ns): static
* @return Collection<int, NodesToNodes>
*/
#[SymfonySerializer\Ignore]
public function getBNodesByField(NodeTypeField $field): Collection
public function getBNodesByField(NodeTypeFieldInterface $field): Collection
{
$criteria = Criteria::create();
$criteria->andWhere(Criteria::expr()->eq('field', $field));
$criteria->andWhere(Criteria::expr()->eq('fieldName', $field->getName()));
$criteria->orderBy(['position' => 'ASC']);
return $this->getBNodes()->matching($criteria);
}
Expand Down Expand Up @@ -833,7 +834,7 @@ public function hasBNode(NodesToNodes $bNode): bool
return $this->getBNodes()->exists(function ($key, NodesToNodes $element) use ($bNode) {
return $bNode->getNodeB()->getId() !== null &&
$element->getNodeB()->getId() === $bNode->getNodeB()->getId() &&
$element->getField()->getId() === $bNode->getField()->getId();
$element->getFieldName() === $bNode->getFieldName();
});
}

Expand All @@ -850,10 +851,10 @@ public function addBNode(NodesToNodes $bNode): static
return $this;
}

public function clearBNodesForField(NodeTypeField $nodeTypeField): Node
public function clearBNodesForField(NodeTypeFieldInterface $field): Node
{
$toRemoveCollection = $this->getBNodes()->filter(function (NodesToNodes $element) use ($nodeTypeField) {
return $element->getField()->getId() === $nodeTypeField->getId();
$toRemoveCollection = $this->getBNodes()->filter(function (NodesToNodes $element) use ($field) {
return $element->getFieldName() === $field->getName();
});
/** @var NodesToNodes $toRemove */
foreach ($toRemoveCollection as $toRemove) {
Expand Down
5 changes: 3 additions & 2 deletions src/Entity/NodeTypeField.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
ORM\Index(columns: ["group_name"]),
ORM\Index(columns: ["group_name_canonical"]),
ORM\Index(columns: ["type"]),
ORM\Index(columns: ["name"], name: 'ntf_name'),
ORM\Index(columns: ["universal"]),
ORM\Index(columns: ["node_type_id", "position"], name: "ntf_type_position"),
ORM\UniqueConstraint(columns: ["name", "node_type_id"]),
Expand All @@ -39,11 +40,11 @@
class NodeTypeField extends AbstractField implements NodeTypeFieldInterface, SerializableInterface
{
#[
ORM\Column(type: "string", length: 250),
ORM\Column(type: "string", length: 50),
Serializer\Expose,
Serializer\Groups(["node_type", "setting"]),
SymfonySerializer\Groups(["node_type", "setting"]),
Assert\Length(max: 250),
Assert\Length(max: 50),
Serializer\Type("string"),
RoadizAssert\NonSqlReservedWord(),
RoadizAssert\SimpleLatinString()
Expand Down
Loading

0 comments on commit 77a1bef

Please sign in to comment.