Skip to content

Commit 74c0b6c

Browse files
simonwclaude
andauthored
Detect upsert support on SQLite 3.24 to 3.34, closes #976 (#977)
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 <noreply@anthropic.com>
1 parent cd69c76 commit 74c0b6c

5 files changed

Lines changed: 20 additions & 3 deletions

File tree

‎.github/actions/setup-sqlite-version/setup-sqlite-version.sh‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ case "$version_spec" in
1313
amalgamation_id="3460000"
1414
builtin_fallback_urls="https://static.simonwillison.net/static/2026/sqlite-amalgamation-3460000.zip"
1515
;;
16+
3.25.3)
17+
sqlite_version="3.25.3"
18+
sqlite_year="2018"
19+
amalgamation_id="3250300"
20+
builtin_fallback_urls=""
21+
;;
1622
3.23.1)
1723
sqlite_version="3.23.1"
1824
sqlite_year="2018"

‎.github/workflows/test-sqlite-support.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jobs:
1616
python-version: ["3.10"]
1717
sqlite-version: [
1818
"3.46",
19+
"3.25.3", # 2018-11-05, UPSERT but requires a conflict target
1920
"3.23.1", # 2018-04-10, before UPSERT
2021
]
2122
steps:

‎sqlite_utils/db.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,8 @@ def supports_strict(self) -> bool:
11681168
@property
11691169
def supports_on_conflict(self) -> bool:
11701170
# SQLite's upsert is implemented as INSERT INTO ... ON CONFLICT DO ...
1171+
# SQLite 3.24 to 3.34 require an explicit conflict target, so the
1172+
# probe includes one: https://github.com/simonw/sqlite-utils/issues/976
11711173
if not hasattr(self, "_supports_on_conflict"):
11721174
table_name = f"t{secrets.token_hex(16)}"
11731175
try:
@@ -1180,7 +1182,7 @@ def supports_on_conflict(self) -> bool:
11801182
)
11811183
self.conn.execute(
11821184
f"insert into {table_name} (id, name) values (1, 'two') "
1183-
"on conflict do update set name = 'two'"
1185+
"on conflict(id) do update set name = 'two'"
11841186
)
11851187
self._supports_on_conflict = True
11861188
except sqlite3.OperationalError:

‎tests/test_fts.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,8 @@ def test_enable_fts_with_triggers(fresh_db):
345345
@pytest.mark.parametrize("fts_version", ["FTS4", "FTS5"])
346346
def test_fts_triggers_keep_updates_and_deletes_in_sync(fresh_db, fts_version):
347347
table_name = "notes '100%'"
348-
if fts_version == "FTS5" and fresh_db.sqlite_version < (3, 24, 0):
349-
# SQLite 3.23.1 does not quote the table name when it builds the
348+
if fts_version == "FTS5" and fresh_db.sqlite_version < (3, 30, 0):
349+
# SQLite before 3.30.0 does not quote the table name when it builds the
350350
# query for "order by rank", so FTS5 search fails with a syntax
351351
# error for any table name that needs quoting
352352
table_name = "notes"

‎tests/test_upsert.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,14 @@ def test_upsert_existing_row_omitting_not_null_column(use_old_upsert):
225225
assert dogs.last_pk == 1
226226

227227

228+
def test_supports_on_conflict_matches_sqlite_version(fresh_db):
229+
# Upsert arrived in SQLite 3.24.0, but versions before 3.35.0 require an
230+
# explicit conflict target - the probe must still detect support there
231+
# https://github.com/simonw/sqlite-utils/issues/976
232+
version = tuple(int(part) for part in sqlite3.sqlite_version.split("."))
233+
assert fresh_db.supports_on_conflict == (version >= (3, 24, 0))
234+
235+
228236
def test_upsert_new_row_omitting_not_null_column_errors(fresh_db):
229237
# A partial upsert that would create a new row must not invent a value
230238
# for a NOT NULL column - it should fail the constraint

0 commit comments

Comments
 (0)