Do not use INSERT OR REPLACE in SQLite. #620

Merged
crodas merged 1 commit from feat/sqlite-drop-replace into main 2025-02-28 11:35:30 +00:00
crodas commented 2025-02-27 23:32:57 +00:00 (Migrated from github.com)

Description

Instead, use INSERT and ON CONFLICT. The reason is that in case of conflicts, the REPLACE will trigger a DELETE and then perform an INSERT, as outlined in the documentation[1], and that may cause a cascade of deletion due to our FOREIGN KEYs.

Here is the official documentation:

When a UNIQUE or PRIMARY KEY constraint violation occurs, the REPLACE algorithm
deletes pre-existing rows that are causing the constraint violation prior to
inserting or updating the current row and the command continues executing
normally. If a NOT NULL constraint violation occurs, the REPLACE conflict
resolution replaces the NULL value with the default value for that column, or
if the column has no default value, then the ABORT algorithm is used. If
a CHECK constraint or foreign key constraint violation occurs, the REPLACE
conflict resolution algorithm works like ABORT.  When the REPLACE conflict
resolution strategy deletes rows in order to satisfy a constraint, delete
triggers fire if and only if recursive triggers are enabled.  The update
hook is not invoked for rows that are deleted by the REPLACE conflict
resolution strategy. Nor does REPLACE increment the change counter. The
exceptional behaviors defined in this paragraph might change in a future
release.

[1] https://www.sqlite.org/lang_conflict.html


Notes to the reviewers


Suggested CHANGELOG Updates

CHANGED

ADDED

REMOVED

FIXED


Checklist

### Description Instead, use `INSERT` and `ON CONFLICT`. The reason is that in case of conflicts, the `REPLACE` will trigger a DELETE and then perform an INSERT, as outlined in the documentation[1], and that may cause a cascade of deletion due to our FOREIGN KEYs. Here is the official documentation: ``` When a UNIQUE or PRIMARY KEY constraint violation occurs, the REPLACE algorithm deletes pre-existing rows that are causing the constraint violation prior to inserting or updating the current row and the command continues executing normally. If a NOT NULL constraint violation occurs, the REPLACE conflict resolution replaces the NULL value with the default value for that column, or if the column has no default value, then the ABORT algorithm is used. If a CHECK constraint or foreign key constraint violation occurs, the REPLACE conflict resolution algorithm works like ABORT. When the REPLACE conflict resolution strategy deletes rows in order to satisfy a constraint, delete triggers fire if and only if recursive triggers are enabled. The update hook is not invoked for rows that are deleted by the REPLACE conflict resolution strategy. Nor does REPLACE increment the change counter. The exceptional behaviors defined in this paragraph might change in a future release. ``` [1] https://www.sqlite.org/lang_conflict.html ----- ### Notes to the reviewers <!-- In this section you can include notes directed to the reviewers, like explaining why some parts of the PR were done in a specific way --> ----- ### Suggested [CHANGELOG](https://github.com/cashubtc/cdk/blob/main/CHANGELOG.md) Updates <!-- Please do not edit the actual changelog but note what you changed here. --> #### CHANGED #### ADDED #### REMOVED #### FIXED ---- ### Checklist * [x] I followed the [code style guidelines](https://github.com/cashubtc/cdk/blob/main/CODE_STYLE.md) * [x] I ran `just final-check` before committing
thesimplekid (Migrated from github.com) approved these changes 2025-02-28 11:35:18 +00:00
thesimplekid (Migrated from github.com) left a comment

Thank you. LGTM

Thank you. LGTM
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cashubtc/cdk!620
No description provided.