-
Notifications
You must be signed in to change notification settings - Fork 938
[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
schmidt-sebastian
merged 23 commits into
firestore-multi-tab
from
multitab-schemamigration
Feb 9, 2018
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
eb44929
Adding Schema Migration
schmidt-sebastian 07e44e7
Pseudocode for Schema Migration
schmidt-sebastian 3e1053f
[AUTOMATED]: Prettier Code Styling
schmidt-sebastian 5e84782
IndexedDb Schema Migration
schmidt-sebastian fd50301
Lint cleanup
schmidt-sebastian be3b463
Removing unused import
schmidt-sebastian de237c5
Removing user ID from instance row
schmidt-sebastian 53e56b5
[AUTOMATED]: Prettier Code Styling
schmidt-sebastian 7b3bedb
Review comments
schmidt-sebastian 7b97c0a
Merge branch 'master' into multitab-schemamigration
schmidt-sebastian c176eff
Lint fixes
schmidt-sebastian 9154aad
Review
schmidt-sebastian 6b072ff
[AUTOMATED]: Prettier Code Styling
schmidt-sebastian 662365f
Fixing the tests
schmidt-sebastian dd21376
Closing the Database in the Schema tests
schmidt-sebastian 5e54b3a
[AUTOMATED]: Prettier Code Styling
schmidt-sebastian cb81df8
Changing test helper to close the DB
schmidt-sebastian 7991970
[AUTOMATED]: Prettier Code Styling
schmidt-sebastian 2c6d6e1
Making v2 the default version
schmidt-sebastian f16bb4f
[AUTOMATED]: Prettier Code Styling
schmidt-sebastian 038c159
Addressing comment
schmidt-sebastian b70303c
[AUTOMATED]: Prettier Code Styling
schmidt-sebastian 34ab25a
Renamed to ALL_STORES
schmidt-sebastian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
packages/firestore/test/unit/local/schema_migration.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* Copyright 2018 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { expect } from 'chai'; | ||
import { IndexedDbPersistence } from '../../../src/local/indexeddb_persistence'; | ||
import { | ||
ALL_STORES, | ||
createOrUpgradeDb, | ||
V1_STORES | ||
} from '../../../src/local/indexeddb_schema'; | ||
import { Deferred } from '../../../src/util/promise'; | ||
import { SimpleDb } from '../../../src/local/simple_db'; | ||
|
||
const INDEXEDDB_TEST_DATABASE = 'schemaTest'; | ||
|
||
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); | ||
db.close(); | ||
}); | ||
} | ||
|
||
function getAllObjectStores(db: IDBDatabase): String[] { | ||
const objectStores: String[] = []; | ||
for (let i = 0; i < db.objectStoreNames.length; ++i) { | ||
objectStores.push(db.objectStoreNames.item(i)); | ||
} | ||
objectStores.sort(); | ||
return objectStores; | ||
} | ||
|
||
describe('IndexedDbSchema: createOrUpgradeDb', () => { | ||
if (!IndexedDbPersistence.isAvailable()) { | ||
console.warn('No IndexedDB. Skipping createOrUpgradeDb() tests.'); | ||
return; | ||
} | ||
|
||
beforeEach(() => SimpleDb.delete(INDEXEDDB_TEST_DATABASE)); | ||
|
||
it('can install schema version 1', () => { | ||
return withDb(1, db => { | ||
expect(db.version).to.be.equal(1); | ||
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); | ||
}); | ||
}); | ||
|
||
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); | ||
}) | ||
); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're not doing the switch / case pattern, I think you should still organize similarly (in particular put version 1 stuff before version 2), and I think we need to have a simple pattern that makes it obvious how to add code whenever we bump SCHEMA_VERSION... As-is, the
toVersion === 2
check will need to be changed when we add version 3. In general, this method should be append-only as we add versions. We can't really change version migration stuff once it's shipped.Perhaps:
Or you could do a for loop:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used code snippet version 1.