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
Changes from 2 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
59 changes: 30 additions & 29 deletions packages/firestore/test/unit/local/schema_migration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,30 @@ import { SimpleDb } from '../../../src/local/simple_db';

const INDEXEDDB_TEST_DATABASE = 'schemaTest';

function initDb(targetVersion): Promise<IDBDatabase> {
const deferred = new Deferred<IDBDatabase>();

const request = window.indexedDB.open(INDEXEDDB_TEST_DATABASE, targetVersion);
request.onsuccess = (event: Event) => {
const db = (event.target as IDBOpenDBRequest).result;
deferred.resolve(db);
};
request.onerror = (event: ErrorEvent) => {
deferred.reject((event.target as IDBOpenDBRequest).error);
};
request.onupgradeneeded = (event: IDBVersionChangeEvent) => {
const db = (event.target as IDBOpenDBRequest).result;
createOrUpgradeDb(db, event.oldVersion, targetVersion);
};

return deferred.promise;
function withDb(schemaVersion, fn: (db: IDBDatabase) => void): Promise<void> {
return new Promise<IDBDatabase>((resolve, reject) => {
const request = window.indexedDB.open(
INDEXEDDB_TEST_DATABASE,
schemaVersion
);
request.onupgradeneeded = (event: IDBVersionChangeEvent) => {
const db = (event.target as IDBOpenDBRequest).result;
createOrUpgradeDb(db, event.oldVersion, schemaVersion);
};
request.onsuccess = (event: Event) => {
resolve((event.target as IDBOpenDBRequest).result);
};
request.onerror = (event: ErrorEvent) => {
reject((event.target as IDBOpenDBRequest).error);
};
})
.then(db => {
fn(db);
return db;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can just db.close() here and drop a then...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

})
.then(db => {
db.close();
});
}

function getAllObjectStores(db: IDBDatabase): String[] {
Expand All @@ -63,31 +70,25 @@ describe('IndexedDbSchema: createOrUpgradeDb', () => {
beforeEach(() => SimpleDb.delete(INDEXEDDB_TEST_DATABASE));

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

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

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