Skip to content

Commit

Permalink
fine-tune sqlite for production use by default
Browse files Browse the repository at this point in the history
  • Loading branch information
imryche committed Dec 7, 2024
1 parent 0b0ba6a commit 2ba19ea
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion litequery/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from litequery.core import setup

__version__ = "0.5.1"
__version__ = "0.5.2"
__all__ = ["setup"]
21 changes: 19 additions & 2 deletions litequery/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ def dataclass_factory(cursor, row):


class LitequeryBase:
PRAGMAS = [
("journal_mode", "wal"),
("foreign_keys", 1),
("synchronous", "normal"),
("mmap_size", 134217728), # 128 Mb
("journal_size_limit", 67108864), # 64 Mb
("cache_size", 2000),
]

def __init__(self, database, queries):
self._database = database
self._conn = None
Expand Down Expand Up @@ -126,6 +135,10 @@ async def query_method(**kwargs):

return query_method

async def _apply_pragmas(self):
for pragma, value in self.PRAGMAS:
await self._conn.execute(f"pragma {pragma} = {value}")

@asynccontextmanager
async def transaction(self):
conn = await self.get_connection()
Expand All @@ -143,7 +156,7 @@ async def transaction(self):
async def connect(self):
self._conn = await aiosqlite.connect(self._database)
self._conn.row_factory = dataclass_factory
await self._conn.execute("PRAGMA foreign_keys = ON")
await self._apply_pragmas()

async def disconnect(self):
if self._conn is None:
Expand Down Expand Up @@ -180,6 +193,10 @@ def query_method(**kwargs):

return query_method

def _apply_pragmas(self):
for pragma, value in self.PRAGMAS:
self._conn.execute(f"pragma {pragma} = {value}")

@contextmanager
def transaction(self):
conn = self.get_connection()
Expand All @@ -197,7 +214,7 @@ def transaction(self):
def connect(self):
self._conn = sqlite3.connect(self._database)
self._conn.row_factory = dataclass_factory
self._conn.execute("PRAGMA foreign_keys = ON")
self._apply_pragmas()

def disconnect(self):
if self._conn is None:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "litequery"
version = "0.5.1"
version = "0.5.2"
authors = [{ name = "Dima Charnyshou", email = "[email protected]" }]
description = "A handy way to interact with an SQLite database from Python"
readme = "README.md"
Expand Down
32 changes: 32 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,35 @@ def test_foreign_keys_enabled_sync(lq_sync):

events = lq_sync.get_all_events()
assert len(events) == 0


@pytest.mark.asyncio
async def test_pragmas_configured_async(lq_async):
expected_pragmas = [
("journal_mode", "wal"),
("foreign_keys", 1),
("synchronous", 1),
("mmap_size", 134217728), # 128 Mb
("journal_size_limit", 67108864), # 64 Mb
("cache_size", 2000),
]
conn = await lq_async.get_connection()
for pragma, expected_value in expected_pragmas:
async with conn.execute(f"pragma {pragma}") as cursor:
result = await cursor.fetchone()
assert getattr(result, pragma) == expected_value


def test_pragmas_configured_sync(lq_sync):
expected_pragmas = [
("journal_mode", "wal"),
("foreign_keys", 1),
("synchronous", 1),
("mmap_size", 134217728), # 128 Mb
("journal_size_limit", 67108864), # 64 Mb
("cache_size", 2000),
]
conn = lq_sync.get_connection()
for pragma, expected_value in expected_pragmas:
result = conn.execute(f"pragma {pragma}").fetchone()
assert getattr(result, pragma) == expected_value

0 comments on commit 2ba19ea

Please sign in to comment.