Enhance index hint validation for multiple indexes#58505
Enhance index hint validation for multiple indexes#58505taylorotwell merged 2 commits intolaravel:12.xfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Updates MySQL index hint validation to support specifying multiple indexes in a single hint string (e.g., useIndex('user_id, PRIMARY')), addressing #58504.
Changes:
- Splits the provided index hint string on commas and validates each index name individually.
- Preserves existing SQL generation while allowing comma/whitespace-separated index lists.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| $indexes = array_map('trim', explode(',', $index)); | ||
| foreach ($indexes as $idx) { | ||
| if (! preg_match('/^[a-zA-Z0-9_$]+$/', $idx)) { | ||
| throw new InvalidArgumentException('Index name contains invalid characters.'); | ||
| } | ||
| } |
There was a problem hiding this comment.
The new comma-separated index parsing/validation should be covered by a unit test (e.g., useIndex('user_id, PRIMARY') producing use index (user_id, PRIMARY)), since tests/Database/DatabaseQueryBuilderTest.php currently only asserts single-index behavior for MySQL. Adding a test will prevent regressions and clarify intended whitespace/comma handling.
shaedrich
left a comment
There was a problem hiding this comment.
It would probably be better if an array could be passed
|
|
||
| if (! preg_match('/^[a-zA-Z0-9_$]+$/', $index)) { | ||
| throw new InvalidArgumentException('Index name contains invalid characters.'); | ||
| $indexes = array_map('trim', explode(',', $index)); |
There was a problem hiding this comment.
You could use first-class callable notation here:
| $indexes = array_map('trim', explode(',', $index)); | |
| $indexes = array_map(trim(...), explode(',', $index)); |
| $indexes = array_map('trim', explode(',', $index)); | ||
| foreach ($indexes as $idx) { | ||
| if (! preg_match('/^[a-zA-Z0-9_$]+$/', $idx)) { | ||
| throw new InvalidArgumentException('Index name contains invalid characters.'); |
There was a problem hiding this comment.
It would be helpful to know which index was the problem:
| throw new InvalidArgumentException('Index name contains invalid characters.'); | |
| throw new InvalidArgumentException('Index name "' . $idx . '" contains invalid characters.'); |
#58504