-
-
Notifications
You must be signed in to change notification settings - Fork 32.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove legacy foreign key constraint from sqlite states table #120779
Conversation
Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
The 9 step alter isn't going to work for this case, and quite frankly its too risky. We need to do the full 12 step procedure https://www.sqlite.org/lang_altertable.html which is slower, but overall sqlite table rebuilds are fast so this is fine. |
Tested on 2x production Need to write some tests |
We already have an migration test for v32+. I've updated it now to verify the index can now be dropped for sqlite as well. I want to write some direct coverage for |
Do not tag this for backport. It should bake in dev for at least a week,and than beta before appearing in a release.Since we were unable to test in beta, we have instead enough people test the custom component in #117263. While a 12 step migration should be quite safe (and in most cases what sqlite actually does under the hood for complex alters), this is brand new for this code base and we cannot fully predict impact.Proposed change
Some old databases have
FOREIGN KEY(event_id) REFERENCES events (event_id) ON DELETE CASCADE,
in the states table and the index is now empty which results in a full table scan per each event_id.Unfortunately sqlite does not support dropping a foreign key constraint.
To fix this the whole states table needs to be recreated with a 12 step table rebuild to remove the legacy foreign key from the
states
tableThankfully a table rebuild is very fast in sqlite. With 2GiB database taking about 20s (1GiB took < 10s). One iteration of the purge cycle that was happening in the issue would take longer than a table rebuild.
A nice side effect is it will reduce the database size by ~4-8% on average after the next repack (2nd sunday of the month) and improve insert performance a tiny bit.
Type of change
Additional information
Checklist
ruff format homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
..coveragerc
.To help with the load of incoming pull requests: