From 9994de3be66624b38f0f6838980310cf65d9687e Mon Sep 17 00:00:00 2001 From: Maria Rekowska Date: Thu, 20 Sep 2018 12:40:01 -0700 Subject: [PATCH] Fix adding and removing index in the same upgrade (#60) * Fix adding and removing index in the same upgrade --- package.json | 2 +- src/SqlProviderBase.ts | 13 +++++--- src/tests/NoSqlProviderTests.ts | 56 +++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 5461f2c..290be04 100644 --- a/package.json +++ b/package.json @@ -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 ", "scripts": { diff --git a/src/SqlProviderBase.ts b/src/SqlProviderBase.ts index e8ac3ab..174a317 100644 --- a/src/SqlProviderBase.ts +++ b/src/SqlProviderBase.ts @@ -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 @@ -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( diff --git a/src/tests/NoSqlProviderTests.ts b/src/tests/NoSqlProviderTests.ts index ef5ee45..38b188c 100644 --- a/src/tests/NoSqlProviderTests.ts +++ b/src/tests/NoSqlProviderTests.ts @@ -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('Shouldn\'t have worked'); + }, () => { + // Expected to fail, so chain from failure to success + return undefined; + }); + + return SyncTasks.all([p1, p2]).then(() => { + return prov.close(); + }); + }); + }); + }); } }); }