Skip to content

Commit

Permalink
Fix adding and removing index in the same upgrade (#60)
Browse files Browse the repository at this point in the history
* Fix adding and removing index in the same upgrade
  • Loading branch information
mariarek authored and berickson1 committed Sep 20, 2018
1 parent 546a4d4 commit 9994de3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nosqlprovider",
"version": "0.6.16",
"version": "0.6.17",
"description": "A cross-browser/platform indexeddb-like client library",
"author": "David de Regt <[email protected]>",
"scripts": {
Expand Down
13 changes: 8 additions & 5 deletions src/SqlProviderBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ export abstract class SqlProviderBase extends NoSqlProvider.DbProvider {
const indexIdentifierDictionary = _.keyBy(storeSchema.indexes, index => getIndexIdentifier(storeSchema, index));
const indexMetaDictionary = _.keyBy(currentIndexMetas, meta => meta.key);

// find indices in the schema that did not exist before
const newIndices = _.filter(storeSchema.indexes, index =>
// find which indices in the schema existed / did not exist before
const [newIndices, existingIndices] = _.partition(storeSchema.indexes, index =>
!indexMetaDictionary[getIndexIdentifier(storeSchema, index)]);

// find indices in the meta that do not exist in the new schema
Expand Down Expand Up @@ -470,10 +470,13 @@ export abstract class SqlProviderBase extends NoSqlProvider.DbProvider {

if (doSqlInPlaceMigration) {
const sqlInPlaceMigrator = () => {
return trans.runQuery('INSERT INTO ' + storeSchema.name +
' SELECT ' + ['nsp_pk', 'nsp_data', ...indexColumns].join(', ') +
' FROM temp_' + storeSchema.name);
const columnsToCopy = ['nsp_pk', 'nsp_data',
..._.map(existingIndices, index => getIndexIdentifier(storeSchema, index))
].join(', ');

return trans.runQuery('INSERT INTO ' + storeSchema.name + '(' + columnsToCopy + ')' +
' SELECT ' + columnsToCopy +
' FROM temp_' + storeSchema.name);
};

tableQueries.push(
Expand Down
56 changes: 56 additions & 0 deletions src/tests/NoSqlProviderTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2228,6 +2228,62 @@ describe('NoSqlProvider', function () {
});
});
});

it('Add and remove index in the same upgrade', () => {
return openProvider(provName, {
version: 1,
stores: [
{
name: 'test',
primaryKeyPath: 'id',
indexes: [{
name: 'ind1',
keyPath: 'tt',
doNotBackfill: true
}]
}
]
}, true)
.then(prov => {
return prov.put('test', { id: 'abc', tt: 'a' , zz: 'aa'}).then(() => {
return prov.close();
});
})
.then(() => {
return openProvider(provName, {
version: 2,
stores: [
{
name: 'test',
primaryKeyPath: 'id',
indexes: [{
name: 'ind2',
keyPath: 'zz',
doNotBackfill: true
}]
}
]
}, false)
.then(prov => {
const p1 = prov.getOnly('test', undefined, 'abc').then((items: any[]) => {
assert.equal(items.length, 1);
assert.equal(items[0].id, 'abc');
assert.equal(items[0].tt, 'a');
assert.equal(items[0].zz, 'aa');
});
const p2 = prov.getOnly('test', 'ind1', 'a').then(items => {
return SyncTasks.Rejected<void>('Shouldn\'t have worked');
}, () => {
// Expected to fail, so chain from failure to success
return undefined;
});

return SyncTasks.all([p1, p2]).then(() => {
return prov.close();
});
});
});
});
}
});
}
Expand Down

0 comments on commit 9994de3

Please sign in to comment.