Skip to content

Commit 22911d5

Browse files
committed
DataConnectCacheDatabaseMigrator.kt: make the database multi-user
Make the database multi-user by adding a `userId` column to the `entity_data`, `query_results`, and `entity_query_map` tables. Also, update the primary keys, unique constraints, and indexes to reflect this change. ### Changes * DataConnectCacheDatabaseMigrator.kt: * Add a `userId` column to the `entity_data`, `query_results`, and `entity_query_map` tables * Update the primary keys, unique constraints, and indexes to reflect this change
1 parent b0bb84d commit 22911d5

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

firebase-dataconnect/src/main/kotlin/com/google/firebase/dataconnect/sqlite2/DataConnectCacheDatabaseMigrator.kt

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -209,43 +209,57 @@ private constructor(private val sqliteDatabase: SQLiteDatabase, private val logg
209209
logger,
210210
"CREATE TABLE sequence_number (id INTEGER PRIMARY KEY AUTOINCREMENT)"
211211
)
212+
sqliteDatabase.execSQL(
213+
logger,
214+
"""CREATE TABLE users (
215+
id INTEGER PRIMARY KEY,
216+
auth_uid TEXT NOT NULL UNIQUE,
217+
debug_info TEXT
218+
)"""
219+
)
212220
sqliteDatabase.execSQL(
213221
logger,
214222
"""CREATE TABLE entity_data (
215223
id INTEGER PRIMARY KEY,
216-
entityId BLOB NOT NULL UNIQUE,
224+
userId INT NOT NULL REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE,
225+
entityId BLOB NOT NULL,
217226
flags INT NOT NULL,
218227
data BLOB NOT NULL,
219228
sequence_number INT NOT NULL,
220-
debug_info TEXT
229+
debug_info TEXT,
230+
UNIQUE (userId, entityId)
221231
)"""
222232
)
223233
sqliteDatabase.execSQL(
224234
logger,
225235
"""CREATE TABLE query_results (
226236
id INTEGER PRIMARY KEY,
227-
queryId BLOB NOT NULL UNIQUE,
237+
userId INT NOT NULL REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE,
238+
queryId BLOB NOT NULL,
228239
flags INT NOT NULL,
229240
data BLOB NOT NULL,
230241
sequence_number INT NOT NULL,
231242
ttl BLOB,
232-
debug_info TEXT
243+
debug_info TEXT,
244+
UNIQUE (userId, queryId)
233245
)"""
234246
)
235247
sqliteDatabase.execSQL(
236248
logger,
237249
"""CREATE TABLE entity_query_map (
250+
userId INT NOT NULL REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE,
238251
queryId BLOB NOT NULL REFERENCES queries ON DELETE CASCADE ON UPDATE CASCADE,
239252
entityId BLOB NOT NULL REFERENCES entities ON DELETE CASCADE ON UPDATE CASCADE,
240-
PRIMARY KEY (queryId, entityId)
253+
PRIMARY KEY (userId, queryId, entityId)
241254
)"""
242255
)
243-
// Add an explicit index on the `entityId` column so that "WHERE entityId=?" queries are fast.
244-
// Note that "WHERE queryId=?" queries are already fast because `queryId` is the _first_
245-
// component of the primary key and, therefore, is implicitly indexed.
256+
// Add an explicit index on the `userId`-`entityId` columns so that
257+
// "WHERE userId=? AND entityId=?" queries are fast. Note that "WHERE userId=? AND queryId=?"
258+
// queries are _already_ fast because `userId` and `queryId` are the _first_ components of the
259+
// primary key and, therefore, are implicitly indexed.
246260
sqliteDatabase.execSQL(
247261
logger,
248-
"CREATE INDEX entity_query_map_entity_index ON entity_query_map(entityId)"
262+
"CREATE INDEX entity_query_map_user_entity_index ON entity_query_map(userId, entityId)"
249263
)
250264
}
251265

0 commit comments

Comments
 (0)