Fix triggers for keeping search index up to date

This commit is contained in:
Magnus Hoff 2017-10-25 14:19:33 +02:00
parent 332e4a8a1f
commit b90d7b9d1e
3 changed files with 29 additions and 5 deletions

View file

@ -6,20 +6,20 @@ CREATE VIRTUAL TABLE article_search USING fts5(
); );
INSERT INTO article_search(title, body, slug) INSERT INTO article_search(title, body, slug)
SELECT title, body, slug FROM article_revisions WHERE latest != 0; SELECT title, body, slug FROM article_revisions WHERE latest = 1;
CREATE TRIGGER article_revisions_ai AFTER INSERT ON article_revisions WHEN new.latest != 0 BEGIN CREATE TRIGGER article_revisions_ai AFTER INSERT ON article_revisions WHEN new.latest = 1 BEGIN
DELETE FROM article_search WHERE rowid = new.article_id; DELETE FROM article_search WHERE rowid = new.article_id;
INSERT INTO article_search(rowid, title, body, slug) VALUES (new.article_id, new.title, new.body, new.slug); INSERT INTO article_search(rowid, title, body, slug) VALUES (new.article_id, new.title, new.body, new.slug);
END; END;
CREATE TRIGGER article_revisions_ad AFTER DELETE ON article_revisions WHEN old.latest != 0 BEGIN CREATE TRIGGER article_revisions_ad AFTER DELETE ON article_revisions WHEN old.latest = 1 BEGIN
DELETE FROM article_search WHERE rowid = old.article_id; DELETE FROM article_search WHERE rowid = old.article_id;
END; END;
-- Index unique_latest_revision_per_article_id makes sure the following is sufficient: -- Index unique_latest_revision_per_article_id makes sure the following is sufficient:
CREATE TRIGGER article_revisions_au_disable AFTER UPDATE ON article_revisions WHEN old.latest != 0 AND new.latest == 0 BEGIN CREATE TRIGGER article_revisions_au_disable AFTER UPDATE ON article_revisions WHEN old.latest = 1 AND new.latest = 0 BEGIN
DELETE FROM article_search WHERE rowid = old.article_id; DELETE FROM article_search WHERE rowid = old.article_id;
END; END;
CREATE TRIGGER article_revisions_au_enable AFTER UPDATE ON article_revisions WHEN old.latest == 0 AND new.latest != 0 BEGIN CREATE TRIGGER article_revisions_au_enable AFTER UPDATE ON article_revisions WHEN old.latest = 0 AND new.latest = 1 BEGIN
INSERT INTO article_search(rowid, title, body, slug) VALUES (new.article_id, new.title, new.body, new.slug); INSERT INTO article_search(rowid, title, body, slug) VALUES (new.article_id, new.title, new.body, new.slug);
END; END;

View file

@ -0,0 +1,24 @@
DROP TRIGGER article_revisions_ai;
DROP TRIGGER article_revisions_ad;
DROP TRIGGER article_revisions_au_disable;
DROP TRIGGER article_revisions_au_enable;
CREATE TRIGGER article_revisions_ai AFTER INSERT ON article_revisions WHEN new.latest = 1 BEGIN
DELETE FROM article_search WHERE rowid = new.article_id;
INSERT INTO article_search(rowid, title, body, slug) VALUES (new.article_id, new.title, new.body, new.slug);
END;
CREATE TRIGGER article_revisions_ad AFTER DELETE ON article_revisions WHEN old.latest = 1 BEGIN
DELETE FROM article_search WHERE rowid = old.article_id;
END;
-- Index unique_latest_revision_per_article_id makes sure the following is sufficient:
CREATE TRIGGER article_revisions_au_disable AFTER UPDATE ON article_revisions WHEN old.latest = 1 AND new.latest = 0 BEGIN
DELETE FROM article_search WHERE rowid = old.article_id;
END;
CREATE TRIGGER article_revisions_au_enable AFTER UPDATE ON article_revisions WHEN old.latest = 0 AND new.latest = 1 BEGIN
INSERT INTO article_search(rowid, title, body, slug) VALUES (new.article_id, new.title, new.body, new.slug);
END;
DELETE FROM article_search;
INSERT INTO article_search(title, body, slug)
SELECT title, body, slug FROM article_revisions WHERE latest = 1;