refactor: Migrate the migrations store to the persisted rune
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
81aaafc085
commit
a49503a1f0
@@ -1,6 +1,5 @@
|
|||||||
import { writable, get, type Writable } from 'svelte/store';
|
|
||||||
import { persist, localStorage } from '$lib/util/persist';
|
|
||||||
import { injectHistoryIDs } from '$lib/components/History/historyState.svelte';
|
import { injectHistoryIDs } from '$lib/components/History/historyState.svelte';
|
||||||
|
import { persisted } from '$lib/util/persist.svelte';
|
||||||
import { logEvent } from './stats';
|
import { logEvent } from './stats';
|
||||||
|
|
||||||
interface MigrationState {
|
interface MigrationState {
|
||||||
@@ -11,14 +10,10 @@ const migrations: Record<string, () => void> = {
|
|||||||
injectHistoryIDs
|
injectHistoryIDs
|
||||||
};
|
};
|
||||||
|
|
||||||
const migrationStore: Writable<MigrationState> = persist(
|
const migrationState = persisted<MigrationState>('migrations', { version: -1 });
|
||||||
writable({ version: -1 }),
|
|
||||||
localStorage(),
|
|
||||||
'migrations'
|
|
||||||
);
|
|
||||||
|
|
||||||
export const applyMigrations = (): void => {
|
export const applyMigrations = (): void => {
|
||||||
const { version }: MigrationState = get(migrationStore);
|
const { version } = migrationState.value;
|
||||||
const allMigrations = Object.entries(migrations);
|
const allMigrations = Object.entries(migrations);
|
||||||
if (version === allMigrations.length - 1) {
|
if (version === allMigrations.length - 1) {
|
||||||
return;
|
return;
|
||||||
@@ -29,7 +24,7 @@ export const applyMigrations = (): void => {
|
|||||||
console.log(`Applying migration ${i}: ${key}.`);
|
console.log(`Applying migration ${i}: ${key}.`);
|
||||||
fn();
|
fn();
|
||||||
logEvent('migration', { key });
|
logEvent('migration', { key });
|
||||||
migrationStore.set({ version: i });
|
migrationState.value = { version: i };
|
||||||
}
|
}
|
||||||
logEvent('migration', { status: 'complete', from: version, to: allMigrations.length - 1 });
|
logEvent('migration', { status: 'complete', from: version, to: allMigrations.length - 1 });
|
||||||
};
|
};
|
||||||
@@ -14,7 +14,7 @@ describe('migrations', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should migrate from v0 to v1', async () => {
|
it('should migrate from v0 to v1', async () => {
|
||||||
const { applyMigrations } = await import('./migrations');
|
const { applyMigrations } = await import('./migrations.svelte');
|
||||||
let manualHistoryStore: HistoryEntry[] = JSON.parse(
|
let manualHistoryStore: HistoryEntry[] = JSON.parse(
|
||||||
window.localStorage.getItem('manualHistoryStore') ?? '[]'
|
window.localStorage.getItem('manualHistoryStore') ?? '[]'
|
||||||
) as HistoryEntry[];
|
) as HistoryEntry[];
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { env } from './env';
|
|||||||
import { loadDataFromUrl } from './fileLoaders/loader';
|
import { loadDataFromUrl } from './fileLoaders/loader';
|
||||||
import { initLoading } from './loading.svelte';
|
import { initLoading } from './loading.svelte';
|
||||||
import { isOnMermaidAI } from './migration/domainMigration';
|
import { isOnMermaidAI } from './migration/domainMigration';
|
||||||
import { applyMigrations } from './migrations';
|
import { applyMigrations } from './migrations.svelte';
|
||||||
import { initURLSubscription, loadState, updateCodeStore, verifyState } from './state.svelte';
|
import { initURLSubscription, loadState, updateCodeStore, verifyState } from './state.svelte';
|
||||||
import { getAnalyticsSafeUrl, initAnalytics, plausible } from './stats';
|
import { getAnalyticsSafeUrl, initAnalytics, plausible } from './stats';
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user