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 * @param hooks
*/ */
export function setupIndexedDb(hooks) { export function setupIndexedDb(hooks) {
hooks.beforeEach(function () { hooks.beforeEach(async function () {
const indexedDb = this.owner.lookup('service:indexed-db'); 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 () { 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 * 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 { export default class IndexedDbService extends Service {
// =attributes // =attributes
#db; #db;
#version = 2;
get db() { get db() {
return this.#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 // Don't run setup again if we already have one or if we didn't get a name
if (this.#db || !dbName) { if (this.#db || !dbName) {
return; 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 = new Dexie(dbName);
this.#db.version(2).stores(modelIndexes); this.#db.version(this.#version).stores(modelIndexes);
} }
/** /**
@@ -47,7 +47,6 @@ export default class ScopesScopeAuthenticateMethodIndexController extends Contro
requestCookies, requestCookies,
{ scope, authMethod }, { scope, authMethod },
); );
this.router.transitionTo('index');
break; break;
case TYPE_AUTH_METHOD_OIDC: case TYPE_AUTH_METHOD_OIDC:
await this.startOIDCAuthentication(authenticatorName, { await this.startOIDCAuthentication(authenticatorName, {
+1 -1
View File
@@ -73,7 +73,7 @@ export default class ApplicationRoute extends Route {
const userId = this.session.data?.authenticated?.user_id; const userId = this.session.data?.authenticated?.user_id;
const hostUrl = this.window.location.host; const hostUrl = this.window.location.host;
if (userId && hostUrl) { if (userId && hostUrl) {
this.indexedDb.setup(formatDbName(userId, hostUrl)); await this.indexedDb.setup(formatDbName(userId, hostUrl));
} }
} }
} }
+7 -5
View File
@@ -15,13 +15,15 @@ export default class SessionService extends BaseSessionService {
* Extend ember simple auth's handleAuthentication method * Extend ember simple auth's handleAuthentication method
* so we can hook in and setup the DB after a successful authentication * so we can hook in and setup the DB after a successful authentication
*/ */
handleAuthentication() { async handleAuthentication() {
super.handleAuthentication(...arguments);
const userId = this.data?.authenticated?.user_id; const userId = this.data?.authenticated?.user_id;
const hostUrl = this.window.location.host; const hostUrl = this.window.location?.host;
if (userId && hostUrl) { if (userId && hostUrl) {
this.indexedDb.setup(formatDbName(userId, hostUrl)); await this.indexedDb.setup(formatDbName(userId, hostUrl));
} }
// We let ember-simple-auth handle transitioning back to the index after authentication.
// This route can be configured in our environment config.
super.handleAuthentication(...arguments);
} }
} }
@@ -5,7 +5,7 @@
import { module, test } from 'qunit'; import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit'; import { setupTest } from 'ember-qunit';
import { currentURL, visit, waitUntil } from '@ember/test-helpers'; import { visit } from '@ember/test-helpers';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage'; import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import { setupIndexedDb } from 'api/test-support/helpers/indexed-db'; import { setupIndexedDb } from 'api/test-support/helpers/indexed-db';
import { authenticateSession } from 'ember-simple-auth/test-support'; import { authenticateSession } from 'ember-simple-auth/test-support';
@@ -55,10 +55,6 @@ module(
urls.authenticate = `scopes/global/authenticate/${instances.authMethod}`; urls.authenticate = `scopes/global/authenticate/${instances.authMethod}`;
}); });
test('it exists', function (assert) {
assert.ok(controller);
});
test('authenticate action saves login information and redirects to correct page', async function (assert) { test('authenticate action saves login information and redirects to correct page', async function (assert) {
await visit(urls.authenticate); await visit(urls.authenticate);
const identification = 'admin123'; const identification = 'admin123';
@@ -76,11 +72,9 @@ module(
identification, identification,
password: 'password', password: 'password',
}); });
await waitUntil(() => currentURL() === urls.globalScope);
const { authenticator: authAfter, username: usernameAfter } = const { authenticator: authAfter, username: usernameAfter } =
session.data.authenticated; session.data.authenticated;
assert.strictEqual(currentURL(), urls.globalScope);
assert.strictEqual( assert.strictEqual(
authAfter, authAfter,
`authenticator:${instances.authMethod.type}`, `authenticator:${instances.authMethod.type}`,