[10.x] Guess table name correctly in migrations if column's name have ('to', 'from' and/or 'in') terms - #48437
Conversation
| const CHANGE_PATTERNS = [ | ||
| '/_(to|from|in)_(\w+)_table$/', | ||
| '/_(to|from|in)_(\w+)$/', | ||
| '/_(to|from|in)_(?!.*?(\_to\_|\_in\_|\_from\_))(\w+)_table$/', | ||
| '/_(to|from|in)_(?!.*?(\_to\_|\_in\_|\_from\_))(\w+)$/', | ||
| ]; |
There was a problem hiding this comment.
I think we can just add .* in the beginning of the regex, so '/_(to|from|in)_(\w+)$/' becomes '/.*_(to|from|in)_(\w+)$/'. Based on my testing it also solves the issue and makes regex simpler.
There was a problem hiding this comment.
But better double check it before applying, maybe I missed something :)
There was a problem hiding this comment.
Overall thanks for PR, I think it's useful for cases like you described.
There was a problem hiding this comment.
@i350, good catch on the bug here. I agree with @hivokas though, this regex could be a little simpler, for example:
const CHANGE_PATTERNS = [
'/.+_(to|from|in)_(\w+)_table$/',
'/.+_(to|from|in)_(\w+)$/',
];Given the greedy nature of regular expressions, this will force it to only match the "last" instance in the string. I quickly tried it with your new test cases in regex101. Feel free to update your PR and see if it works for you.
There was a problem hiding this comment.
Thanks @hivokas & @jasonmccreary
I just pushed an update to use the simplified regex you provided (52eac63)
This PR improves the table guesser, to work correctly if the user trying make migration that add/update/delete column have one of the terms ("to", "from" or/and "in")
For example, if the user executed the command
The table name guesser before this update will return "3rd_party_service_column_to_users" as a table name, but after the update, it returns "users"