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 { persisted } from '$lib/util/persist.svelte';
|
||||
import { logEvent } from './stats';
|
||||
|
||||
interface MigrationState {
|
||||
@@ -11,14 +10,10 @@ const migrations: Record<string, () => void> = {
|
||||
injectHistoryIDs
|
||||
};
|
||||
|
||||
const migrationStore: Writable<MigrationState> = persist(
|
||||
writable({ version: -1 }),
|
||||
localStorage(),
|
||||
'migrations'
|
||||
);
|
||||
const migrationState = persisted<MigrationState>('migrations', { version: -1 });
|
||||
|
||||
export const applyMigrations = (): void => {
|
||||
const { version }: MigrationState = get(migrationStore);
|
||||
const { version } = migrationState.value;
|
||||
const allMigrations = Object.entries(migrations);
|
||||
if (version === allMigrations.length - 1) {
|
||||
return;
|
||||
@@ -29,7 +24,7 @@ export const applyMigrations = (): void => {
|
||||
console.log(`Applying migration ${i}: ${key}.`);
|
||||
fn();
|
||||
logEvent('migration', { key });
|
||||
migrationStore.set({ version: i });
|
||||
migrationState.value = { version: i };
|
||||
}
|
||||
logEvent('migration', { status: 'complete', from: version, to: allMigrations.length - 1 });
|
||||
};
|
||||
@@ -14,7 +14,7 @@ describe('migrations', () => {
|
||||
});
|
||||
|
||||
it('should migrate from v0 to v1', async () => {
|
||||
const { applyMigrations } = await import('./migrations');
|
||||
const { applyMigrations } = await import('./migrations.svelte');
|
||||
let manualHistoryStore: HistoryEntry[] = JSON.parse(
|
||||
window.localStorage.getItem('manualHistoryStore') ?? '[]'
|
||||
) as HistoryEntry[];
|
||||
|
||||
@@ -3,7 +3,7 @@ import { env } from './env';
|
||||
import { loadDataFromUrl } from './fileLoaders/loader';
|
||||
import { initLoading } from './loading.svelte';
|
||||
import { isOnMermaidAI } from './migration/domainMigration';
|
||||
import { applyMigrations } from './migrations';
|
||||
import { applyMigrations } from './migrations.svelte';
|
||||
import { initURLSubscription, loadState, updateCodeStore, verifyState } from './state.svelte';
|
||||
import { getAnalyticsSafeUrl, initAnalytics, plausible } from './stats';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user