Skip to content

Commit 9994de3

Browse files
mariarekberickson1
authored andcommitted
Fix adding and removing index in the same upgrade (#60)
* Fix adding and removing index in the same upgrade
1 parent 546a4d4 commit 9994de3

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nosqlprovider",
3-
"version": "0.6.16",
3+
"version": "0.6.17",
44
"description": "A cross-browser/platform indexeddb-like client library",
55
"author": "David de Regt <David.de.Regt@microsoft.com>",
66
"scripts": {

src/SqlProviderBase.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,8 @@ export abstract class SqlProviderBase extends NoSqlProvider.DbProvider {
327327
const indexIdentifierDictionary = _.keyBy(storeSchema.indexes, index => getIndexIdentifier(storeSchema, index));
328328
const indexMetaDictionary = _.keyBy(currentIndexMetas, meta => meta.key);
329329

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

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

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

477+
return trans.runQuery('INSERT INTO ' + storeSchema.name + '(' + columnsToCopy + ')' +
478+
' SELECT ' + columnsToCopy +
479+
' FROM temp_' + storeSchema.name);
477480
};
478481

479482
tableQueries.push(

src/tests/NoSqlProviderTests.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,6 +2228,62 @@ describe('NoSqlProvider', function () {
22282228
});
22292229
});
22302230
});
2231+
2232+
it('Add and remove index in the same upgrade', () => {
2233+
return openProvider(provName, {
2234+
version: 1,
2235+
stores: [
2236+
{
2237+
name: 'test',
2238+
primaryKeyPath: 'id',
2239+
indexes: [{
2240+
name: 'ind1',
2241+
keyPath: 'tt',
2242+
doNotBackfill: true
2243+
}]
2244+
}
2245+
]
2246+
}, true)
2247+
.then(prov => {
2248+
return prov.put('test', { id: 'abc', tt: 'a' , zz: 'aa'}).then(() => {
2249+
return prov.close();
2250+
});
2251+
})
2252+
.then(() => {
2253+
return openProvider(provName, {
2254+
version: 2,
2255+
stores: [
2256+
{
2257+
name: 'test',
2258+
primaryKeyPath: 'id',
2259+
indexes: [{
2260+
name: 'ind2',
2261+
keyPath: 'zz',
2262+
doNotBackfill: true
2263+
}]
2264+
}
2265+
]
2266+
}, false)
2267+
.then(prov => {
2268+
const p1 = prov.getOnly('test', undefined, 'abc').then((items: any[]) => {
2269+
assert.equal(items.length, 1);
2270+
assert.equal(items[0].id, 'abc');
2271+
assert.equal(items[0].tt, 'a');
2272+
assert.equal(items[0].zz, 'aa');
2273+
});
2274+
const p2 = prov.getOnly('test', 'ind1', 'a').then(items => {
2275+
return SyncTasks.Rejected<void>('Shouldn\'t have worked');
2276+
}, () => {
2277+
// Expected to fail, so chain from failure to success
2278+
return undefined;
2279+
});
2280+
2281+
return SyncTasks.all([p1, p2]).then(() => {
2282+
return prov.close();
2283+
});
2284+
});
2285+
});
2286+
});
22312287
}
22322288
});
22332289
}

0 commit comments

Comments
 (0)