Skip to content

Commit

Permalink
perf: Made all foreign key columns not nullable for index performance
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Mar 5, 2024
1 parent dba0237 commit fd90805
Show file tree
Hide file tree
Showing 34 changed files with 296 additions and 408 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
"symfony/property-info": "6.4.*",
"symfony/proxy-manager-bridge": "6.4.*",
"symfony/rate-limiter": "6.4.*",
"symfony/redis-messenger": "6.4.*",
"symfony/requirements-checker": "^2.0",
"symfony/routing": "6.4.*",
"symfony/runtime": "6.4.*",
Expand Down
31 changes: 31 additions & 0 deletions lib/RoadizCoreBundle/migrations/Version20240305133641.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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 Version20240305133641 extends AbstractMigration
{
public function getDescription(): string
{
return 'Set nodes_sources node_id and translation_id columns not-nullable';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE nodes_sources CHANGE node_id node_id INT NOT NULL, CHANGE translation_id translation_id INT NOT NULL');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE nodes_sources CHANGE node_id node_id INT DEFAULT NULL, CHANGE translation_id translation_id INT DEFAULT NULL');
}
}
37 changes: 37 additions & 0 deletions lib/RoadizCoreBundle/migrations/Version20240305134734.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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 Version20240305134734 extends AbstractMigration
{
public function getDescription(): string
{
return 'Set translations tables foreign key columns not-nullable';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE documents_translations CHANGE translation_id translation_id INT NOT NULL, CHANGE document_id document_id INT NOT NULL');
$this->addSql('ALTER TABLE folders_translations CHANGE folder_id folder_id INT NOT NULL, CHANGE translation_id translation_id INT NOT NULL');
$this->addSql('ALTER TABLE tags_translations CHANGE tag_id tag_id INT NOT NULL, CHANGE translation_id translation_id INT NOT NULL');
$this->addSql('ALTER TABLE tags_translations_documents CHANGE tag_translation_id tag_translation_id INT NOT NULL, CHANGE document_id document_id INT NOT NULL');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE documents_translations CHANGE translation_id translation_id INT DEFAULT NULL, CHANGE document_id document_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE folders_translations CHANGE folder_id folder_id INT DEFAULT NULL, CHANGE translation_id translation_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE tags_translations CHANGE tag_id tag_id INT DEFAULT NULL, CHANGE translation_id translation_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE tags_translations_documents CHANGE tag_translation_id tag_translation_id INT DEFAULT NULL, CHANGE document_id document_id INT DEFAULT NULL');
}
}
62 changes: 62 additions & 0 deletions lib/RoadizCoreBundle/migrations/Version20240305142243.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?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 Version20240305142243 extends AbstractMigration
{
public function getDescription(): string
{
return 'Set composite tables foreign key columns not-nullable';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE attribute_group_translations CHANGE attribute_group_id attribute_group_id INT NOT NULL, CHANGE translation_id translation_id INT NOT NULL');
$this->addSql('ALTER TABLE attribute_translations CHANGE attribute_id attribute_id INT NOT NULL, CHANGE translation_id translation_id INT NOT NULL');
$this->addSql('ALTER TABLE attribute_value_translations CHANGE translation_id translation_id INT NOT NULL, CHANGE attribute_value attribute_value INT NOT NULL');
$this->addSql('ALTER TABLE attribute_values CHANGE attribute_id attribute_id INT NOT NULL, CHANGE node_id node_id INT NOT NULL');
$this->addSql('ALTER TABLE attributes_documents CHANGE attribute_id attribute_id INT NOT NULL, CHANGE document_id document_id INT NOT NULL');
$this->addSql('ALTER TABLE custom_form_answers CHANGE custom_form_id custom_form_id INT NOT NULL');
$this->addSql('ALTER TABLE custom_form_field_attributes CHANGE custom_form_answer_id custom_form_answer_id INT NOT NULL, CHANGE custom_form_field_id custom_form_field_id INT NOT NULL');
$this->addSql('ALTER TABLE custom_form_fields CHANGE custom_form_id custom_form_id INT NOT NULL');

// Remove all node_type_fields where node_type_id is null before changing it to not-nullable
$this->addSql('DELETE FROM node_type_fields WHERE node_type_id IS NULL');
$this->addSql('ALTER TABLE node_type_fields CHANGE node_type_id node_type_id INT NOT NULL');

// Remove all nodes where nodeType_id is null before changing it to not-nullable
$this->addSql('DELETE FROM nodes WHERE nodeType_id IS NULL');
$this->addSql('ALTER TABLE nodes CHANGE nodeType_id nodeType_id INT NOT NULL');

// Remove all url_aliases where ns_id is null before changing it to not-nullable
$this->addSql('DELETE FROM url_aliases WHERE ns_id IS NULL');
$this->addSql('ALTER TABLE url_aliases DROP FOREIGN KEY FK_E261ED65AA2D61');
$this->addSql('ALTER TABLE url_aliases CHANGE ns_id ns_id INT NOT NULL');
$this->addSql('ALTER TABLE url_aliases ADD CONSTRAINT FK_E261ED65AA2D61 FOREIGN KEY (ns_id) REFERENCES nodes_sources (id) ON DELETE CASCADE');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE attribute_group_translations CHANGE translation_id translation_id INT DEFAULT NULL, CHANGE attribute_group_id attribute_group_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE attribute_translations CHANGE translation_id translation_id INT DEFAULT NULL, CHANGE attribute_id attribute_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE attribute_value_translations CHANGE translation_id translation_id INT DEFAULT NULL, CHANGE attribute_value attribute_value INT DEFAULT NULL');
$this->addSql('ALTER TABLE attribute_values CHANGE node_id node_id INT DEFAULT NULL, CHANGE attribute_id attribute_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE attributes_documents CHANGE attribute_id attribute_id INT DEFAULT NULL, CHANGE document_id document_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE custom_form_answers CHANGE custom_form_id custom_form_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE custom_form_field_attributes CHANGE custom_form_answer_id custom_form_answer_id INT DEFAULT NULL, CHANGE custom_form_field_id custom_form_field_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE custom_form_fields CHANGE custom_form_id custom_form_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE node_type_fields CHANGE node_type_id node_type_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE nodes CHANGE nodeType_id nodeType_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE url_aliases DROP FOREIGN KEY FK_E261ED65AA2D61');
$this->addSql('ALTER TABLE url_aliases CHANGE ns_id ns_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE url_aliases ADD CONSTRAINT FK_E261ED65AA2D61 FOREIGN KEY (ns_id) REFERENCES nodes_sources (id) ON UPDATE NO ACTION ON DELETE NO ACTION');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public function create(?PersistableInterface $entity): ?BreadcrumbsInterface
}

if (
null === $entity->getNode()->getNodeType() ||
!$entity->isReachable()
) {
return null;
Expand Down
44 changes: 9 additions & 35 deletions lib/RoadizCoreBundle/src/Entity/AttributeDocuments.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ class AttributeDocuments extends AbstractPositioned
ORM\JoinColumn(
name: "attribute_id",
referencedColumnName: "id",
nullable: false,
onDelete: "CASCADE"
),
Serializer\Exclude(),
SymfonySerializer\Ignore()
]
protected ?Attribute $attribute = null;
protected Attribute $attribute;

#[
ORM\ManyToOne(
Expand All @@ -49,72 +50,45 @@ class AttributeDocuments extends AbstractPositioned
ORM\JoinColumn(
name: "document_id",
referencedColumnName: "id",
nullable: false,
onDelete: "CASCADE"
),
Serializer\Groups(["attribute"]),
SymfonySerializer\Groups(["attribute"]),
Serializer\Type(Document::class)
]
protected ?Document $document = null;
protected Document $document;

/**
* @param Attribute|null $attribute
* @param Document|null $document
*/
public function __construct(Attribute $attribute = null, Document $document = null)
public function __construct(Attribute $attribute, Document $document)
{
$this->document = $document;
$this->attribute = $attribute;
}

/**
*
*/
public function __clone()
{
if ($this->id) {
$this->id = null;
$this->attribute = null;
}
}

/**
* Gets the value of document.
*
* @return Document|null
*/
public function getDocument(): ?Document
public function getDocument(): Document
{
return $this->document;
}

/**
* Sets the value of document.
*
* @param Document|null $document the document
*
* @return AttributeDocuments
*/
public function setDocument(?Document $document): AttributeDocuments
public function setDocument(Document $document): AttributeDocuments
{
$this->document = $document;

return $this;
}

/**
* @return Attribute|null
*/
public function getAttribute(): ?Attribute
public function getAttribute(): Attribute
{
return $this->attribute;
}

/**
* @param Attribute|null $attribute
* @return AttributeDocuments
*/
public function setAttribute(?Attribute $attribute): AttributeDocuments
public function setAttribute(Attribute $attribute): AttributeDocuments
{
$this->attribute = $attribute;
return $this;
Expand Down
24 changes: 5 additions & 19 deletions lib/RoadizCoreBundle/src/Entity/AttributeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,9 @@ class AttributeValue extends AbstractPositioned implements AttributeValueInterfa
{
use AttributeValueTrait;

/**
* @var Node|null
*/
#[
ORM\ManyToOne(targetEntity: Node::class, inversedBy: "attributeValues"),
ORM\JoinColumn(name: "node_id", onDelete: "CASCADE"),
ORM\JoinColumn(name: "node_id", referencedColumnName: "id", nullable: false, onDelete: "CASCADE"),
Serializer\Groups(["attribute_node"]),
SymfonySerializer\Groups(["attribute_node"]),
SymfonySerializer\MaxDepth(1),
Expand All @@ -55,7 +52,7 @@ class AttributeValue extends AbstractPositioned implements AttributeValueInterfa
"node.visible"
])
]
protected ?Node $node = null;
protected Node $node;

