From c0e30df89b859047f7bf30896af84efc2d600afc Mon Sep 17 00:00:00 2001 From: Rami Abdelrazzaq Date: Sat, 7 Mar 2026 11:47:10 -0600 Subject: [PATCH] fix: avoid detect-types transform crash on header-only CSV Fixes simonw/sqlite-utils#702 --- sqlite_utils/cli.py | 2 +- tests/test_cli.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index 9b9ee20e..9c7022a5 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -1176,7 +1176,7 @@ def insert_upsert_implementation( ) else: raise - if tracker is not None: + if tracker is not None and db.table(table).exists(): db.table(table).transform(types=tracker.types) # Clean up open file-like objects diff --git a/tests/test_cli.py b/tests/test_cli.py index 40c3595c..f519257b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -2305,6 +2305,21 @@ def test_insert_detect_types(tmpdir, option): ] +def test_insert_detect_types_header_only_csv(tmpdir): + db_path = str(tmpdir / "test.db") + data = "name,age\n" + + result = CliRunner().invoke( + cli.cli, + ["insert", db_path, "creatures", "-", "--csv", "--detect-types"], + catch_exceptions=False, + input=data, + ) + assert result.exit_code == 0 + db = Database(db_path) + assert not db["creatures"].exists() + + @pytest.mark.parametrize("option", (None, "-d", "--detect-types")) def test_upsert_detect_types(tmpdir, option): """Test that type detection is now the default behavior for upsert"""