wallet-core: make DB migration logic more tolerant
This commit is contained in:
parent
509011f9bc
commit
3222617b81
@ -2468,8 +2468,14 @@ export async function applyFixups(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Upgrade an IndexedDB in an upgrade transaction.
|
||||||
|
*
|
||||||
|
* The upgrade is made based on a store map, i.e. the metadata
|
||||||
|
* structure that describes all the object stores and indexes.
|
||||||
|
*/
|
||||||
function upgradeFromStoreMap(
|
function upgradeFromStoreMap(
|
||||||
storeMap: any,
|
storeMap: any, // FIXME: nail down type
|
||||||
db: IDBDatabase,
|
db: IDBDatabase,
|
||||||
oldVersion: number,
|
oldVersion: number,
|
||||||
newVersion: number,
|
newVersion: number,
|
||||||
@ -2511,6 +2517,10 @@ function upgradeFromStoreMap(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let s: IDBObjectStore;
|
let s: IDBObjectStore;
|
||||||
|
// Be tolerant if object store already exists.
|
||||||
|
// Probably means somebody deployed without
|
||||||
|
// adding the "addedInVersion" attribute.
|
||||||
|
if (!upgradeTransaction.objectStoreNames.contains(swi.storeName)) {
|
||||||
try {
|
try {
|
||||||
s = db.createObjectStore(swi.storeName, {
|
s = db.createObjectStore(swi.storeName, {
|
||||||
autoIncrement: storeDesc.autoIncrement,
|
autoIncrement: storeDesc.autoIncrement,
|
||||||
@ -2522,12 +2532,19 @@ function upgradeFromStoreMap(
|
|||||||
`Migration failed. Could not create store ${swi.storeName}.${moreInfo}`,
|
`Migration failed. Could not create store ${swi.storeName}.${moreInfo}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
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;
|
||||||
if (indexAddedVersion <= oldVersion) {
|
if (indexAddedVersion <= oldVersion) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
// Be tolerant if index already exists.
|
||||||
|
// Probably means somebody deployed without
|
||||||
|
// adding the "addedInVersion" attribute.
|
||||||
|
if (!s.indexNames.contains(indexDesc.name)) {
|
||||||
try {
|
try {
|
||||||
s.createIndex(indexDesc.name, indexDesc.keyPath, {
|
s.createIndex(indexDesc.name, indexDesc.keyPath, {
|
||||||
multiEntry: indexDesc.multiEntry,
|
multiEntry: indexDesc.multiEntry,
|
||||||
@ -2541,6 +2558,7 @@ function upgradeFromStoreMap(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function promiseFromTransaction(transaction: IDBTransaction): Promise<void> {
|
function promiseFromTransaction(transaction: IDBTransaction): Promise<void> {
|
||||||
|
Loading…
Reference in New Issue
Block a user