Ensure MariaDB/MySQL can be purged and handle states being deleted out from under the recorder #43610
Ensure MariaDB/MySQL can be purged and handle states being deleted out from under the recorder #43610bdraco merged 6 commits intohome-assistant:devfrom moinmoin-sh:recorder_fix
Conversation
This addresses #42402 Relationships within table "states" and between tables "states" and "events " #40467 prevent the purge from working correctly. The database increases w/o any purge. This proposal sets related indices to NULL and permits deleting of rows. Further explanations can be found here #42402 This proposal also allows to purge the tables "events" and "states" in any order.
|
Hi @moinmoin-sh, It seems you haven't yet signed a CLA. Please do so here. Once you do that we will be able to review and accept this pull request. Thanks! |
|
does this require re-creating the DB? |
|
@Spartan-II-117 Good point. Yes it does. |
Corrected for Black style requirements
We will need to bump the database version in See If we find one of the ones we want to alter we probably need to drop and re-add them. On a side note, we probably need to move to |
Co-authored-by: J. Nick Koston <nick@koston.org>
|
Here is a migration script based on what It needs a bit of adjusting as it couldn't work it out automaticlly. |
I'm sure the "state" table has to be altered to reflect the newly defined CONSTRAINTS (ondelete ...) as described by other users here #42402. Unfotunately I've no clue how to integrate this in migrations.py. Can you take care for that ? |
|
It works out to manually write out: |
Done. Please give it a try. |
|
Thanks , will do tomorrow. Too late for today (11:30 pm) |
|
I'm 11 hours behind you so I might see your response before I go to bed. I was hoping to write a test for this but we don't have a way to mock MySQL or MariaDB in the test suite so it's probably ok as is. |
|
OK, I did some testing with the modified code: short summary : looks great to me ! And here a little bit more detail:
When using the above commands I observed that when running the DELETE from the "events" table first, also the corresponding rows from the "states" table were deleted - thanks to "ON DELETE CASCADE" I assume. |
|
Thanks for testing. I want to test downgrades before I merge this. Sadly all of the databases we support implement foreign keys slightly differently which is how this problem crept in. Since sqlite3 won't handle the cascade the same way we should keep deleting the states first. I also want to test with a fresh SQLite setup just to be sure (almost definitely fine though since we cover this with tests) |
|
OK, understood. Please let me know if I need to do something to get this merged. Thanks again and enjoy the Black Friday |
|
|
I did some more testing this morning and everything looks good 👍 |
Proposed change
Relationships within table "states" and between tables
statesandeventsprevent the purge from working correctly on some databases.This proposal sets related indices to NULL or CASCADE to permit deleting of old rows.
Further explanations can be found here #42402
This proposal also allows to purge the tables "events" and "states" in any order.
Type of change
Example entry for
configuration.yaml:# Example configuration.yamlAdditional information
Checklist
black --fast 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.The integration reached or maintains the following Integration Quality Scale:
To help with the load of incoming pull requests: