From 29b833d181f4af794ad3eed96299de9d7d448259 Mon Sep 17 00:00:00 2001 From: peterolkhov Date: Tue, 10 Nov 2026 16:50:19 -0500 Subject: [PATCH 1/4] Honor truncate for empty imports --- docs/cli.rst | 2 ++ docs/python-api.rst | 2 ++ sqlite_utils/db.py | 4 ++++ tests/test_cli_insert.py | 22 ++++++++++++++++++++ tests/test_create.py | 44 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 74 insertions(+) diff --git a/docs/cli.rst b/docs/cli.rst index 78c33b8ae..9f1693e86 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -1195,6 +1195,8 @@ You can delete all the existing rows in the table before inserting the new recor sqlite-utils insert dogs.db dogs dogs.json --truncate +Valid empty input, such as a JSON ``[]`` or a CSV file containing only a header row, will also delete all existing rows when ``--truncate`` is used. The table's schema and indexes are preserved. If the table does not exist, empty input does not create it. + You can add the ``--analyze`` option to run ``ANALYZE`` against the table after the rows have been inserted. .. note:: diff --git a/docs/python-api.rst b/docs/python-api.rst index d515642a5..34276f46c 100644 --- a/docs/python-api.rst +++ b/docs/python-api.rst @@ -1105,6 +1105,8 @@ You can skip inserting any records that have a primary key that already exists u You can delete all the existing rows in the table before inserting the new records using ``truncate=True``. This is useful if you want to replace the data in the table. +An empty iterable, or a list or tuple iterator containing only column names, will also delete all existing rows when ``truncate=True``. The table's schema and indexes are preserved. If the table does not exist, empty input does not create it. + Pass ``analyze=True`` to run ``ANALYZE`` against the table after inserting the new records. .. note:: diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index c011d9bb0..f0b625fb3 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -4779,6 +4779,8 @@ def insert_all( try: first_record = next(records_iter) except StopIteration: + if truncate: + return self.delete_where() return self # It was an empty list # Check if this is list mode or dict mode @@ -4796,6 +4798,8 @@ def insert_all( try: first_record = next(records_iter) except StopIteration: + if truncate: + return self.delete_where() return self # Only headers, no data if not isinstance(first_record, (list, tuple)): raise ValueError( # noqa: TRY004 diff --git a/tests/test_cli_insert.py b/tests/test_cli_insert.py index 011786259..6995a8f95 100644 --- a/tests/test_cli_insert.py +++ b/tests/test_cli_insert.py @@ -354,6 +354,28 @@ def test_insert_truncate(db_path): ] == list(db.query("select foo, n from from_json_nl")) +@pytest.mark.parametrize( + "input, options", + (("[]", []), ("id,name\n", ["--csv"]), ("id\tname\n", ["--tsv"]), ("", ["--nl"])), +) +def test_insert_truncate_empty(tmp_path, input, options): + db_path = str(tmp_path / "items.db") + db = Database(db_path) + table = db["items"].insert({"id": 1, "name": "old"}, pk="id") + table.create_index(["name"]) + schema, indexes = table.schema, table.indexes + db.close() + + result = CliRunner().invoke( + cli.cli, ["insert", db_path, "items", "-", "--truncate"] + options, input=input + ) + assert result.exit_code == 0, result.output + with Database(db_path) as reopened: + assert list(reopened["items"].rows) == [] + assert reopened["items"].schema == schema + assert reopened["items"].indexes == indexes + + def test_insert_alter(db_path, tmpdir): result = CliRunner().invoke( cli.cli, diff --git a/tests/test_create.py b/tests/test_create.py index b738df952..b8e070d2a 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -1303,6 +1303,50 @@ def test_insert_all_empty_list(fresh_db): assert fresh_db.table("t").count == 1 +@pytest.mark.parametrize("records", ([], iter(()), [["id", "name"]], [("id", "name")])) +@pytest.mark.parametrize("truncate", (False, True)) +def test_insert_all_empty_truncate(tmp_path, records, truncate): + db_path = tmp_path / "items.db" + db = Database(db_path) + table = db["items"].insert({"id": 1, "name": "old"}, pk="id") + table.create_index(["name"]) + schema, indexes = table.schema, table.indexes + + assert table.insert_all(records, truncate=truncate) is table + assert table.schema == schema + assert table.indexes == indexes + db.close() + with Database(db_path) as reopened: + assert list(reopened["items"].rows) == ( + [] if truncate else [{"id": 1, "name": "old"}] + ) + + +@pytest.mark.parametrize("records", ([], iter(()), [["id", "name"]])) +def test_insert_all_empty_truncate_missing_table(tmp_path, records): + db = Database(tmp_path / "items.db") + table = db["items"] + assert table.insert_all(records, truncate=True) is table + assert not table.exists() + + +@pytest.mark.parametrize( + "records, error", + ( + (None, TypeError), + ([None], AttributeError), + ([[1]], ValueError), + ([["id"], None], ValueError), + ), +) +def test_insert_all_truncate_invalid_input(tmp_path, records, error): + db = Database(tmp_path / "items.db") + table = db["items"].insert({"id": 1, "name": "old"}, pk="id") + with pytest.raises(error): + table.insert_all(records, truncate=True) + assert list(table.rows) == [{"id": 1, "name": "old"}] + + def test_insert_all_single_column(fresh_db): table = fresh_db.table("table") table.insert_all([{"name": "Cleo"}], pk="name") From f0a6fc320a4f60ad8b0b53f9a0d966531795422d Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 29 Nov 2026 14:03:28 -0800 Subject: [PATCH 2/4] Use db.table() in the new truncate tests to satisfy pyright Co-Authored-By: Claude Fable 5.2 --- tests/test_cli_insert.py | 8 ++++---- tests/test_create.py | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/test_cli_insert.py b/tests/test_cli_insert.py index 56f49ba2e..922750a80 100644 --- a/tests/test_cli_insert.py +++ b/tests/test_cli_insert.py @@ -376,7 +376,7 @@ def test_insert_truncate(db_path): def test_insert_truncate_empty(tmp_path, input, options): db_path = str(tmp_path / "items.db") db = Database(db_path) - table = db["items"].insert({"id": 1, "name": "old"}, pk="id") + table = db.table("items").insert({"id": 1, "name": "old"}, pk="id") table.create_index(["name"]) schema, indexes = table.schema, table.indexes db.close() @@ -386,9 +386,9 @@ def test_insert_truncate_empty(tmp_path, input, options): ) assert result.exit_code == 0, result.output with Database(db_path) as reopened: - assert list(reopened["items"].rows) == [] - assert reopened["items"].schema == schema - assert reopened["items"].indexes == indexes + assert list(reopened.table("items").rows) == [] + assert reopened.table("items").schema == schema + assert reopened.table("items").indexes == indexes def test_insert_alter(db_path, tmpdir): diff --git a/tests/test_create.py b/tests/test_create.py index c80dd1703..fcd9eee34 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -1308,7 +1308,7 @@ def test_insert_all_empty_list(fresh_db): def test_insert_all_empty_truncate(tmp_path, records, truncate): db_path = tmp_path / "items.db" db = Database(db_path) - table = db["items"].insert({"id": 1, "name": "old"}, pk="id") + table = db.table("items").insert({"id": 1, "name": "old"}, pk="id") table.create_index(["name"]) schema, indexes = table.schema, table.indexes @@ -1325,7 +1325,7 @@ def test_insert_all_empty_truncate(tmp_path, records, truncate): @pytest.mark.parametrize("records", ([], iter(()), [["id", "name"]])) def test_insert_all_empty_truncate_missing_table(tmp_path, records): db = Database(tmp_path / "items.db") - table = db["items"] + table = db.table("items") assert table.insert_all(records, truncate=True) is table assert not table.exists() @@ -1341,7 +1341,7 @@ def test_insert_all_empty_truncate_missing_table(tmp_path, records): ) def test_insert_all_truncate_invalid_input(tmp_path, records, error): db = Database(tmp_path / "items.db") - table = db["items"].insert({"id": 1, "name": "old"}, pk="id") + table = db.table("items").insert({"id": 1, "name": "old"}, pk="id") with pytest.raises(error): table.insert_all(records, truncate=True) assert list(table.rows) == [{"id": 1, "name": "old"}] From 2012e095f6be6c546c35f9c769686fc7989c76f1 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 29 Nov 2026 14:04:39 -0800 Subject: [PATCH 3/4] Copy tweak --- 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 85a4b4d41..78f73f2cc 100644 --- a/docs/python-api.rst +++ b/docs/python-api.rst @@ -1105,7 +1105,7 @@ You can skip inserting any records that have a primary key that already exists u You can delete all the existing rows in the table before inserting the new records using ``truncate=True``. This is useful if you want to replace the data in the table. -An empty iterable, or a list or tuple iterator containing only column names, will also delete all existing rows when ``truncate=True``. The table's schema and indexes are preserved. If the table does not exist, empty input does not create it. +An empty iterable, or a list or tuple iterator containing only column names, will also delete all existing rows when ``truncate=True``. Pass ``analyze=True`` to run ``ANALYZE`` against the table after inserting the new records. From be43db7138a2eccf5c80f180359f769404dee990 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 29 Nov 2026 14:05:39 -0800 Subject: [PATCH 4/4] Removed explanation --- docs/cli.rst | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/cli.rst b/docs/cli.rst index 9f1693e86..78c33b8ae 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -1195,8 +1195,6 @@ You can delete all the existing rows in the table before inserting the new recor sqlite-utils insert dogs.db dogs dogs.json --truncate -Valid empty input, such as a JSON ``[]`` or a CSV file containing only a header row, will also delete all existing rows when ``--truncate`` is used. The table's schema and indexes are preserved. If the table does not exist, empty input does not create it. - You can add the ``--analyze`` option to run ``ANALYZE`` against the table after the rows have been inserted. .. note::