[][src]Constant places::db::schema::CREATE_SYNC_TRIGGERS_SQL

const CREATE_SYNC_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 Sync connection.\n\n--- Pushes uploaded changes back to the local and remote trees. This is more\n--- or less equivalent to Desktop\'s `PlacesSyncUtils.bookmarks.pushChanges`.\nCREATE TEMP TRIGGER pushUploadedChanges\nAFTER UPDATE OF uploadedAt ON itemsToUpload WHEN NEW.uploadedAt > -1\nBEGIN\n    -- Reduce the change counter and update the sync status for uploaded items.\n    -- If the item was uploaded during the sync, its change counter will still\n    -- be > 0 for the next sync.\n    UPDATE moz_bookmarks SET\n        syncChangeCounter = max(syncChangeCounter - NEW.syncChangeCounter, 0),\n        syncStatus = 2 -- SyncStatus::Normal\n    WHERE guid = NEW.guid;\n\n    -- Remove uploaded tombstones.\n    DELETE FROM moz_bookmarks_deleted\n    WHERE guid = NEW.guid;\n\n    -- Write the uploaded item back to the synced bookmarks table, to match\n    -- what\'s on the server now.\n    REPLACE INTO moz_bookmarks_synced(guid, parentGuid, serverModified, needsMerge,\n                                      validity, isDeleted, kind, dateAdded, title,\n                                      placeId, keyword)\n    VALUES(NEW.guid, NEW.parentGuid, NEW.uploadedAt, 0,\n           1, -- SyncedBookmarkValidity::Valid\n           NEW.isDeleted, NEW.kind, NEW.dateAdded, NEW.title,\n           NEW.placeId, NEW.keyword);\n\n    -- Update the list of children to reflect what we just uploaded.\n    INSERT INTO moz_bookmarks_synced_structure(guid, parentGuid, position)\n    SELECT guid, NEW.guid, position\n    FROM structureToUpload\n    WHERE parentId = NEW.id;\n\n    -- ...And tags, too.\n    INSERT INTO moz_bookmarks_synced_tag_relation(itemId, tagId)\n    SELECT v.id, (SELECT t.id FROM moz_tags t\n                  WHERE t.tag = o.tag)\n    FROM tagsToUpload o\n    JOIN itemsToUpload u ON u.id = o.id\n    JOIN moz_bookmarks_synced v ON v.guid = u.guid\n    WHERE o.id = NEW.id;\nEND;\n\nCREATE TEMP TRIGGER changeGuids\nAFTER DELETE ON changeGuidOps\nBEGIN\n  UPDATE moz_bookmarks SET\n    guid = OLD.mergedGuid,\n    lastModified = OLD.lastModified,\n    syncStatus = IFNULL(OLD.syncStatus, syncStatus)\n  WHERE guid = OLD.localGuid;\nEND;\n\nCREATE TEMP TRIGGER applyNewLocalStructure\nAFTER DELETE ON applyNewLocalStructureOps\nBEGIN\n  UPDATE moz_bookmarks SET\n    parent = (SELECT id FROM moz_bookmarks\n              WHERE guid = OLD.mergedParentGuid),\n    position = OLD.position,\n    lastModified = OLD.lastModified\n  WHERE guid = OLD.mergedGuid;\nEND;\n";