mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 03:37:07 +00:00
Use sqlite's WAL mode to avoid database is locked
errors (#6519)
* Use sqlite's WAL mode to avoid `database is locked` errors - Relevant issue: https://github.com/home-assistant/home-assistant/issues/4780 Code: - http://stackoverflow.com/a/23661501/1588795 - http://docs.sqlalchemy.org/en/rel_0_9/dialects/sqlite.html#foreign-key-support - https://github.com/g2p/bedup/pull/86/files * Only set WAL if using sqlite * Reorder imports * Fix pylint warnings
This commit is contained in:
parent
97b9d3bd21
commit
33450c726d
@ -287,13 +287,27 @@ class Recorder(threading.Thread):
|
|||||||
|
|
||||||
def _setup_connection(self):
|
def _setup_connection(self):
|
||||||
"""Ensure database is ready to fly."""
|
"""Ensure database is ready to fly."""
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine, event
|
||||||
|
from sqlalchemy.engine import Engine
|
||||||
from sqlalchemy.orm import scoped_session
|
from sqlalchemy.orm import scoped_session
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
from . import models
|
from . import models
|
||||||
|
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
|
|
||||||
|
# pylint: disable=unused-variable
|
||||||
|
@event.listens_for(Engine, "connect")
|
||||||
|
def set_sqlite_pragma(dbapi_connection, connection_record):
|
||||||
|
"""Set sqlite's WAL mode."""
|
||||||
|
if self.db_url.startswith("sqlite://"):
|
||||||
|
old_isolation = dbapi_connection.isolation_level
|
||||||
|
dbapi_connection.isolation_level = None
|
||||||
|
cursor = dbapi_connection.cursor()
|
||||||
|
cursor.execute("PRAGMA journal_mode=WAL")
|
||||||
|
cursor.close()
|
||||||
|
dbapi_connection.isolation_level = old_isolation
|
||||||
|
|
||||||
if self.db_url == 'sqlite://' or ':memory:' in self.db_url:
|
if self.db_url == 'sqlite://' or ':memory:' in self.db_url:
|
||||||
from sqlalchemy.pool import StaticPool
|
from sqlalchemy.pool import StaticPool
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user