feat: 🎸 Add migration check to delete index DB when updating (#2241)

* feat: 🎸 Add migration check to delete index DB when updating

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-13223
This commit is contained in:
Zhihe Li authored and GitHub committed 2025-02-13 11:00:19 -05:00
1 parent f3232f34c2
commit 20c4bbcee2
6 files changed
+33 -19

No files matched your search

@@ -20,9 +20,9 @@
* @param hooks
*/
export function setupIndexedDb(hooks) {
hooks.beforeEach(function () {
hooks.beforeEach(async function () {
const indexedDb = this.owner.lookup('service:indexed-db');
indexedDb.setup(`test-indexed-db-${Date.now()}`);
await indexedDb.setup(`test-indexed-db-${Date.now()}`);
});
hooks.afterEach(async function () {
+22 -3
View File
@@ -97,23 +97,42 @@ const unconvertFields = (result, object, schema, serializer) => {
/**
* Service to encapsulate the IndexedDB implementation. To use this service, call
* `setup` from the application root and
* `setup` from the application root. This will create the database and the necessary indexes.
*
* Make sure to increment the version number of the database whenever the indexes change.
* This will delete the old database and create a new one with the new indexes.
*/
export default class IndexedDbService extends Service {
// =attributes
#db;
#version = 2;
get db() {
return this.#db;
}
setup(dbName) {
async setup(dbName) {
// Don't run setup again if we already have one or if we didn't get a name
if (this.#db || !dbName) {
return;
}
const doesDbExist = await Dexie.exists(dbName);
// If the database already exists, open the database and
// check if the version is a lower version than the current one
if (doesDbExist) {
const dbVerifier = new Dexie(dbName);
await dbVerifier.open();
if (dbVerifier.verno < this.#version) {
await dbVerifier.delete();
} else {
dbVerifier.close();
}
}
this.#db = new Dexie(dbName);
this.#db.version(2).stores(modelIndexes);
this.#db.version(this.#version).stores(modelIndexes);
}
/**