From 7aeb39fa1374978139c181a2a1a83c602ba90e68 Mon Sep 17 00:00:00 2001 From: sanidhyasaluja Date: Sat, 14 Nov 2026 23:19:19 +0530 Subject: [PATCH 1/2] Apply upsert SQL conversions only once, refs #946 --- docs/python-api.rst | 2 ++ sqlite_utils/db.py | 7 +---- tests/test_conversions.py | 63 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/docs/python-api.rst b/docs/python-api.rst index d515642a5..1ce2b1c8f 100644 --- a/docs/python-api.rst +++ b/docs/python-api.rst @@ -2346,6 +2346,8 @@ You can specify an upper case conversion for a specific column like so: The dictionary key is the column name to be converted. The value is the SQL fragment to use, with a ``?`` placeholder for the original value. +Conversions are applied once to each incoming value. With ``upsert()`` and ``upsert_all()``, this is the same whether a row is inserted or updated. + A more useful example: if you are working with `SpatiaLite `__ you may find yourself wanting to create geometry values from a WKT value. Code to do that could look like this: .. code-block:: python diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index c011d9bb0..e9ac59c88 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -4449,12 +4449,7 @@ def build_insert_queries_and_params( assignments = [] for c in non_pk_cols: c_quoted = quote_identifier(c) - if c in conversions: - assignments.append( - f"{c_quoted} = {conversions[c].replace('?', f'excluded.{c_quoted}')}" - ) - else: - assignments.append(f"{c_quoted} = excluded.{c_quoted}") + assignments.append(f"{c_quoted} = excluded.{c_quoted}") do_clause = "DO UPDATE SET " + ", ".join(assignments) else: # All columns are in the PK – nothing to update. diff --git a/tests/test_conversions.py b/tests/test_conversions.py index bb58df420..a7267174a 100644 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -1,3 +1,8 @@ +import pytest + +from sqlite_utils import Database + + def test_insert_conversion(fresh_db): table = fresh_db.table("table") table.insert({"foo": "bar"}, conversions={"foo": "upper(?)"}) @@ -28,6 +33,64 @@ def test_upsert_all_conversion(fresh_db): assert [{"id": 1, "foo": "BAR"}] == list(table.rows) +@pytest.mark.parametrize("use_old_upsert", (False, True)) +@pytest.mark.parametrize("method", ("upsert", "upsert_all")) +@pytest.mark.parametrize( + "first,second,conversion,expected_first,expected_second", + ( + (10, 12, "? * 100", 1000, 1200), + ("first", "second", "? || '-stored'", "first-stored", "second-stored"), + ( + '{"name":"first"}', + '{"name":"second"}', + "json_extract(?, '$.name')", + "first", + "second", + ), + ("first", "second", "? || '?'", "first?", "second?"), + ), + ids=("multiply", "append", "json-extract", "literal-question-mark"), +) +def test_upsert_conversion_once( + use_old_upsert, method, first, second, conversion, expected_first, expected_second +): + db = Database(memory=True, use_old_upsert=use_old_upsert) + table = db["records"] + for value, expected in ( + (first, expected_first), + (second, expected_second), + (second, expected_second), + ): + record = {"id": 1, "value": value} + getattr(table, method)( + record if method == "upsert" else [record], + pk="id", + conversions={"value": conversion}, + ) + assert table.get(1) == {"id": 1, "value": expected} + + +@pytest.mark.parametrize("use_old_upsert", (False, True)) +@pytest.mark.parametrize("batch_size", (1, 100)) +def test_upsert_all_converted_values_mixed_insert_update(use_old_upsert, batch_size): + db = Database(memory=True, use_old_upsert=use_old_upsert) + table = db["prices"] + table.insert({"id": 1, "amount": 1000, "note": "original"}, pk="id") + table.upsert_all( + [ + {"id": 1, "amount": 12, "note": "updated"}, + {"id": 2, "amount": 20, "note": "new"}, + ], + pk="id", + conversions={"amount": "? * 100"}, + batch_size=batch_size, + ) + assert list(table.rows_where(order_by="id")) == [ + {"id": 1, "amount": 1200, "note": "updated"}, + {"id": 2, "amount": 2000, "note": "new"}, + ] + + def test_update_conversion(fresh_db): table = fresh_db.table("table") table.insert({"id": 5, "foo": "bar"}, pk="id") From e16d89d87dae0c0daeeb419f0acb7a8b4b829a1a Mon Sep 17 00:00:00 2001 From: sanidhyasaluja Date: Sun, 15 Nov 2026 05:31:54 +0530 Subject: [PATCH 2/2] Clarify legacy primary-key conversion exception, refs #947 --- docs/python-api.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/python-api.rst b/docs/python-api.rst index 1ce2b1c8f..86859e314 100644 --- a/docs/python-api.rst +++ b/docs/python-api.rst @@ -2346,7 +2346,7 @@ You can specify an upper case conversion for a specific column like so: The dictionary key is the column name to be converted. The value is the SQL fragment to use, with a ``?`` placeholder for the original value. -Conversions are applied once to each incoming value. With ``upsert()`` and ``upsert_all()``, this is the same whether a row is inserted or updated. +With ``upsert()`` and ``upsert_all()``, conversions are applied once to each incoming non-primary-key value, whether a row is inserted or updated. The legacy upsert implementation (``use_old_upsert=True``) ignores conversions on primary-key columns. A more useful example: if you are working with `SpatiaLite `__ you may find yourself wanting to create geometry values from a WKT value. Code to do that could look like this: