From 8d7529aad953f5b4f5689672df9fd6ae725bc89f Mon Sep 17 00:00:00 2001 From: Ambroise Maupate Date: Mon, 18 Mar 2024 21:49:34 +0100 Subject: [PATCH] feat: Reduce NodeTypeField name to maximum 50 characters long. BREAKING CHANGE: Make sure you don't have fields with name longer than 50 characters before migrating. Migration can be skipped if so. --- .../migrations/Version20240318204224.php | 44 +++++++++++++++++++ .../src/Entity/FieldAwareEntityTrait.php | 4 +- .../src/Entity/NodeTypeField.php | 5 ++- 3 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 lib/RoadizCoreBundle/migrations/Version20240318204224.php diff --git a/lib/RoadizCoreBundle/migrations/Version20240318204224.php b/lib/RoadizCoreBundle/migrations/Version20240318204224.php new file mode 100644 index 00000000..0897dc0b --- /dev/null +++ b/lib/RoadizCoreBundle/migrations/Version20240318204224.php @@ -0,0 +1,44 @@ +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'); + } +} diff --git a/lib/RoadizCoreBundle/src/Entity/FieldAwareEntityTrait.php b/lib/RoadizCoreBundle/src/Entity/FieldAwareEntityTrait.php index 2e6ac2b9..be0611eb 100644 --- a/lib/RoadizCoreBundle/src/Entity/FieldAwareEntityTrait.php +++ b/lib/RoadizCoreBundle/src/Entity/FieldAwareEntityTrait.php @@ -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 diff --git a/lib/RoadizCoreBundle/src/Entity/NodeTypeField.php b/lib/RoadizCoreBundle/src/Entity/NodeTypeField.php index 83bd7700..07dd18bf 100644 --- a/lib/RoadizCoreBundle/src/Entity/NodeTypeField.php +++ b/lib/RoadizCoreBundle/src/Entity/NodeTypeField.php @@ -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"]), @@ -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()