Add ability to disable the sqlite3 quick_check (#39479)

This commit is contained in:
J. Nick Koston authored and GitHub committed 2020-09-02 10:12:56 +02:00
1 parent 225becc89a
commit 557684c3ce
5 files changed
+99 -21

No files matched your search

+17 -7
View File
@@ -9,7 +9,7 @@ from sqlalchemy.exc import OperationalError, SQLAlchemyError
import homeassistant.util.dt as dt_util
from .const import DATA_INSTANCE, SQLITE_URL_PREFIX
from .const import CONF_DB_INTEGRITY_CHECK, DATA_INSTANCE, SQLITE_URL_PREFIX
from .models import ALL_TABLES, process_timestamp
_LOGGER = logging.getLogger(__name__)
@@ -21,7 +21,7 @@ SQLITE3_POSTFIXES = ["", "-wal", "-shm"]
# This is the maximum time after the recorder ends the session
# before we no longer consider startup to be a "restart" and we
# should do a check on the sqlite3 database.
MAX_RESTART_TIME = timedelta(minutes=6)
MAX_RESTART_TIME = timedelta(minutes=10)
@contextmanager
@@ -110,7 +110,7 @@ def execute(qry, to_native=False, validate_entity_ids=True):
time.sleep(QUERY_RETRY_WAIT)
def validate_or_move_away_sqlite_database(dburl: str) -> bool:
def validate_or_move_away_sqlite_database(dburl: str, db_integrity_check: bool) -> bool:
"""Ensure that the database is valid or move it away."""
dbpath = dburl[len(SQLITE_URL_PREFIX) :]
@@ -118,7 +118,7 @@ def validate_or_move_away_sqlite_database(dburl: str) -> bool:
# Database does not exist yet, this is OK
return True
if not validate_sqlite_database(dbpath):
if not validate_sqlite_database(dbpath, db_integrity_check):
_move_away_broken_database(dbpath)
return False
@@ -154,13 +154,13 @@ def basic_sanity_check(cursor):
return True
def validate_sqlite_database(dbpath: str) -> bool:
def validate_sqlite_database(dbpath: str, db_integrity_check: bool) -> bool:
"""Run a quick check on an sqlite database to see if it is corrupt."""
import sqlite3 # pylint: disable=import-outside-toplevel
try:
conn = sqlite3.connect(dbpath)
run_checks_on_open_db(dbpath, conn.cursor())
run_checks_on_open_db(dbpath, conn.cursor(), db_integrity_check)
conn.close()
except sqlite3.DatabaseError:
_LOGGER.exception("The database at %s is corrupt or malformed.", dbpath)
@@ -169,7 +169,7 @@ def validate_sqlite_database(dbpath: str) -> bool:
return True
def run_checks_on_open_db(dbpath, cursor):
def run_checks_on_open_db(dbpath, cursor, db_integrity_check):
"""Run checks that will generate a sqlite3 exception if there is corruption."""
if basic_sanity_check(cursor) and last_run_was_recently_clean(cursor):
_LOGGER.debug(
@@ -177,6 +177,16 @@ def run_checks_on_open_db(dbpath, cursor):
)
return
if not db_integrity_check:
# Always warn so when it does fail they remember it has
# been manually disabled
_LOGGER.warning(
"The quick_check on the sqlite3 database at %s was skipped because %s was disabled",
dbpath,
CONF_DB_INTEGRITY_CHECK,
)
return
_LOGGER.debug(
"A quick_check is being performed on the sqlite3 database at %s", dbpath
)