#[ORM\ManyToOne(targetEntity: Realm::class)]
#[ORM\JoinColumn(
Expand Down Expand Up @@ -85,10 +82,7 @@ public function getPosition(): float
return $this->position;
}

/**
* @inheritDoc
*/
public function getAttributable(): ?AttributableInterface
public function getAttributable(): Node
{
return $this->node;
}
Expand All @@ -105,20 +99,12 @@ public function setAttributable(?AttributableInterface $attributable)
throw new \InvalidArgumentException('Attributable have to be an instance of Node.');
}

/**
* @return Node|null
*/
public function getNode(): ?Node
public function getNode(): Node
{
return $this->node;
}

/**
* @param Node|null $node
*
* @return AttributeValue
*/
public function setNode(?Node $node): AttributeValue
public function setNode(Node $node): AttributeValue
{
$this->node = $node;

Expand Down
1 change: 0 additions & 1 deletion lib/RoadizCoreBundle/src/Entity/CustomForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ public function removeField(CustomFormField $field): CustomForm
{
if ($this->getFields()->contains($field)) {
$this->getFields()->removeElement($field);
$field->setCustomForm(null);
}

return $this;
Expand Down
4 changes: 2 additions & 2 deletions lib/RoadizCoreBundle/src/Entity/CustomFormAnswer.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ class CustomFormAnswer extends AbstractEntity
targetEntity: CustomForm::class,
inversedBy: "customFormAnswers"
),
ORM\JoinColumn(name: "custom_form_id", referencedColumnName: "id", onDelete: "CASCADE"),
ORM\JoinColumn(name: "custom_form_id", referencedColumnName: "id", nullable: false, onDelete: "CASCADE"),
Serializer\Exclude,
SymfonySerializer\Ignore
]
private ?CustomForm $customForm = null;
private CustomForm $customForm;

