Better handle non-existent migration line + suggestions with Levenshtein distance #558
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.
This is related to #546. That was a documentation problem, but there are
some code problems that are related.
The first is that an unknown migration line is a panic in the CLI. This
is a byproduct of the fact that
rivermigrate.New
checks the migrationline name, but doesn't return an error, so it'd panic instead.
Here, I'm suggesting that we do a small breaking change by having
rivermigrate.New
return a possible error. It's not great, but it'ssomething that'll easy for people to fix, and might help avoid some
future panics. An alternative possibility would be to add a new
NewWithError
or something of that nature, but it'd make the API alittle uglier.
Along with that change, we also bring in a change to suggest migration
lines in case of an unknown one using Levenshtein distances. This is
aimed at preventing totally unactionable errors in the event of very
simple misspellings (e.g. "workflow" versus "workflows"). Cobra actually
already has a similar feature built-in for command misspellings.
I vendored in this repo [1] as a Levenshtein implementation and did a
little code clean up. I don't want to add another Go module dependency
for such a simple algorithm (it's just a dozen lines), and I believe
their MIT license should be compatible.
[1] https://github.com/agnivade/levenshtein/tree/master