Skip to content

Conversation

VincentLanglet
Copy link
Contributor

@VincentLanglet VincentLanglet commented Jul 16, 2025

Q A
Type improvement ? fix ?
Fixed issues

Summary

When a Type Enum is used AND a length

#[ORM\Column(name: 'role', type: Types::Enum, length: 50)

the length is not used at all when generating the sql.

This makes sens for MysqlPlatform which list all the possible case, but for non-mysql platform it could be a way to reserved more length in advance in order to avoid futur ALTER TABLE.
The current strategy is too look at the max length of all the existing values, which means that adding new enum values might trigger every time an ALTER table just to use a bigger length.

@VincentLanglet VincentLanglet force-pushed the enumLength branch 2 times, most recently from 2523651 to f34ba6a Compare July 16, 2025 09:09
@morozov
Copy link
Member

morozov commented Jul 16, 2025

but for non-mysql platform it could be a way to reserved more length in advance in order to avoid futur ALTER TABLE.

What's the problem with future ALTER TABLE? On MySQL, if you add a new element to the enum, there will be an ALTER TABLE. Why avoid it on other platforms?

@VincentLanglet
Copy link
Contributor Author

What's the problem with future ALTER TABLE?

First, it feels odd to have

#[ORM\Column(name: 'role', type: Types::Enum, length: 50)

not throwing an error if the length is not used at all.

Then, running ALTER TABLE query might be slow if you already have something like million or billion of rows, no ?
It could be from Varchar(5) to VarChar(10) when adding an enum value or worst from VarChar(10) to VarChar(5) when removing one (And I assume the database will need to check every existing row length).
So how about allowing to avoid such extra query ?

If someone wants futur ALTER TABLE, he just have to not provide any length in the column.
If someone wants to reduce futur ALTER TABLE, that will be the purpose of the length column.

On MySQL, if you add a new element to the enum, there will be an ALTER TABLE. Why avoid it on other platforms?

Mysql use specific syntax ENUM() which really restrict the possible values we can save in the database, so there is a real benefit updating the table. For other Platform there is no such specific syntax and doctrine rely on string field ; so there is no benefit updating every time the column definition.

For example with Postgres, we can define constraint check

ALTER TABLE foo ADD CONSTRAINT foo_check CHECK (bar IN ('A', 'B', 'C'))

and when changing the enum, only the constraint is needed to be updated, but there is no gain to update the field length everytime (if we create the field with a bigger length first time).

@derrabus
Copy link
Member

First, it feels odd to have

#[ORM\Column(name: 'role', type: Types::Enum, length: 50)

not throwing an error if the length is not used at all.

It will also not throw if you assign a precision to that column. 🤷🏻‍♂️

Then, running ALTER TABLE query might be slow if you already have something like million or billion of rows, no ?

The column has variable length already, so the operation of changing the maximum column length itself is cheap. However, if the column is part of an index, that index might need to be rebuilt which can be expesinve.

It could be from Varchar(5) to VarChar(10) when adding an enum value or worst from VarChar(10) to VarChar(5) when removing one (And I assume the database will need to check every existing row length). So how about allowing to avoid such extra query ?

Yeah, allowing to specify the length of the varchar field is reasonable. I think, we should allow it.

@derrabus derrabus changed the base branch from 4.3.x to 4.4.x July 17, 2025 16:03
@VincentLanglet VincentLanglet requested a review from derrabus July 17, 2025 16:09
@VincentLanglet VincentLanglet force-pushed the enumLength branch 2 times, most recently from 89241ce to cceced6 Compare July 17, 2025 20:18
Copy link
Member

@derrabus derrabus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure this new behavior is covered by a functional test as well.

@VincentLanglet
Copy link
Contributor Author

Please make sure this new behavior is covered by a functional test as well.

If I look at your original PR the only functional test is for MySql https://github.com/doctrine/dbal/pull/6536/files#diff-5c5395e4aacfd52ec445d9845a95e2fd534deeaa427ad12b95ffab58127ceffaR16 which is not concerned here.

I'm basically doing the same than #6579 which didn't add functional tests.

I feel like the changed are already 100% covered by the unit test.

I'm not sure which functional test should be added ? @derrabus

@VincentLanglet VincentLanglet requested a review from derrabus July 24, 2025 18:55
@VincentLanglet VincentLanglet force-pushed the enumLength branch 4 times, most recently from 697ac8b to 7ca0e97 Compare July 25, 2025 16:28
derrabus
derrabus previously approved these changes Jul 26, 2025
@derrabus
Copy link
Member

Thanks. The documentation needs a little update as well, I suppose.

@VincentLanglet
Copy link
Contributor Author

Does 809e7d7 is enough @derrabus or you want something else (any suggestion welcomed).

@VincentLanglet VincentLanglet requested a review from derrabus July 26, 2025 12:30
@derrabus
Copy link
Member

Thank you!

@derrabus derrabus added this to the 4.4.0 milestone Jul 26, 2025
@derrabus derrabus merged commit e2e36dd into doctrine:4.4.x Jul 26, 2025
90 checks passed
@VincentLanglet
Copy link
Contributor Author

Thanks to you too !

Also, I have two extra question @derrabus ;

Thanks and good weekend

@derrabus
Copy link
Member

I've seen that one and it's on my list. 🤞🏻

@derrabus
Copy link
Member

We don't have a fixed schedule for releases. We do releases as we see fit.

derrabus added a commit to derrabus/dbal that referenced this pull request Aug 5, 2025
* 4.4.x:
  CI: Update SQL Server to version 2022 (doctrine#7069)
  fix(MariaDb): add support of new reserved word `VECTOR` (11.7+) (doctrine#7061)
  Improve deprecation
  Update schema definition documentaion
  Fix binding null as a boolean parameter on pgsql (doctrine#7059)
  Use provided length for enum type if given for non-mysql platform (doctrine#7034)
derrabus added a commit to derrabus/dbal that referenced this pull request Aug 5, 2025
* 4.4.x:
  CI: Update SQL Server to version 2022 (doctrine#7069)
  fix(MariaDb): add support of new reserved word `VECTOR` (11.7+) (doctrine#7061)
  Improve deprecation
  Update schema definition documentaion
  Fix binding null as a boolean parameter on pgsql (doctrine#7059)
  Use provided length for enum type if given for non-mysql platform (doctrine#7034)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants