Skip to content

fix: roll back a failed .import - #284

Open
Rajvira10 wants to merge 1 commit into
dbcli:mainfrom
Rajvira10:fix-import-rollback
Open

Rajvira10 wants to merge 1 commit into
dbcli:mainfrom
Rajvira10:fix-import-rollback

Conversation

@Rajvira10

Copy link
Copy Markdown

Description

.import runs its inserts inside BEGIN ... COMMIT, but if one insert fails (for example a UNIQUE or NOT NULL violation), the exception skips the COMMIT and the transaction stays open.

Before:

litecli> create table t(id integer primary key, name text);
litecli> .import dup.csv t          -- dup.csv: "1,a" and "1,b"
UNIQUE constraint failed: t.id
litecli> select * from t;           -- the half-imported row is visible but uncommitted
1 | a
litecli> .import dup.csv t
cannot start a transaction within a transaction

The session also keeps holding its write lock, so other connections can't write to the database until the user notices and runs ROLLBACK by hand.

After: a failed import is rolled back before the error is raised. The table is left as it was, and the next statement or .import works normally.

The regression test in tests/test_main.py imports a CSV with a duplicate key. It checks that no transaction is left open and no rows are kept, then checks that a later import succeeds.

Checklist

  • I've added this contribution to the CHANGELOG.md file.

This branch has not been deployed

No deployments
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