From c4c17e3e046a627a7d2353c734bc69f05d4fc3b1 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Tue, 17 Nov 2026 21:06:05 +0100 Subject: feat(core): add configurable SQLite pragmas Apply validated SQLite settings through SQLAlchemy's connection event so every SQLite connection and bind receives consistent foreign key, WAL, timeout, and security defaults. Allow deployments to override supported PRAGMAs safely while skipping file-only settings for in-memory databases and leaving other database backends untouched. --- tests/test_config.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'tests/test_config.py') diff --git a/tests/test_config.py b/tests/test_config.py index 81ff498..ca87403 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -497,3 +497,64 @@ def test_proxy_fix_can_be_configured_via_environment( app = create_app(EnvProxyConfig) assert app.config["PROXY_FIX_TRUSTED_HOPS"] == 1 assert app.wsgi_app.__class__.__name__ == "ProxyFix" + + +def test_sqlite_pragma_overrides_keep_default_settings(tmp_path: Path) -> None: + class PartialSQLiteConfig(TestingConfig): + LOG_FILE = str(tmp_path / "partial-sqlite-test.log") + SQLITE_PRAGMAS = {"busy_timeout": 10000, "synchronous": "normal"} + + app = create_app(PartialSQLiteConfig) + assert app.config["SQLITE_PRAGMAS"] == { + "foreign_keys": True, + "journal_mode": "WAL", + "busy_timeout": 10000, + "trusted_schema": False, + "synchronous": "NORMAL", + "cache_size": None, + "wal_autocheckpoint": None, + "journal_size_limit": None, + } + # The default mapping must not be modified by individual app overrides. + assert TestingConfig.SQLITE_PRAGMAS["busy_timeout"] == 5000 + + +@pytest.mark.parametrize( + "invalid", + [ + "PRAGMA foreign_keys=OFF", + None, + {"foreign_keys": "OFF"}, + {"journal_mode": "WAL; DROP TABLE users"}, + {"journal_mode": "MEMORY"}, + {"trusted_schema": 0}, + {"busy_timeout": -1}, + {"busy_timeout": True}, + {"cache_size": 2**32}, + {"wal_autocheckpoint": -1}, + {"journal_size_limit": -2}, + {"synchronous": "INVALID"}, + {"unsupported_pragma": 42}, + ], +) +def test_invalid_sqlite_pragmas_fail_at_startup( + tmp_path: Path, invalid: object +) -> None: + class BadSQLiteConfig(TestingConfig): + LOG_FILE = str(tmp_path / "bad-sqlite-test.log") + SQLITE_PRAGMAS = invalid + + with pytest.raises((TypeError, ValueError), match="SQLITE_PRAGMAS"): + create_app(BadSQLiteConfig) + + +def test_uppercase_sqlite_pragma_keys_support_environment_convention( + tmp_path: Path, +) -> None: + class EnvStyleSQLiteConfig(TestingConfig): + LOG_FILE = str(tmp_path / "env-style-sqlite-test.log") + SQLITE_PRAGMAS = {"CACHE_SIZE": -8192, "SYNCHRONOUS": "normal"} + + app = create_app(EnvStyleSQLiteConfig) + assert app.config["SQLITE_PRAGMAS"]["cache_size"] == -8192 + assert app.config["SQLITE_PRAGMAS"]["synchronous"] == "NORMAL" -- cgit v1.3.1