editoast: fix broken diesel migration reverts #14103
Open
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.
When testing the Diesel migrations, I noticed that several
down.sqlwere broken for various reasons (empty scripts, tables deleted in the wrong order, migrations that delete or alter objects in their up script and do not revert the changes correctly in the down script).The PR fixes those migrations reverts.
To check the broken migrations: run
diesel migration redo --allwith an up-to-date database on thedevbranch. The command will fail during migration reverts. The same command should work on this branch.Note: the problem was often in a different migration than the one that broke. Some migration reverts were valid and coherent with their
up.sqlbut broke due to the revert of other migrations that did not correctly reset the database schema to its prior state on revert. It happens quite a lot with the migrations generated automatically with the commandmake-migration, as they tend to delete and re-create the objects they manipulate in theirup.sqland delete the same objects in thedown.sqlno matter if the objects existed before the migration or not. Those generated migration scripts work fine if the said objects didn't exist before but if they did, their revert leave the schema in a different state compared to before applying them, which then causes the revert of earlier migrations to crash.Example:
That migration has coherent up and down scripts:
But reverting it breaks because of the following migration:
There are several migrations that break in the same manner because of deleted tables / functions / triggers in later migrations (including the revert of
init.sql😅)The cleanest fix would be to modify the
make-migrationcommand and re-generate those migration scripts correctly. We could add a way to revert the object changes to their prior state instead of deleting them in the generated scripts. However, that sort of fix seems much more complex and time consuming than manually editing the faulty scripts and the end result would be the same. I chose not to go down that road and focus in this pull request on making the Diesel migrations reverts work again in a simple and fast manner.For now, I directly edited the faulty generated migration scripts and ignored their warning message
-- DO NOT EDIT THIS FILE MANUALLY, as it is their generation that is broken and it is not being fixed in this pull request. I could add a note in the relevant scripts to mention the manual fix, give some context and maybe add a TODO for a cleaner fix ?Note: there might be other issues with the migrations reverts. This pull request only fixes the obvious ones, i.e. the ones that lead
diesel migration redo --allto crash.