Automatic recovery/cleanup of sqlite history db

Occasionally the history db file gets corrupted and cannot be (re)loaded by homeassistant. The only known solution I’ve found so far is to manually remove it, letting homeassistant recreate it.

It would be nice if the system supported automatic recovery. If the db cannot be loaded, it could run some sqlite commands to try to rebuild it, and if that fails, just clear it and write a new one. Having a truncated history that works correctly is better than having no history, log file full of spam with write errors, etc.