Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/crawlee/storage_clients/_sql/_storage_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ def __init__(
self._initialized = False
self.session_maker: None | async_sessionmaker[AsyncSession] = None

# Flag needed to apply optimizations only for default database
self._default_flag = self._engine is None and self._connection_string is None
# Flag needed to apply optimizations only for SQLite database
self._optimization_flag = self._engine is None and (
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we instead set up an _on_connect callback here that would receive the new DB connection? That way, the logic (if sqlite, set up some pragmas) would be in one place.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea.

self._connection_string is None or self._connection_string.startswith('sqlite')
)
self._dialect_name: str | None = None

# Call the notification only once
Expand Down Expand Up @@ -129,7 +131,7 @@ async def initialize(self, configuration: Configuration) -> None:
# This is likely an attempt to create a database from several parallel processes.
try:
# Set SQLite pragmas for performance and consistency
if self._default_flag:
if self._optimization_flag:
await conn.execute(text('PRAGMA journal_mode=WAL')) # Better concurrency
await conn.execute(text('PRAGMA synchronous=NORMAL')) # Balanced safety/speed
await conn.execute(text('PRAGMA cache_size=100000')) # 100MB cache
Expand Down
51 changes: 51 additions & 0 deletions tests/unit/storage_clients/_sql/test_sql_storage_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from sqlalchemy import text
from sqlalchemy.ext.asyncio import create_async_engine

from crawlee.configuration import Configuration
from crawlee.storage_clients import SqlStorageClient

if TYPE_CHECKING:
from pathlib import Path


async def test_sqlite_wal_mode_with_default_connection(tmp_path: Path) -> None:
"""Test that WAL mode is applied for the default SQLite connection."""
configuration = Configuration(storage_dir=str(tmp_path))

async with SqlStorageClient() as storage_client:
await storage_client.initialize(configuration)

async with storage_client.engine.begin() as conn:
result = await conn.execute(text('PRAGMA journal_mode'))
assert result.scalar() == 'wal'


async def test_sqlite_wal_mode_with_connection_string(tmp_path: Path) -> None:
"""Test that WAL mode is applied when using a custom SQLite connection string."""
db_path = tmp_path / 'test.db'
configuration = Configuration(storage_dir=str(tmp_path))

async with SqlStorageClient(connection_string=f'sqlite+aiosqlite:///{db_path}') as storage_client:
await storage_client.initialize(configuration)

async with storage_client.engine.begin() as conn:
result = await conn.execute(text('PRAGMA journal_mode'))
assert result.scalar() == 'wal'


async def test_sqlite_wal_mode_not_applied_with_custom_engine(tmp_path: Path) -> None:
"""Test that WAL mode is not applied when using a user-provided engine."""
db_path = tmp_path / 'test.db'
configuration = Configuration(storage_dir=str(tmp_path))
engine = create_async_engine(f'sqlite+aiosqlite:///{db_path}', future=True)

async with SqlStorageClient(engine=engine) as storage_client:
await storage_client.initialize(configuration)

async with engine.begin() as conn:
result = await conn.execute(text('PRAGMA journal_mode'))
assert result.scalar() != 'wal'
4 changes: 4 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading