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
BREAKING CHANGE: Make sure you don't have fields with name longer than 50 characters before migrating. Migration can be skipped if so.
  • Loading branch information
ambroisemaupate committed Mar 18, 2024
1 parent fb5a2d8 commit 8d7529a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
44 changes: 44 additions & 0 deletions lib/RoadizCoreBundle/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');
}
}
4 changes: 2 additions & 2 deletions lib/RoadizCoreBundle/src/Entity/FieldAwareEntityTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

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

public function getFieldName(): string
Expand Down
5 changes: 3 additions & 2 deletions lib/RoadizCoreBundle/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

0 comments on commit 8d7529a

Please sign in to comment.