1 -- This Source Code Form is subject to the terms of the Mozilla Public
2 -- License, v. 2.0. If a copy of the MPL was not distributed with this
3 -- file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 -- Temp tables used by Sync.
6 -- Note that this is executed both before and after a sync.
8 CREATE TEMP TABLE IF NOT EXISTS storage_sync_staging (
9 guid TEXT NOT NULL PRIMARY KEY,
11 /* The extension_id is explicitly not the GUID used on the server.
12 It can't be a regular foreign-key relationship back to storage_sync_data,
13 nor can it be NOT NULL, as the ext_id for incoming items may not appear
14 in storage_sync_data at the time we populate this table, and also
15 because incoming tombstones have no extension ID.
19 /* The JSON payload. We *do* allow NULL here - it means "deleted" */
23 DELETE FROM temp.storage_sync_staging;
25 -- We record the changes we are making via sync in this table, so that at the
26 -- end of the sync extensions can find out via notifications what changes
28 CREATE TEMP TABLE IF NOT EXISTS storage_sync_applied (
29 ext_id TEXT NOT NULL UNIQUE,
31 /* A StorageChanges value serialized as JSON. */
35 DELETE FROM temp.storage_sync_applied;
37 -- We store metadata about items we are uploading in this temp table. After
38 -- we get told the upload was successful we use this to update the local
40 CREATE TEMP TABLE IF NOT EXISTS storage_sync_outgoing_staging (
41 guid TEXT NOT NULL PRIMARY KEY DEFAULT(generate_guid()),
42 ext_id TEXT NOT NULL UNIQUE,
44 sync_change_counter INTEGER NOT NULL,
45 was_uploaded BOOLEAN NOT NULL DEFAULT 0
48 CREATE TEMP TRIGGER IF NOT EXISTS record_uploaded
49 AFTER UPDATE OF was_uploaded ON storage_sync_outgoing_staging
52 -- Decrement the local change counter for uploaded items. If any local items
53 -- changed while we were uploading, their change counters will remain > 0,
54 -- and we'll merge them again on the next sync.
55 UPDATE storage_sync_data SET
56 sync_change_counter = sync_change_counter - NEW.sync_change_counter
57 WHERE NEW.ext_id IS NOT NULL AND ext_id = NEW.ext_id;
59 -- Delete uploaded tombstones entirely; they're only kept in the mirror.
60 DELETE FROM storage_sync_data WHERE data IS NULL AND sync_change_counter = 0 AND ext_id = NEW.ext_id;
62 -- And write the uploaded item back to the mirror.
63 INSERT OR REPLACE INTO storage_sync_mirror (guid, ext_id, data)
64 -- Our mirror has a constraint for tombstones, so handle that - if data is
65 -- null we want a null ext_id (as that's whats on the server)
66 VALUES (NEW.guid, CASE WHEN NEW.data IS NULL THEN NULL ELSE NEW.ext_id END, NEW.data);
69 DELETE FROM temp.storage_sync_outgoing_staging;