Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement an EnumType for MySQL/MariaDB #6536

Merged
merged 1 commit into from
Oct 10, 2024

Conversation

derrabus
Copy link
Member

@derrabus derrabus commented Oct 9, 2024

Q A
Type feature
Fixed issues doctrine/migrations#1441 (partly)

Summary

This PR adds an EnumType that allows us to introspect and diff tables that make use of MySQL's ENUM column type.

src/Platforms/AbstractPlatform.php Outdated Show resolved Hide resolved
src/Platforms/AbstractPlatform.php Outdated Show resolved Hide resolved
@berkut1
Copy link
Contributor

berkut1 commented Oct 9, 2024

I'm just curious, if you've decided to implement partial support for ENUM only for certain DBMSs, then why not reconsider my previous idea doctrine/migrations#1441 (comment), which theoretically shouldn't be limited to just the ENUM type?

I know you rejected this idea here doctrine/migrations#1441 (comment), but to me, it seems like the simplest and most flexible solution to implement. However, I might be missing something.

@stof
Copy link
Member

stof commented Oct 9, 2024

@berkut1 the VerbatimType to be used during schema introspection solves a different need that having an EnumType in DBAL (even though there is indeed some overlap when considering the case of introspecting a database containing an enum field).

@derrabus derrabus force-pushed the feature/enum-types branch 2 times, most recently from 2bb4731 to 2f81998 Compare October 9, 2024 17:01
@derrabus
Copy link
Member Author

derrabus commented Oct 9, 2024

What @stof said, basically. I still believe a verbatim type is useful because I don't want to continuously catch up with obscure types that anyone might be using out there. But MySQL enums are pretty popular and I've seen so many workarounds in the wild that I felt like we should do something in the core to accommodate projects that rely on ENUM columns.

@derrabus derrabus force-pushed the feature/enum-types branch 4 times, most recently from 0f5cfd2 to 3c462e4 Compare October 10, 2024 08:44
@derrabus derrabus added this to the 4.2.0 milestone Oct 10, 2024
@derrabus derrabus requested a review from stof October 10, 2024 09:46
@derrabus derrabus force-pushed the feature/enum-types branch 2 times, most recently from cf3001f to 3811651 Compare October 10, 2024 09:59

use Doctrine\DBAL\Platforms\AbstractPlatform;

final class EnumType extends Type

Choose a reason for hiding this comment

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

Why do you make the class final? Other types are not final

Copy link
Member Author

Choose a reason for hiding this comment

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

Why would you want to extend it?

Choose a reason for hiding this comment

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

Well e.g. my custom enum types could extend it, and then it'd be clear hierarchy, that this is an enum related type. Its not critical, but there is just one other type (AsciiStringType) that is final, so I don't see reason why limit the inheritance specifically for EnumType, all other types are open.

Copy link
Member Author

Choose a reason for hiding this comment

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

You don't need that hierarchy. This class contains a single method which is just a delegate to a platform method. Extending this class is absolutely pointless.

And we can discuss finalizing the other types for the sake of consistency as well, if you like. 😉

throw ColumnValuesRequired::new($this, 'ENUM');
}

return $this->getStringTypeDeclarationSQL(['length' => max(...array_map(mb_strlen(...), $column['values']))]);
Copy link
Member

Choose a reason for hiding this comment

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

max accepts an array.

Suggested change
return $this->getStringTypeDeclarationSQL(['length' => max(...array_map(mb_strlen(...), $column['values']))]);
return $this->getStringTypeDeclarationSQL(['length' => max(array_map(mb_strlen(...), $column['values']))]);

Copy link
Member Author

Choose a reason for hiding this comment

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

It does, but why's that better than using the ... operator? 🤔

Copy link
Member

Choose a reason for hiding this comment

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

3 bytes less 😅
That generates some less OPcode operations. https://3v4l.org/GjuE7/vld

@derrabus derrabus merged commit 9744af4 into doctrine:4.2.x Oct 10, 2024
91 of 92 checks passed
@derrabus derrabus deleted the feature/enum-types branch October 10, 2024 12:20
derrabus added a commit to derrabus/dbal that referenced this pull request Oct 10, 2024
* 4.2.x:
  Implement an EnumType for MySQL/MariaDB (doctrine#6536)
  Leverage the new PDO subclasses (doctrine#6532)
  PHPStan 1.12.6 (doctrine#6535)
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.

7 participants