From 8624ce9662932f92cf203ef75039204d3ac84d6d Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Fri, 29 Sep 2023 21:15:41 -0400 Subject: [PATCH] fix: really turn off SQLite journal files on 3.12+ --- CHANGES.rst | 3 +++ coverage/sqlitedb.py | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 92e8571d5..86c1ddb88 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -29,6 +29,9 @@ Unreleased data files. Now they are always ignored, fixing `issue 1605`_. Thanks to Brad Smith for suggesting fixes and providing detailed debugging. +- On Python 3.12+, we now disable SQLite writing journal files, which should be + a little faster. + .. _issue 1605: https://github.com/nedbat/coveragepy/pull/1605 .. _issue 1684: https://github.com/nedbat/coveragepy/issues/1684 .. _pull 1685: https://github.com/nedbat/coveragepy/pull/1685 diff --git a/coverage/sqlitedb.py b/coverage/sqlitedb.py index 4d15b5b50..c9e7aee30 100644 --- a/coverage/sqlitedb.py +++ b/coverage/sqlitedb.py @@ -55,8 +55,18 @@ def _connect(self) -> None: self.con.create_function("REGEXP", 2, lambda txt, pat: re.search(txt, pat) is not None) + # Turning off journal_mode can speed up writing. It can't always be + # disabled, so we have to be prepared for *-journal files elsewhere. + # In Python 3.12+, we can change the config to allow journal_mode=off. + if hasattr(sqlite3, "SQLITE_DBCONFIG_DEFENSIVE"): + # Turn off defensive mode, so that journal_mode=off can succeed. + self.con.setconfig( # type: ignore[attr-defined] + sqlite3.SQLITE_DBCONFIG_DEFENSIVE, False + ) + # This pragma makes writing faster. It disables rollbacks, but we never need them. self.execute_void("pragma journal_mode=off") + # This pragma makes writing faster. It can fail in unusual situations # (https://github.com/nedbat/coveragepy/issues/1646), so use fail_ok=True # to keep things going.