| 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/. |
| 4 |
|
| 5 |
-- This is a very simple schema for a chrome.storage.* implementation. At time |
| 6 |
-- of writing, only chrome.storage.sync is supported, but this can be trivially |
| 7 |
-- enhanced to support chrome.storage.local (the api is identical, it's just a |
| 8 |
-- different "bucket" and doesn't sync). |
| 9 |
-- |
| 10 |
-- Even though the spec allows for a single extension to have any number of |
| 11 |
-- "keys", we've made the decision to store all keys for a given extension in a |
| 12 |
-- single row as a JSON representation of all keys and values. |
| 13 |
-- We've done this primarily due to: |
| 14 |
-- * The shape of the API is very JSON, and it almost encourages multiple keys |
| 15 |
-- to be fetched at one time. |
| 16 |
-- * The defined max sizes that extensions are allowed to store using this API |
| 17 |
-- is sufficiently small that we don't have many concerns around record sizes. |
| 18 |
-- * We'd strongly prefer to keep one record per extension when syncing this |
| 19 |
-- data, so having the local store in this shape makes syncing easier. |
| 20 |
|
| 21 |
CREATE TABLE IF NOT EXISTS storage_sync_data ( |
| 22 |
ext_id TEXT NOT NULL PRIMARY KEY, |
| 23 |
|
| 24 |
/* The JSON payload. NULL means it's a tombstone */ |
| 25 |
data TEXT, |
| 26 |
|
| 27 |
/* Same "sync change counter" strategy used by other components. */ |
| 28 |
sync_change_counter INTEGER NOT NULL DEFAULT 1 |
| 29 |
); |
| 30 |
|
| 31 |
CREATE TABLE IF NOT EXISTS storage_sync_mirror ( |
| 32 |
guid TEXT NOT NULL PRIMARY KEY, |
| 33 |
|
| 34 |
/* The extension_id is explicitly not the GUID used on the server. |
| 35 |
It can't be a regular foreign-key relationship back to storage_sync_data |
| 36 |
as items with no data on the server (ie, deleted items) will not appear |
| 37 |
in storage_sync_data, and the guid isn't in that table either. |
| 38 |
It must allow NULL as tombstones do not carry the ext_id, so we have |
| 39 |
an additional CHECK constraint. |
| 40 |
*/ |
| 41 |
ext_id TEXT UNIQUE, |
| 42 |
|
| 43 |
/* The JSON payload. We *do* allow NULL here - it means "deleted" */ |
| 44 |
data TEXT |
| 45 |
|
| 46 |
/* tombstones have no ext_id and no data. Non tombstones must have both */ |
| 47 |
CHECK((ext_id IS NULL AND data IS NULL) OR (ext_id IS NOT NULL AND data IS NOT NULL)) |
| 48 |
); |
| 49 |
|
| 50 |
-- This table holds key-value metadata - primarily for sync. |
| 51 |
CREATE TABLE IF NOT EXISTS meta ( |
| 52 |
key TEXT PRIMARY KEY, |
| 53 |
value NOT NULL |
| 54 |
) WITHOUT ROWID; |