Skip to content

Commit

Permalink
fix: repair broken config once migration completed
Browse files Browse the repository at this point in the history
  • Loading branch information
vitonsky committed Jun 19, 2024
1 parent 28fc793 commit 069e6bf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
24 changes: 22 additions & 2 deletions src/app/ConfigStorage/ConfigStorage.migrations.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import browser from 'webextension-polyfill';

import { DEFAULT_TRANSLATOR, DEFAULT_TTS } from '../../config';
import { DEFAULT_TRANSLATOR, DEFAULT_TTS, defaultConfig } from '../../config';
import { createMigrationTask, Migration } from '../../lib/migrations/createMigrationTask';
import { decodeStruct } from '../../lib/types';
import { AppConfig } from '../../types/runtime';

const migrations: Migration[] = [
{
Expand Down Expand Up @@ -131,6 +133,24 @@ const migrations: Migration[] = [
await browser.storage.local.set({ [storageName]: updatedConfig });
},
},
{
version: 7,
async migrate() {
// Empty migration, to bump migration number and to trigger hook for repair config
},
},
];

export const ConfigStorageMigration = createMigrationTask(migrations);
export const ConfigStorageMigration = createMigrationTask(migrations, {
onComplete: async () => {
// Repair config if necessary
const storageName = 'appConfig';
const { [storageName]: config } = await browser.storage.local.get(storageName);

const { errors } = decodeStruct(AppConfig, config);
if (errors === null) return;

console.warn('Config object is invalid, fallback to default config', errors);
await browser.storage.local.set({ [storageName]: defaultConfig });
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ export class PersistentMigrationsExecutor {

await migration.migrate(currentVersion, latestVersion);

const { hooks = {} } = migration;
if (hooks.onComplete) {
await hooks.onComplete();
}

// Update storage version
migrationsVersions[name] = latestVersion;
await this.storage.setMigrationsVersions(migrationsVersions);
Expand Down
12 changes: 11 additions & 1 deletion src/lib/migrations/createMigrationTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export type Migration = {
migrate: (previousVersion: number) => Promise<void>;
};

export type MigrationHooks = {
onComplete?: () => Promise<void>;
};

/**
* Task to migrate from one version to another
* Task may execute several migrations, so it may take a time
Expand All @@ -29,12 +33,17 @@ export type MigrationTask = {
* WARNING: previous version may contain `0` in case when data structure run migration first time
*/
migrate: (previousVersion: number, currentVersion: number) => Promise<void>;

hooks?: MigrationHooks;
};

/**
* Build migration task from migrations list
*/
export const createMigrationTask = (migrations: Migration[]): MigrationTask => {
export const createMigrationTask = (
migrations: Migration[],
hooks?: MigrationHooks,
): MigrationTask => {
const sortedMigrations = migrations.sort(
(migration1, migration2) => migration1.version - migration2.version,
);
Expand All @@ -47,6 +56,7 @@ export const createMigrationTask = (migrations: Migration[]): MigrationTask => {

return {
version: lastMigrationVersion,
hooks,
migrate: async (fromVersion: number, toVersion: number) => {
const migrationsToApply = sortedMigrations.filter(
(migration) =>
Expand Down

0 comments on commit 069e6bf

Please sign in to comment.