fix: Disable Foreign Key Checks During Restore #4444
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.
What type of PR is this?
(REQUIRED)
What this PR does / why we need it:
(REQUIRED)
In certain instances, when restoring from backup, users may be restored before households. Since users have a fk constraint with households, this will fail, because the household doesn't exist yet.
This PR disables fk constraints during restore, then re-enables them. It uses a context manager to do this, so even if there's an error it will always be restored to its initial value.
Which issue(s) this PR fixes:
(REQUIRED)
Fixes #4433
Special notes for your reviewer:
(fill-in or delete this section)
Since it's possible to disable fk constraints manually (and they are disabled on sqlite by default) I made sure to preserve the existing setting rather than disable/re-enable. This approach ensures if fk constraints are disabled before Mealie touches them, then they stay disabled.
Testing
(fill-in or delete this section)
I used the backup related to #4433 to reproduce the issue locally on postgres, then observed that it's fixed now. I also added an anonymized backup to our test suite.