Skip to content
Closed
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
11 changes: 8 additions & 3 deletions sqlite_utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4779,6 +4779,8 @@ def insert_all(
try:
first_record = next(records_iter)
except StopIteration:
if truncate:
self.delete_where()
return self # It was an empty list

# Check if this is list mode or dict mode
Expand All @@ -4796,6 +4798,8 @@ def insert_all(
try:
first_record = next(records_iter)
except StopIteration:
if truncate:
self.delete_where()
return self # Only headers, no data
if not isinstance(first_record, (list, tuple)):
raise ValueError( # noqa: TRY004
Expand All @@ -4807,6 +4811,8 @@ def insert_all(
try:
first_record = next(records_iter)
except StopIteration:
if truncate:
self.delete_where()
return self
first_record = cast(dict[str, Any], first_record)
num_columns = len(first_record.keys())
Expand All @@ -4820,9 +4826,8 @@ def insert_all(
)
self.last_rowid = None
self.last_pk = None
if truncate and self.exists():
with self.db.atomic():
self.db.execute(f"DELETE FROM {quote_identifier(self.name)};")
if truncate:
self.delete_where()
result = None
for chunk in chunks(itertools.chain([first_record], records_iter), batch_size):
chunk = list(chunk)
Expand Down
24 changes: 24 additions & 0 deletions tests/test_cli_insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,30 @@ def test_insert_truncate(db_path):
] == list(db.query("select foo, n from from_json_nl"))


def test_insert_truncate_empty_json(db_path):
db = Database(db_path)
db["from_json"].insert({"id": 1})
result = CliRunner().invoke(
cli.cli,
["insert", db_path, "from_json", "-", "--truncate"],
input="[]",
)
assert result.exit_code == 0, result.output
assert db["from_json"].count == 0


def test_insert_truncate_header_only_csv(db_path):
db = Database(db_path)
db["from_csv"].insert({"id": 1, "name": "Cleo"})
result = CliRunner().invoke(
cli.cli,
["insert", db_path, "from_csv", "-", "--csv", "--truncate"],
input="id,name\n",
)
assert result.exit_code == 0, result.output
assert db["from_csv"].count == 0


def test_insert_alter(db_path, tmpdir):
result = CliRunner().invoke(
cli.cli,
Expand Down
24 changes: 24 additions & 0 deletions tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,30 @@ def test_insert_all_empty_list(fresh_db):
assert fresh_db.table("t").count == 1


def test_insert_all_empty_list_truncate(fresh_db):
table = fresh_db.table("t")
table.insert({"foo": 1})
table.insert_all([], truncate=True)
assert table.count == 0
assert table.columns_dict == {"foo": int}


def test_insert_all_empty_generator_truncate(fresh_db):
table = fresh_db.table("t")
table.insert({"foo": 1})
table.insert_all(iter([]), truncate=True)
assert table.count == 0
assert table.columns_dict == {"foo": int}


def test_insert_all_header_only_list_truncate(fresh_db):
table = fresh_db.table("t")
table.insert({"foo": 1})
table.insert_all([["foo"]], truncate=True)
assert table.count == 0
assert table.columns_dict == {"foo": int}


def test_insert_all_single_column(fresh_db):
table = fresh_db.table("table")
table.insert_all([{"name": "Cleo"}], pk="name")
Expand Down
Loading