Formatting fixes

This commit is contained in:
Friedrich Lindenberg 2021-11-17 18:27:48 +01:00
parent 5885680645
commit 4e359f5e01
3 changed files with 29 additions and 20 deletions

View File

@ -21,7 +21,7 @@ def connect(
ensure_schema=True,
row_type=row_type,
sqlite_wal_mode=True,
sqlite_pragmas=None,
on_connect_statements=None,
):
"""Opens a new connection to a database.
@ -40,12 +40,13 @@ def connect(
the `ensure_schema` argument. It can also be overridden in a lot of the
data manipulation methods using the `ensure` flag.
If you want to run custom SQLite pragmas on database connect, you can add them to sqlite_pragmas
You can view a full list of PRAGMAs here:
https://www.sqlite.org/pragma.html
If you want to run custom SQLite pragmas on database connect, you can add them
to on_connect_statements as a set of strings. You can view a full
`list of PRAGMAs here`_.
.. _SQLAlchemy Engine URL: http://docs.sqlalchemy.org/en/latest/core/engines.html#sqlalchemy.create_engine
.. _DB connection timeout: http://docs.sqlalchemy.org/en/latest/core/pooling.html#setting-pool-recycle
.. _list of PRAGMAs here: https://www.sqlite.org/pragma.html
"""
if url is None:
url = os.environ.get("DATABASE_URL", "sqlite://")
@ -57,5 +58,5 @@ def connect(
ensure_schema=ensure_schema,
row_type=row_type,
sqlite_wal_mode=sqlite_wal_mode,
on_connect_statements=sqlite_pragmas,
on_connect_statements=on_connect_statements,
)

View File

@ -59,7 +59,8 @@ class Database(object):
self.is_sqlite = self.engine.dialect.name == "sqlite"
if on_connect_statements is None:
on_connect_statements = []
def _run_on_connect_statements(dbapi_con, con_record):
def _run_on_connect(dbapi_con, con_record):
# reference:
# https://stackoverflow.com/questions/9671490/how-to-set-sqlite-pragma-statements-with-sqlalchemy
# https://stackoverflow.com/a/7831210/1890086
@ -70,8 +71,8 @@ class Database(object):
# we only enable WAL mode for sqlite databases that are not in-memory
on_connect_statements.append("PRAGMA journal_mode=WAL")
if on_connect_statements:
event.listen(self.engine, "connect", _run_on_connect_statements)
if len(on_connect_statements):
event.listen(self.engine, "connect", _run_on_connect)
self.types = Types(is_postgres=self.is_postgres)
self.url = url

View File

@ -31,7 +31,14 @@ def iter_result_proxy(rp, step=None):
def make_sqlite_url(
path, cache=None, timeout=None, mode=None, check_same_thread=True, immutable=False, nolock=False):
path,
cache=None,
timeout=None,
mode=None,
check_same_thread=True,
immutable=False,
nolock=False,
):
# NOTE: this PR
# https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/1474/
# added support for URIs in SQLite
@ -41,24 +48,24 @@ def make_sqlite_url(
# https://www.sqlite.org/uri.html
params = {}
if cache:
assert cache in ('shared', 'private')
params['cache'] = cache
assert cache in ("shared", "private")
params["cache"] = cache
if timeout:
# Note: if timeout is None, it uses the default timeout
params['timeout'] = timeout
params["timeout"] = timeout
if mode:
assert mode in ('ro', 'rw', 'rwc')
params['mode'] = mode
assert mode in ("ro", "rw", "rwc")
params["mode"] = mode
if nolock:
params['nolock'] = 1
params["nolock"] = 1
if immutable:
params['immutable'] = 1
params["immutable"] = 1
if not check_same_thread:
params['check_same_thread'] = 'false'
params["check_same_thread"] = "false"
if not params:
return 'sqlite:///' + path
params['uri'] = 'true'
return 'sqlite:///file:' + path + '?' + urlencode(params)
return "sqlite:///" + path
params["uri"] = "true"
return "sqlite:///file:" + path + "?" + urlencode(params)
class ResultIter(object):