[5.6] Better enumeration columns support #22109
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Proposal
Currently we have true enumeration columns support for MySQL and PostgreSQL. MySQL implementation is using the native non-standard
ENUM
data type while PostgreSQL implementation is using aVARCHAR
data type with aCHECK
constraint - PostgreSQL does have a native enumerated data type but they both work and it's easier to implement and sometimes preferable to use constraints. They're well implemented.Since
CHECK
constraints are part of SQL-92 standard (ANSI X3.135-1992 and ISO/IEC 9075:1992) and both SQLite and SQL Server does support them, I'm proposing true support for enumerated columns for all database drivers.Implementation
Both SQLite and SQL Server would be using the same implementation being used in PostgreSQL: a
VARCHAR
compatible data type with aCHECK
constraint. SQL Server would be usingNVARCHAR(255)
while SQLite justVARCHAR
since it does not have support for column length.As a proof-of-concept that it works, here are the SQL fiddles demonstrating them in action:
Changes
I'm targeting this for 5.5 first just because I'm not sure where it would be desirable since it's a feature that would be applied only to new installations and just adds a non-existing behaviour that matches the usage expectation. If this would be acceptable just for 5.6, let me know and I'll rebase it accordingly ASAP. 😉
Code
Illuminate\Database\Grammar
quote()
helper method to reuse within concrete grammars.Illuminate\Database\Schema\Grammars\MySqlGrammar
Illuminate\Database\Schema\Grammars\PostgresGrammar
typeEnum()
method to use the newquote()
method, minor code readability improvements.Illuminate\Database\Schema\Grammars\SQLiteGrammar
Illuminate\Database\Schema\Grammars\SqlServerGrammar
typeEnum()
method to include a newCHECK
constraint.Tests
Illuminate\Tests\Database\DatabaseMySqlSchemaGrammarTest
Illuminate\Tests\Database\DatabasePostgresSchemaGrammarTest
Illuminate\Tests\Database\DatabaseSQLiteSchemaGrammarTest
Illuminate\Tests\Database\DatabaseSqlServerSchemaGrammarTest