Skip to content

[Multi-Tab] Adding Schema Migration #485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Feb 9, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
eb44929
Adding Schema Migration
schmidt-sebastian Feb 2, 2018
07e44e7
Pseudocode for Schema Migration
schmidt-sebastian Feb 2, 2018
3e1053f
[AUTOMATED]: Prettier Code Styling
schmidt-sebastian Feb 2, 2018
5e84782
IndexedDb Schema Migration
schmidt-sebastian Feb 5, 2018
fd50301
Lint cleanup
schmidt-sebastian Feb 6, 2018
be3b463
Removing unused import
schmidt-sebastian Feb 6, 2018
de237c5
Removing user ID from instance row
schmidt-sebastian Feb 6, 2018
53e56b5
[AUTOMATED]: Prettier Code Styling
schmidt-sebastian Feb 6, 2018
7b3bedb
Review comments
schmidt-sebastian Feb 7, 2018
7b97c0a
Merge branch 'master' into multitab-schemamigration
schmidt-sebastian Feb 7, 2018
c176eff
Lint fixes
schmidt-sebastian Feb 7, 2018
9154aad
Review
schmidt-sebastian Feb 7, 2018
6b072ff
[AUTOMATED]: Prettier Code Styling
schmidt-sebastian Feb 7, 2018
662365f
Fixing the tests
schmidt-sebastian Feb 7, 2018
dd21376
Closing the Database in the Schema tests
schmidt-sebastian Feb 8, 2018
5e54b3a
[AUTOMATED]: Prettier Code Styling
schmidt-sebastian Feb 8, 2018
cb81df8
Changing test helper to close the DB
schmidt-sebastian Feb 8, 2018
7991970
[AUTOMATED]: Prettier Code Styling
schmidt-sebastian Feb 8, 2018
2c6d6e1
Making v2 the default version
schmidt-sebastian Feb 9, 2018
f16bb4f
[AUTOMATED]: Prettier Code Styling
schmidt-sebastian Feb 9, 2018
038c159
Addressing comment
schmidt-sebastian Feb 9, 2018
b70303c
[AUTOMATED]: Prettier Code Styling
schmidt-sebastian Feb 9, 2018
34ab25a
Renamed to ALL_STORES
schmidt-sebastian Feb 9, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions packages/firestore/src/local/indexeddb_persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
createOrUpgradeDb,
DbOwner,
DbOwnerKey,
DEFAULT_SCHEMA_VERSION
SCHEMA_VERSION
} from './indexeddb_schema';
import { LocalSerializer } from './local_serializer';
import { MutationQueue } from './mutation_queue';
Expand Down Expand Up @@ -132,11 +132,7 @@ export class IndexedDbPersistence implements Persistence {
assert(!this.started, 'IndexedDbPersistence double-started!');
this.started = true;

return SimpleDb.openOrCreate(
this.dbName,
DEFAULT_SCHEMA_VERSION,
createOrUpgradeDb
)
return SimpleDb.openOrCreate(this.dbName, SCHEMA_VERSION, createOrUpgradeDb)
.then(db => {
this.simpleDb = db;
})
Expand Down
19 changes: 7 additions & 12 deletions packages/firestore/src/local/indexeddb_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ import { assert } from '../util/assert';
import { encode, EncodedResourcePath } from './encoded_resource_path';

/**
* Default Schema Version for the Web client (containing the Mutation Queue, the
* Query and the Remote Document Cache).
* Schema Version for the Web client (containing the Mutation Queue, the Query
* and the Remote Document Cache) and Multi-Tab Support.
*/
export const DEFAULT_SCHEMA_VERSION = 1;
export const SCHEMA_VERSION = 2;

/**
* Performs database creation and schema upgrades.
*
* Note that in production, this method is only ever used to upgrade the schema
* to DEFAULT_SCHEMA_VERSION. Different versions are only used for testing and
* to SCHEMA_VERSION. Different versions are only used for testing and
* local feature development.
*/
export function createOrUpgradeDb(
Expand Down Expand Up @@ -563,7 +563,8 @@ function createClientMetadataStore(db: IDBDatabase): void {
});
}

const V1_STORES = [
// Visible for testing
export const V1_STORES = [
DbMutationQueue.store,
DbMutationBatch.store,
DbDocumentMutation.store,
Expand All @@ -581,10 +582,4 @@ const V2_STORES = [DbClientMetadata.store, DbTargetChange.store];
* used when creating transactions so that access across all stores is done
* atomically.
*/
export const DEFAULT_STORES = V1_STORES;

/**
* The list of all IndexedDb stores. This is only available in multi-tab enabled
* clients with the schema version 2.
*/
export const ALL_STORES = [...V1_STORES, ...V2_STORES];
export const DEFAULT_STORES = [...V1_STORES, ...V2_STORES];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put back to ALL_STORES?

4 changes: 2 additions & 2 deletions packages/firestore/src/local/simple_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { debug } from '../util/log';
import { AnyDuringMigration } from '../util/misc';

import { PersistencePromise } from './persistence_promise';
import { DEFAULT_SCHEMA_VERSION } from './indexeddb_schema';
import { SCHEMA_VERSION } from './indexeddb_schema';

const LOG_TAG = 'SimpleDb';

Expand Down Expand Up @@ -75,7 +75,7 @@ export class SimpleDb {
// cheating and just passing the raw IndexedDB in, since
// createObjectStore(), etc. are synchronous.
const db = (event.target as IDBOpenDBRequest).result;
runUpgrade(db, event.oldVersion, DEFAULT_SCHEMA_VERSION);
runUpgrade(db, event.oldVersion, SCHEMA_VERSION);
};
}).toPromise();
}
Expand Down
20 changes: 8 additions & 12 deletions packages/firestore/test/unit/local/schema_migration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { IndexedDbPersistence } from '../../../src/local/indexeddb_persistence';
import {
DEFAULT_STORES,
createOrUpgradeDb,
ALL_STORES
V1_STORES
} from '../../../src/local/indexeddb_schema';
import { Deferred } from '../../../src/util/promise';
import { SimpleDb } from '../../../src/local/simple_db';
Expand All @@ -42,14 +42,10 @@ function withDb(schemaVersion, fn: (db: IDBDatabase) => void): Promise<void> {
request.onerror = (event: ErrorEvent) => {
reject((event.target as IDBOpenDBRequest).error);
};
})
.then(db => {
fn(db);
return db;
})
.then(db => {
db.close();
});
}).then(db => {
fn(db);
db.close();
});
}

function getAllObjectStores(db: IDBDatabase): String[] {
Expand All @@ -72,22 +68,22 @@ describe('IndexedDbSchema: createOrUpgradeDb', () => {
it('can install schema version 1', () => {
return withDb(1, db => {
expect(db.version).to.be.equal(1);
expect(getAllObjectStores(db)).to.have.members(DEFAULT_STORES);
expect(getAllObjectStores(db)).to.have.members(V1_STORES);
});
});

it('can install schema version 2', () => {
return withDb(2, db => {
expect(db.version).to.be.equal(2);
expect(getAllObjectStores(db)).to.have.members(ALL_STORES);
expect(getAllObjectStores(db)).to.have.members(DEFAULT_STORES);
});
});

it('can upgrade from schema version 1 to 2', () => {
return withDb(1, () => {}).then(() =>
withDb(2, db => {
expect(db.version).to.be.equal(2);
expect(getAllObjectStores(db)).to.have.members(ALL_STORES);
expect(getAllObjectStores(db)).to.have.members(DEFAULT_STORES);
})
);
});
Expand Down