Skip to content
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

Add a schema migration that drops the query cache #1019

Merged
merged 3 commits into from
Jul 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 21 additions & 5 deletions packages/firestore/src/local/indexeddb_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ import { SnapshotVersion } from '../core/snapshot_version';
* 1. Initial version including Mutation Queue, Query Cache, and Remote Document
* Cache
* 2. Added targetCount to targetGlobal row.
* 3. Dropped and re-created Query Cache to deal with cache corruption related
* to limbo resolution. Addresses
* https://github.com/firebase/firebase-ios-sdk/issues/1548
*/
export const SCHEMA_VERSION = 2;
export const SCHEMA_VERSION = 3;

/**
* Performs database creation and schema upgrades.
Expand All @@ -46,11 +49,8 @@ export function createOrUpgradeDb(
fromVersion: number,
toVersion: number
): PersistencePromise<void> {
// This function currently supports migrating to schema version 1 (Mutation
// Queue, Query and Remote Document Cache) and schema version 2 (Query
// counting).
assert(
fromVersion < toVersion && fromVersion >= 0 && toVersion <= 2,
fromVersion < toVersion && fromVersion >= 0 && toVersion <= SCHEMA_VERSION,
'Unexpected schema upgrade from v${fromVersion} to v{toVersion}.'
);

Expand All @@ -67,6 +67,16 @@ export function createOrUpgradeDb(
saveTargetCount(txn, targetGlobal)
);
}

p = p.next(() => {
// Brand new clients don't need to drop and recreate--only clients that
// potentially have corrupt data.
if (fromVersion !== 0 && fromVersion < 3 && toVersion >= 3) {
dropQueryCache(db);
createQueryCache(db);
}
});

return p;
}

Expand Down Expand Up @@ -507,6 +517,12 @@ function createQueryCache(db: IDBDatabase): void {
db.createObjectStore(DbTargetGlobal.store);
}

function dropQueryCache(db: IDBDatabase): void {
db.deleteObjectStore(DbTargetDocument.store);
db.deleteObjectStore(DbTarget.store);
db.deleteObjectStore(DbTargetGlobal.store);
}

/**
* Counts the number of targets persisted and adds that value to the target
* global singleton.
Expand Down
64 changes: 63 additions & 1 deletion packages/firestore/test/unit/local/indexeddb_schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ import { IndexedDbPersistence } from '../../../src/local/indexeddb_persistence';
import {
ALL_STORES,
createOrUpgradeDb,
DbMutationBatch,
DbMutationBatchKey,
DbTarget,
DbTargetGlobal,
DbTargetGlobalKey
DbTargetGlobalKey,
DbTargetKey
} from '../../../src/local/indexeddb_schema';
import { SimpleDb, SimpleDbTransaction } from '../../../src/local/simple_db';
import { PersistencePromise } from '../../../src/local/persistence_promise';
Expand Down Expand Up @@ -135,4 +138,63 @@ describe('IndexedDbSchema: createOrUpgradeDb', () => {
})
);
});

it('drops the query cache from 2 to 3', () => {
const userId = 'user';
const batchId = 1;
const targetId = 2;

const expectedMutation = new DbMutationBatch(userId, batchId, 1000, []);

return withDb(2, db => {
const sdb = new SimpleDb(db);
return sdb.runTransaction(
'readwrite',
[DbTarget.store, DbMutationBatch.store],
txn => {
const targets = txn.store<DbTargetKey, DbTarget>(DbTarget.store);
const mutations = txn.store<DbMutationBatchKey, DbMutationBatch>(
DbMutationBatch.store
);

return (
targets
// tslint:disable-next-line:no-any
.put({ targetId, canonicalId: 'foo' } as any)
.next(() => mutations.put(expectedMutation))
);
}
);
}).then(() => {
return withDb(3, db => {
expect(db.version).to.equal(3);
expect(getAllObjectStores(db)).to.have.members(ALL_STORES);

const sdb = new SimpleDb(db);
return sdb.runTransaction(
'readwrite',
[DbTarget.store, DbMutationBatch.store],
txn => {
const targets = txn.store<DbTargetKey, DbTarget>(DbTarget.store);
const mutations = txn.store<DbMutationBatchKey, DbMutationBatch>(
DbMutationBatch.store
);

return targets
.get(targetId)
.next(target => {
// The target should have been dropped
expect(target).to.be.null;
})
.next(() => mutations.get([userId, batchId]))
.next(mutation => {
// Mutations should be unaffected.
expect(mutation.userId).to.equal(userId);
expect(mutation.batchId).to.equal(batchId);
});
}
);
});
});
});
});
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy