Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Queries impacted by UPDATE triggers are not included in listener callouts by UPSERT statements. #2198

Closed
andersio opened this issue Mar 17, 2021 · 0 comments · Fixed by #2222
Labels

Comments

@andersio
Copy link
Contributor

andersio commented Mar 17, 2021

Build Environment
SQLDelight version: 1.4.4
OS: iOS
Kotlin version: 1.4.31

Describe the bug
Given these tables and this AFTER UPDATE trigger:

CREATE TABLE Movie (
    id INTEGER PRIMARY KEY NOT NULL, 
    name TEXT NOT NULL, 
    companyId INTEGER NOT NULL REFERENCES Company("id"), 
    rating INTEGER NOT NULL
);

CREATE TABLE Company (
    id INTEGER PRIMARY KEY NOT NULL,
    name TEXT NOT NULL,
    rating_total INTEGER NOT NULL
);

CREATE TRIGGER IF NOT EXISTS aggregate_ratings
AFTER UPDATE ON Movie
BEGIN
  UPDATE Company
  SET rating_total = rating_total + (new.rating - old.rating)
  WHERE Company.id = new.companyId;
END;

and these statements:

upsert:
INSERT INTO Movie (id, name, companyId, rating) VALUES (?, ?, ?, ?)
ON CONFLICT (id)
DO UPDATE SET rating = excluded.rating;

getAllRatings:
SELECT id, rating_total FROM Company;

getAllRatings() is impacted by an AFTER UPDATE trigger, which can be fired by upsert(). But the synthesized implementation of upsert() does not include any listener callout code.

  public override fun upsert(
    id: Long?,
    name: String,
    companyId: Long,
    rating: Long
  ): Unit {
    driver.execute(1180485434, """
    |INSERT INTO Movie (id, name, companyId, rating) VALUES (?, ?, ?, ?)
    |ON CONFLICT (id)
    |DO UPDATE SET rating = excluded.rating
    """.trimMargin(), 4,
      binders = {
      bindLong(1, id)
      bindString(2, name)
      bindLong(3, companyId)
      bindLong(4, rating)
    }
    )
  }

It seems like the compiler only looks through INSERT triggers for queries being impacted by an UPSERT statements, but not walking through the UPDATE triggers too.

@andersio andersio added the bug label Mar 17, 2021
@andersio andersio changed the title Queries impacted by AFTER UPDATE triggers are not included in listener callouts by UPSERT statements. Queries impacted by UPDATE triggers are not included in listener callouts by UPSERT statements. Mar 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant