Skip to content

Commit eb44929

Browse files
Adding Schema Migration
1 parent 631e1ad commit eb44929

File tree

3 files changed

+108
-46
lines changed

3 files changed

+108
-46
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* Copyright 2018 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import {
17+
DbDocumentMutation, DbInstance,
18+
DbMutationBatch, DbMutationQueue, DbOwner, DbRemoteDocument, DbTarget,
19+
DbTargetDocument, DbTargetGlobal
20+
} from './indexeddb_schema';
21+
import {fail} from '../util/assert';
22+
23+
export function createDb(db: IDBDatabase): void {
24+
db.createObjectStore(DbMutationQueue.store, {
25+
keyPath: DbMutationQueue.keyPath
26+
});
27+
28+
// TODO(mikelehen): Get rid of "as any" if/when TypeScript fixes their
29+
// types. https://github.com/Microsoft/TypeScript/issues/14322
30+
db.createObjectStore(
31+
DbMutationBatch.store,
32+
// tslint:disable-next-line:no-any
33+
{ keyPath: DbMutationBatch.keyPath as any }
34+
);
35+
36+
const targetDocumentsStore = db.createObjectStore(
37+
DbTargetDocument.store,
38+
// tslint:disable-next-line:no-any
39+
{ keyPath: DbTargetDocument.keyPath as any }
40+
);
41+
targetDocumentsStore.createIndex(
42+
DbTargetDocument.documentTargetsIndex,
43+
DbTargetDocument.documentTargetsKeyPath,
44+
{ unique: true }
45+
);
46+
47+
const targetStore = db.createObjectStore(DbTarget.store, {
48+
keyPath: DbTarget.keyPath
49+
});
50+
// NOTE: This is unique only because the TargetId is the suffix.
51+
targetStore.createIndex(
52+
DbTarget.queryTargetsIndexName,
53+
DbTarget.queryTargetsKeyPath,
54+
{ unique: true }
55+
);
56+
57+
const instanceStore = db.createObjectStore(DbInstance.store, {
58+
keyPath: DbInstance.keyPath as any
59+
});
60+
61+
// NOTE: keys for these stores are specified explicitly rather than using a
62+
// keyPath.
63+
db.createObjectStore(DbDocumentMutation.store);
64+
db.createObjectStore(DbRemoteDocument.store);
65+
db.createObjectStore(DbOwner.store);
66+
db.createObjectStore(DbTargetGlobal.store);
67+
}
68+
69+
export function upgradeDbFromV1(db: IDBDatabase): void {
70+
71+
}
72+
73+
export class IndexedDbMigrations {
74+
/**
75+
* Runs any migrations needed to bring the given database up to the current
76+
* schema version.
77+
*/
78+
static runMigrations(db: IDBDatabase, oldVersion: number) {
79+
if (oldVersion == 0) {
80+
createDb(db, oldVersion);
81+
return;
82+
}
83+
84+
if (oldVersion == 1) {
85+
upgradeDbFromV1(db, oldVersion);
86+
return;
87+
}
88+
89+
fail('Unexpected upgrade from version ' + oldVersion);
90+
}
91+
}

packages/firestore/src/local/indexeddb_persistence.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { PersistencePromise } from './persistence_promise';
3434
import { QueryCache } from './query_cache';
3535
import { RemoteDocumentCache } from './remote_document_cache';
3636
import { SimpleDb, SimpleDbTransaction } from './simple_db';
37+
import {IndexedDbMigrations} from './indexeddb_migrations';
3738

3839
const LOG_TAG = 'IndexedDbPersistence';
3940

@@ -127,7 +128,7 @@ export class IndexedDbPersistence implements Persistence {
127128
assert(!this.started, 'IndexedDbPersistence double-started!');
128129
this.started = true;
129130

130-
return SimpleDb.openOrCreate(this.dbName, SCHEMA_VERSION, createOrUpgradeDb)
131+
return SimpleDb.openOrCreate(this.dbName, SCHEMA_VERSION, IndexedDbMigrations.runMigrations)
131132
.then(db => {
132133
this.simpleDb = db;
133134
})

packages/firestore/src/local/indexeddb_schema.ts

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,51 +24,6 @@ import { encode, EncodedResourcePath } from './encoded_resource_path';
2424

2525
export const SCHEMA_VERSION = 1;
2626

27-
/** Performs database creation and (in the future) upgrades between versions. */
28-
export function createOrUpgradeDb(db: IDBDatabase, oldVersion: number): void {
29-
assert(oldVersion === 0, 'Unexpected upgrade from version ' + oldVersion);
30-
31-
db.createObjectStore(DbMutationQueue.store, {
32-
keyPath: DbMutationQueue.keyPath
33-
});
34-
35-
// TODO(mikelehen): Get rid of "as any" if/when TypeScript fixes their
36-
// types. https://github.com/Microsoft/TypeScript/issues/14322
37-
db.createObjectStore(
38-
DbMutationBatch.store,
39-
// tslint:disable-next-line:no-any
40-
{ keyPath: DbMutationBatch.keyPath as any }
41-
);
42-
43-
const targetDocumentsStore = db.createObjectStore(
44-
DbTargetDocument.store,
45-
// tslint:disable-next-line:no-any
46-
{ keyPath: DbTargetDocument.keyPath as any }
47-
);
48-
targetDocumentsStore.createIndex(
49-
DbTargetDocument.documentTargetsIndex,
50-
DbTargetDocument.documentTargetsKeyPath,
51-
{ unique: true }
52-
);
53-
54-
const targetStore = db.createObjectStore(DbTarget.store, {
55-
keyPath: DbTarget.keyPath
56-
});
57-
// NOTE: This is unique only because the TargetId is the suffix.
58-
targetStore.createIndex(
59-
DbTarget.queryTargetsIndexName,
60-
DbTarget.queryTargetsKeyPath,
61-
{ unique: true }
62-
);
63-
64-
// NOTE: keys for these stores are specified explicitly rather than using a
65-
// keyPath.
66-
db.createObjectStore(DbDocumentMutation.store);
67-
db.createObjectStore(DbRemoteDocument.store);
68-
db.createObjectStore(DbOwner.store);
69-
db.createObjectStore(DbTargetGlobal.store);
70-
}
71-
7227
/**
7328
* Wrapper class to store timestamps (seconds and nanos) in IndexedDb objects.
7429
*/
@@ -455,6 +410,21 @@ export class DbTargetGlobal {
455410
) {}
456411
}
457412

413+
export type DbInstanceKey = [string, string];
414+
415+
export class DbInstance {
416+
static store = 'instances';
417+
418+
static keyPath = ['userId', 'instanceId'];
419+
420+
constructor(
421+
public userId: string,
422+
public instanceId: string,
423+
public updateTimeMs: number,
424+
public visibilityState: VisibilityState
425+
) {}
426+
}
427+
458428
/**
459429
* The list of all IndexedDB stored used by the SDK. This is used when creating
460430
* transactions so that access across all stores is done atomically.

0 commit comments

Comments
 (0)