public function __construct()
{
Expand Down
23 changes: 6 additions & 17 deletions lib/RoadizCoreBundle/src/Entity/CustomFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ class CustomFormField extends AbstractField

#[
ORM\ManyToOne(targetEntity: CustomForm::class, inversedBy: "fields"),
ORM\JoinColumn(name: "custom_form_id", referencedColumnName: "id", onDelete: "CASCADE"),
ORM\JoinColumn(name: "custom_form_id", referencedColumnName: "id", nullable: false, onDelete: "CASCADE"),
Serializer\Exclude,
SymfonySerializer\Ignore
]
private ?CustomForm $customForm = null;
private CustomForm $customForm;

/**
* @var Collection<int, CustomFormFieldAttribute>
Expand Down Expand Up @@ -84,33 +84,23 @@ public function __construct()
*
* @return $this
*/
public function setLabel($label)
public function setLabel($label): CustomFormField
{
parent::setLabel($label);
$this->setName($label);

return $this;
}

/**
* @return CustomForm|null
*/
public function getCustomForm(): ?CustomForm
public function getCustomForm(): CustomForm
{
return $this->customForm;
}

/**
* @param CustomForm|null $customForm
*
* @return $this
*/
public function setCustomForm(CustomForm $customForm = null): CustomFormField
public function setCustomForm(CustomForm $customForm): CustomFormField
{
$this->customForm = $customForm;
if (null !== $customForm) {
$this->customForm->addField($this);
}
$this->customForm->addField($this);

return $this;
}
Expand Down Expand Up @@ -162,7 +152,6 @@ public function __clone()
{
if ($this->id) {
$this->id = null;
$this->customForm = null;
$this->customFormFieldAttributes = new ArrayCollection();
}
}
Expand Down
Loading

0 comments on commit fd90805

Please sign in to comment.