wallet-core: fix DB migration logic when only an index is added

This commit is contained in:
Florian Dold 2023-02-20 15:07:30 +01:00
parent 6b28885f0b
commit 85c5c6d7c7
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B

View File

@ -118,7 +118,7 @@ export const CURRENT_DB_CONFIG_KEY = "currentMainDbName";
* backwards-compatible way or object stores and indices * backwards-compatible way or object stores and indices
* are added. * are added.
*/ */
export const WALLET_DB_MINOR_VERSION = 4; export const WALLET_DB_MINOR_VERSION = 5;
/** /**
* Ranges for operation status fields. * Ranges for operation status fields.
@ -2291,7 +2291,7 @@ export const WalletStoresV1 = {
"byExchangeAndContractPriv", "byExchangeAndContractPriv",
["exchangeBaseUrl", "contractPriv"], ["exchangeBaseUrl", "contractPriv"],
{ {
versionAdded: 4, versionAdded: 5,
unique: true, unique: true,
}, },
), ),
@ -2299,7 +2299,7 @@ export const WalletStoresV1 = {
"byWithdrawalGroupId", "byWithdrawalGroupId",
"withdrawalGroupId", "withdrawalGroupId",
{ {
versionAdded: 4, versionAdded: 5,
}, },
), ),
byStatus: describeIndex("byStatus", "status"), byStatus: describeIndex("byStatus", "status"),
@ -2319,7 +2319,7 @@ export const WalletStoresV1 = {
"byExchangeAndContractPriv", "byExchangeAndContractPriv",
["exchangeBaseUrl", "contractPriv"], ["exchangeBaseUrl", "contractPriv"],
{ {
versionAdded: 4, versionAdded: 5,
unique: true, unique: true,
}, },
), ),
@ -2337,7 +2337,7 @@ export const WalletStoresV1 = {
"byWithdrawalGroupId", "byWithdrawalGroupId",
"withdrawalGroupId", "withdrawalGroupId",
{ {
versionAdded: 4, versionAdded: 5,
}, },
), ),
}, },
@ -2663,28 +2663,28 @@ function upgradeFromStoreMap(
]; ];
const storeDesc: StoreDescriptor<unknown> = swi.store; const storeDesc: StoreDescriptor<unknown> = swi.store;
const storeAddedVersion = storeDesc.versionAdded ?? 0; const storeAddedVersion = storeDesc.versionAdded ?? 0;
if (storeAddedVersion <= oldVersion) {
continue;
}
let s: IDBObjectStore; let s: IDBObjectStore;
// Be tolerant if object store already exists. if (storeAddedVersion > oldVersion) {
// Probably means somebody deployed without // Be tolerant if object store already exists.
// adding the "addedInVersion" attribute. // Probably means somebody deployed without
if (!upgradeTransaction.objectStoreNames.contains(swi.storeName)) { // adding the "addedInVersion" attribute.
try { if (!upgradeTransaction.objectStoreNames.contains(swi.storeName)) {
s = db.createObjectStore(swi.storeName, { try {
autoIncrement: storeDesc.autoIncrement, s = db.createObjectStore(swi.storeName, {
keyPath: storeDesc.keyPath, autoIncrement: storeDesc.autoIncrement,
}); keyPath: storeDesc.keyPath,
} catch (e) { });
const moreInfo = e instanceof Error ? ` Reason: ${e.message}` : ""; } catch (e) {
throw Error( const moreInfo = e instanceof Error ? ` Reason: ${e.message}` : "";
`Migration failed. Could not create store ${swi.storeName}.${moreInfo}`, throw Error(
); `Migration failed. Could not create store ${swi.storeName}.${moreInfo}`,
);
}
} }
} else {
s = upgradeTransaction.objectStore(swi.storeName);
} }
s = upgradeTransaction.objectStore(swi.storeName);
for (const indexName in swi.indexMap as any) { for (const indexName in swi.indexMap as any) {
const indexDesc: IndexDescriptor = swi.indexMap[indexName]; const indexDesc: IndexDescriptor = swi.indexMap[indexName];
const indexAddedVersion = indexDesc.versionAdded ?? 0; const indexAddedVersion = indexDesc.versionAdded ?? 0;