From 59c89fab941a403592f146b1d6ea592562d53046 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Mon, 30 Nov 2026 14:43:50 -0800 Subject: [PATCH] Detect upsert support on SQLite 3.24 to 3.34, closes #976 The supports_on_conflict probe used ON CONFLICT DO UPDATE with no conflict target, which SQLite only accepts from 3.35. On 3.24-3.34 this fell back to INSERT OR IGNORE + UPDATE, silently dropping new rows that violated NOT NULL. The probe now includes an explicit conflict target. Also adds SQLite 3.25.3 to the CI matrix, and moves the FTS5 quoting workaround in a test to apply below 3.30.0, where that SQLite bug was fixed. Co-Authored-By: Claude Opus 5.5 --- .../actions/setup-sqlite-version/setup-sqlite-version.sh | 6 ++++++ .github/workflows/test-sqlite-support.yml | 1 + sqlite_utils/db.py | 4 +++- tests/test_fts.py | 4 ++-- tests/test_upsert.py | 8 ++++++++ 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/.github/actions/setup-sqlite-version/setup-sqlite-version.sh b/.github/actions/setup-sqlite-version/setup-sqlite-version.sh index 0df72907c..924ba43d2 100644 --- a/.github/actions/setup-sqlite-version/setup-sqlite-version.sh +++ b/.github/actions/setup-sqlite-version/setup-sqlite-version.sh @@ -13,6 +13,12 @@ case "$version_spec" in amalgamation_id="3460000" builtin_fallback_urls="https://static.simonwillison.net/static/2026/sqlite-amalgamation-3460000.zip" ;; + 3.25.3) + sqlite_version="3.25.3" + sqlite_year="2018" + amalgamation_id="3250300" + builtin_fallback_urls="" + ;; 3.23.1) sqlite_version="3.23.1" sqlite_year="2018" diff --git a/.github/workflows/test-sqlite-support.yml b/.github/workflows/test-sqlite-support.yml index b8a81153e..b5f5e04bd 100644 --- a/.github/workflows/test-sqlite-support.yml +++ b/.github/workflows/test-sqlite-support.yml @@ -16,6 +16,7 @@ jobs: python-version: ["3.10"] sqlite-version: [ "3.46", + "3.25.3", # 2018-11-05, UPSERT but requires a conflict target "3.23.1", # 2018-04-10, before UPSERT ] steps: diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index ce0866c8f..e85ac52a9 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -1168,6 +1168,8 @@ def supports_strict(self) -> bool: @property def supports_on_conflict(self) -> bool: # SQLite's upsert is implemented as INSERT INTO ... ON CONFLICT DO ... + # SQLite 3.24 to 3.34 require an explicit conflict target, so the + # probe includes one: https://github.com/simonw/sqlite-utils/issues/976 if not hasattr(self, "_supports_on_conflict"): table_name = f"t{secrets.token_hex(16)}" try: @@ -1180,7 +1182,7 @@ def supports_on_conflict(self) -> bool: ) self.conn.execute( f"insert into {table_name} (id, name) values (1, 'two') " - "on conflict do update set name = 'two'" + "on conflict(id) do update set name = 'two'" ) self._supports_on_conflict = True except sqlite3.OperationalError: diff --git a/tests/test_fts.py b/tests/test_fts.py index 00d6cbbf2..5dd92fa20 100644 --- a/tests/test_fts.py +++ b/tests/test_fts.py @@ -345,8 +345,8 @@ def test_enable_fts_with_triggers(fresh_db): @pytest.mark.parametrize("fts_version", ["FTS4", "FTS5"]) def test_fts_triggers_keep_updates_and_deletes_in_sync(fresh_db, fts_version): table_name = "notes '100%'" - if fts_version == "FTS5" and fresh_db.sqlite_version < (3, 24, 0): - # SQLite 3.23.1 does not quote the table name when it builds the + if fts_version == "FTS5" and fresh_db.sqlite_version < (3, 30, 0): + # SQLite before 3.30.0 does not quote the table name when it builds the # query for "order by rank", so FTS5 search fails with a syntax # error for any table name that needs quoting table_name = "notes" diff --git a/tests/test_upsert.py b/tests/test_upsert.py index da60f1268..e2c2aeb37 100644 --- a/tests/test_upsert.py +++ b/tests/test_upsert.py @@ -225,6 +225,14 @@ def test_upsert_existing_row_omitting_not_null_column(use_old_upsert): assert dogs.last_pk == 1 +def test_supports_on_conflict_matches_sqlite_version(fresh_db): + # Upsert arrived in SQLite 3.24.0, but versions before 3.35.0 require an + # explicit conflict target - the probe must still detect support there + # https://github.com/simonw/sqlite-utils/issues/976 + version = tuple(int(part) for part in sqlite3.sqlite_version.split(".")) + assert fresh_db.supports_on_conflict == (version >= (3, 24, 0)) + + def test_upsert_new_row_omitting_not_null_column_errors(fresh_db): # A partial upsert that would create a new row must not invent a value # for a NOT NULL column - it should fail the constraint