const CREATE_SYNC_TEMP_TABLES_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-- Temp tables used by Sync.\n-- Note that this is executed both before and after a sync.\n\nCREATE TEMP TABLE IF NOT EXISTS storage_sync_staging (\n guid TEXT NOT NULL PRIMARY KEY,\n\n /* The extension_id is explicitly not the GUID used on the server.\n It can\'t be a regular foreign-key relationship back to storage_sync_data,\n nor can it be NOT NULL, as the ext_id for incoming items may not appear\n in storage_sync_data at the time we populate this table, and also\n because incoming tombstones have no extension ID.\n */\n ext_id TEXT UNIQUE,\n\n /* The JSON payload. We *do* allow NULL here - it means \"deleted\" */\n data TEXT\n);\n\nDELETE FROM temp.storage_sync_staging;\n\n-- We record the changes we are making via sync in this table, so that at the\n-- end of the sync extensions can find out via notifications what changes\n-- were applied.\nCREATE TEMP TABLE IF NOT EXISTS storage_sync_applied (\n ext_id TEXT NOT NULL UNIQUE,\n\n /* A StorageChanges value serialized as JSON. */\n changes TEXT NOT NULL\n);\n\nDELETE FROM temp.storage_sync_applied;\n\n-- We store metadata about items we are uploading in this temp table. After\n-- we get told the upload was successful we use this to update the local\n-- tables.\nCREATE TEMP TABLE IF NOT EXISTS storage_sync_outgoing_staging (\n guid TEXT NOT NULL PRIMARY KEY DEFAULT(generate_guid()),\n ext_id TEXT NOT NULL UNIQUE,\n data TEXT,\n sync_change_counter INTEGER NOT NULL,\n was_uploaded BOOLEAN NOT NULL DEFAULT 0\n);\n\nCREATE TEMP TRIGGER IF NOT EXISTS record_uploaded\nAFTER UPDATE OF was_uploaded ON storage_sync_outgoing_staging\nWHEN NEW.was_uploaded\nBEGIN\n -- Decrement the local change counter for uploaded items. If any local items\n -- changed while we were uploading, their change counters will remain > 0,\n -- and we\'ll merge them again on the next sync.\n UPDATE storage_sync_data SET\n sync_change_counter = sync_change_counter - NEW.sync_change_counter\n WHERE NEW.ext_id IS NOT NULL AND ext_id = NEW.ext_id;\n\n -- Delete uploaded tombstones entirely; they\'re only kept in the mirror.\n DELETE FROM storage_sync_data WHERE data IS NULL AND sync_change_counter = 0 AND ext_id = NEW.ext_id;\n\n -- And write the uploaded item back to the mirror.\n INSERT OR REPLACE INTO storage_sync_mirror (guid, ext_id, data)\n -- Our mirror has a constraint for tombstones, so handle that - if data is\n -- null we want a null ext_id (as that\'s whats on the server)\n VALUES (NEW.guid, CASE WHEN NEW.data IS NULL THEN NULL ELSE NEW.ext_id END, NEW.data);\nEND;\n\nDELETE FROM temp.storage_sync_outgoing_staging;\n";