Skip to content

Add a schema migration that drops the query cache #1019

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 3 commits into from
Jul 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 18 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,13 @@ export function createOrUpgradeDb(
saveTargetCount(txn, targetGlobal)
);
}

// Brand new clients don't need to drop and recreate--only clients that
// potentially have corrupt data.
if (fromVersion !== 0 && fromVersion < 3 && toVersion >= 3) {
Copy link
Contributor

@schmidt-sebastian schmidt-sebastian Jul 18, 2018

Choose a reason for hiding this comment

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

You should wait for p (or get rid of the target count migration) before you continue with the migration.

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.

dropQueryCache(db);
createQueryCache(db);
}
return p;
}

Expand Down Expand Up @@ -507,6 +514,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 PersistencePromise.resolve().next(() =>
targets
Copy link
Contributor

Choose a reason for hiding this comment

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

You can just do:

return targets.put({...}).next(() => ...)

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.

// 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 PersistencePromise.resolve()
.next(() => targets.get(targetId))
Copy link
Contributor

Choose a reason for hiding this comment

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

Per Sebastian's comment above, you could do return targets.get(...).next() => ...) here too, I think?

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.

.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);
});
}
);
});
});
});
});