Skip to content

Add --summary option to insert and upsert (refs #516) - #980

Open
shamilhabeebnelli wants to merge 4 commits into
simonw:mainfrom
shamilhabeebnelli:shamilhabeebnelli-patch-1
Open

shamilhabeebnelli wants to merge 4 commits into
simonw:mainfrom
shamilhabeebnelli:shamilhabeebnelli-patch-1

Conversation

@shamilhabeebnelli

@shamilhabeebnelli shamilhabeebnelli commented Dec 1, 2026 •

Copy link
Copy Markdown

Refs #516.

Adds an opt-in --summary flag to sqlite-utils insert and sqlite-utils upsert. It writes a one-line count to standard error, for file input and stdin alike:

  • plain insert: 2 rows inserted
  • --ignore: 1 row inserted, 1 ignored
  • --replace: 0 rows inserted, 2 replaced
  • upsert: 0 rows inserted, 2 updated

Nothing changes unless --summary is passed, so existing output and scripts are unaffected. The counts come from the table row count before and after the insert compared with the number of input rows, so --truncate and tables created by the command report correctly. bulk is not touched.

Changes: sqlite_utils/cli.py (option, row counter, _insert_summary), 4 new tests in tests/test_cli_insert.py (insert, ignore, replace, upsert, truncate with CSV, and no output without the flag), a docs section in docs/cli.rst, and the regenerated docs/cli-reference.rst via cog.

Test output on this branch (Python 3.10, SQLite from the system Python):

$ pytest tests/test_cli_insert.py -q -k summary
6 passed, 69 deselected
$ pytest tests -q
1633 passed, 20 skipped

Real CLI run from the branch:

$ sqlite-utils insert s.db t a.json --pk id --summary
2 rows inserted
$ sqlite-utils insert s.db t b.json --pk id --ignore --summary
1 row inserted, 1 ignored
$ sqlite-utils insert s.db t b.json --pk id --replace --summary
0 rows inserted, 2 replaced
$ sqlite-utils upsert s.db t b.json --pk id --summary
0 rows inserted, 2 updated
$ cat b.json | sqlite-utils insert s.db t - --pk id --ignore --summary
0 rows inserted, 2 ignored

(a.json holds ids 1 and 2, b.json holds ids 2 and 3.) Only the Python 3.10 environment above was tested; I did not run the other CI matrix versions. The flag name --summary follows the suggestion in the issue thread; happy to rename it.

With regards, Shamil Nelli, denelli.in


📚 Documentation preview 📚: https://sqlite-utils--980.org.readthedocs.build/en/980/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant