Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sqlite_utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -2033,7 +2033,7 @@ def memory(
rows = (_flatten(row) for row in rows)

db.table(file_table).insert_all(rows, alter=True)
if tracker is not None:
if tracker is not None and db.table(file_table).exists():
db.table(file_table).transform(types=tracker.types)
# Add convenient t / t1 / t2 views
view_names = ["t{}".format(i + 1)]
Expand Down
16 changes: 16 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2305,6 +2305,22 @@ def test_insert_detect_types(tmpdir, option):
]


def test_insert_detect_types_empty_csv(tmpdir):
"""Header-only CSV should not crash with --detect-types (issue #702)."""
db_path = str(tmpdir / "test.db")
data = "name,age,weight\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 "creatures" not in db.table_names()


@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"""
Expand Down
Loading