const CREATE_MAIN_TRIGGERS_SQL: &str = "-- This Source Code Form is subject to the terms of the Mozilla Public\n-- License, v. 2.0. If a copy of the MPL was not distributed with this\n-- file, You can obtain one at http://mozilla.org/MPL/2.0/.\n\n-- This file defines triggers for the main read-write connection.\n\n-- Unlike history, we manage bookmark tombstones via triggers. We do this\n-- because we rely on foreign-keys to auto-remove children of a deleted folder.\nCREATE TEMP TRIGGER moz_create_bookmarks_deleted_trigger\nAFTER DELETE ON moz_bookmarks\nFOR EACH ROW WHEN OLD.syncStatus = 2 -- SyncStatus::Normal\nBEGIN\n INSERT into moz_bookmarks_deleted VALUES (OLD.guid, now());\nEND;\n\n-- Updating the guid is only allowed by Sync, and it will use a connection\n-- without some of these triggers - so for now we prevent changing the guid\n-- of an existing item.\nCREATE TEMP TRIGGER moz_remove_bookmarks_deleted_update_trigger\nAFTER UPDATE ON moz_bookmarks\nFOR EACH ROW WHEN OLD.guid != NEW.guid\nBEGIN\n SELECT RAISE(FAIL, \'guids are immutable\');\nEND;\n\n-- These triggers bump the Sync change counter for all affected bookmarks when\n-- a URL is tagged or untagged.\nCREATE TEMP TRIGGER moz_tags_relations_afterinsert_sync_trigger\nAFTER INSERT ON moz_tags_relation\nBEGIN\n UPDATE moz_bookmarks SET\n syncChangeCounter = syncChangeCounter + 1\n WHERE fk = NEW.place_id;\nEND;\n\nCREATE TEMP TRIGGER moz_tags_relations_afterupdate_sync_trigger\nAFTER UPDATE ON moz_tags_relation\nBEGIN\n UPDATE moz_bookmarks SET\n syncChangeCounter = syncChangeCounter + 1\n WHERE fk IN (OLD.place_id, NEW.place_id);\nEND;\n\nCREATE TEMP TRIGGER moz_tags_relations_afterdelete_sync_trigger\nAFTER DELETE ON moz_tags_relation\nBEGIN\n UPDATE moz_bookmarks SET\n syncChangeCounter = syncChangeCounter + 1\n WHERE fk = OLD.place_id;\nEND;\n\n-- Our \"global\" sync change counter.\n-- It\'s \"global\" in the sense that it applies to all bookmarks across all\n-- connections to the same DB.\nCREATE TEMP TRIGGER moz_bookmarks_gscc_after_insert\nAFTER INSERT ON moz_bookmarks FOR EACH ROW\nBEGIN\n SELECT note_bookmarks_sync_change() WHERE NEW.syncChangeCounter > 0;\nEND;\n\nCREATE TEMP TRIGGER moz_bookmarks_gscc_after_update\nAFTER UPDATE OF fk, syncChangeCounter ON moz_bookmarks FOR EACH ROW\nBEGIN\n SELECT note_bookmarks_sync_change()\n WHERE NEW.syncChangeCounter <> OLD.syncChangeCounter;\nEND;\n\n-- Note that this will not capture deletions of bookmarks with a sync status of\n-- \"New\" (because we don\'t tombstone them) whereas the other triggers above do,\n-- but that\'s fine for this use-case, and we want the trigger on\n-- moz_bookmarks_deleted to stay as close as possible to desktop.\nCREATE TEMP TRIGGER moz_bookmarks_gscc_after_delete\nAFTER INSERT ON moz_bookmarks_deleted FOR EACH ROW\nBEGIN\n SELECT note_bookmarks_sync_change();\nEND;\n";