diff --git a/migrations/20171024122359_add_article_fts_table/up.sql b/migrations/20171024122359_add_article_fts_table/up.sql index cd5ea3d..76a1622 100644 --- a/migrations/20171024122359_add_article_fts_table/up.sql +++ b/migrations/20171024122359_add_article_fts_table/up.sql @@ -6,20 +6,20 @@ CREATE VIRTUAL TABLE article_search USING fts5( ); 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; 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 != 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; 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 != 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; 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); END; diff --git a/migrations/20171025121618_fix_article_search_triggers/down.sql b/migrations/20171025121618_fix_article_search_triggers/down.sql new file mode 100644 index 0000000..e69de29 diff --git a/migrations/20171025121618_fix_article_search_triggers/up.sql b/migrations/20171025121618_fix_article_search_triggers/up.sql new file mode 100644 index 0000000..0ec17cf --- /dev/null +++ b/migrations/20171025121618_fix_article_search_triggers/up.sql @@ -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;