Skip to content

fix: Update SQLITE3 dialect to allow for OnConflictUpdateWhere#440

Open
loganintech wants to merge 2 commits into
doug-martin:masterfrom
loganintech:master
Open

fix: Update SQLITE3 dialect to allow for OnConflictUpdateWhere#440
loganintech wants to merge 2 commits into
doug-martin:masterfrom
loganintech:master

Conversation

@loganintech

Copy link
Copy Markdown

These days SQLITE3 supports WHERE clauses inside of a conflict DO UPDATE. This PR changes the SQLITE3 dialect to support that.

https://www.sqlite.org/lang_upsert.html

Relevant docs:

The only use for the WHERE clause at the end of the DO UPDATE is to optionally change the DO UPDATE into a no-op depending on the original and/or new values. For example:

    CREATE TABLE phonebook2(
      name TEXT PRIMARY KEY,
      phonenumber TEXT,
      validDate DATE
    );
    INSERT INTO phonebook2(name,phonenumber,validDate)
      VALUES('Alice','704-555-1212','2018-05-08')
      ON CONFLICT(name) DO UPDATE SET
        phonenumber=excluded.phonenumber,
        validDate=excluded.validDate
      WHERE excluded.validDate>phonebook2.validDate;

In this last example, the phonebook2 entry is only updated if the validDate for the newly inserted value is newer than the entry already in the table. If the table already contains an entry with the same name and a current validDate, then the WHERE clause causes the DO UPDATE to become a no-op. 

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