|
| 1 | +// Legacy migration marker (#280 "Legacy migration marker", Phase 2 of #280 / |
| 2 | +// issue #284). One-shot conversion of the pre-aggregate flat localStorage |
| 3 | +// state into one atomic StoredWorkspaceV1: |
| 4 | +// |
| 5 | +// 1. read legacy values (the caller supplies them — this module is pure); |
| 6 | +// 2. build one candidate StoredWorkspaceV1; |
| 7 | +// 3. create the initial Dashboard from legacy favorites (`spec.favorite`); |
| 8 | +// 4. convert the legacy layout preferences (asb:dashLayout/asb:dashCols) to a |
| 9 | +// valid flow@1 layout; |
| 10 | +// 5. validate the WHOLE candidate (via the repository's commit); |
| 11 | +// 6. persist it atomically; |
| 12 | +// 7. treat a successful aggregate write as migration completion. |
| 13 | +// |
| 14 | +// The marker is aggregate RECORD EXISTENCE — migration runs only when the |
| 15 | +// store holds no record (checked via `store.read()`), never keyed on |
| 16 | +// loadCurrent validity, so a present-but-corrupt aggregate is never clobbered |
| 17 | +// by a re-run. Legacy keys are NEVER deleted or modified here: Phase 2 still |
| 18 | +// serves the favorites-driven UI off them, and #280 forbids touching them |
| 19 | +// before the aggregate write succeeds. Removing the legacy reads (and the |
| 20 | +// `spec.favorite` dual-write) is the documented Phase 3-5 removal path, not |
| 21 | +// this phase. |
| 22 | +// |
| 23 | +// Pure over injected seams: the query-collection is decoded by the caller, the |
| 24 | +// ID generator is injected, and persistence goes through the injected |
| 25 | +// WorkspaceStore/WorkspaceRepository — no DOM, no storage, no crypto import. |
| 26 | + |
| 27 | +import { queryFavorite } from '../core/saved-query.js'; |
| 28 | +import { activeDashboardView } from '../core/dashboard.js'; |
| 29 | +import { queryDashboardRole } from '../dashboard/model/workspace-semantics.js'; |
| 30 | +import { CURRENT_STORAGE_VERSION, DEFAULT_WORKSPACE_NAME } from './workspace-operations.js'; |
| 31 | +import type { WorkspaceIdGen } from './workspace-operations.js'; |
| 32 | +import type { WorkspaceStore } from './workspace-store.types.js'; |
| 33 | +import type { WorkspaceRepository, WorkspaceCommitResult } from './workspace-repository.js'; |
| 34 | +import type { WorkspaceDiagnostic } from '../dashboard/model/workspace-diagnostics.js'; |
| 35 | +import type { |
| 36 | + FlowPresetV1, SavedQueryV2, StoredWorkspaceV1, |
| 37 | +} from '../generated/json-schema.types.js'; |
| 38 | + |
| 39 | +/** The flat legacy persistence the caller reads out of localStorage before |
| 40 | + * migrating: the decoded saved-query collection (asb:saved), the Library name |
| 41 | + * (asb:libraryName), and the two Dashboard layout preferences |
| 42 | + * (asb:dashLayout/asb:dashCols). */ |
| 43 | +export interface LegacyWorkspaceInput { |
| 44 | + name: string; |
| 45 | + queries: readonly SavedQueryV2[]; |
| 46 | + dashLayout: string; |
| 47 | + dashCols: number; |
| 48 | +} |
| 49 | + |
| 50 | +/** Map the legacy Dashboard layout preferences to a normative flow@1 preset. |
| 51 | + * Reuses the existing `activeDashboardView` derivation (core/dashboard.ts) and |
| 52 | + * remaps its `wide` value to the flow preset name `full-width`; `report`, |
| 53 | + * `columns-2`, and `columns-3` already match the flow preset names. */ |
| 54 | +export function legacyLayoutToFlowPreset(dashLayout: string, dashCols: number): FlowPresetV1 { |
| 55 | + const view = activeDashboardView({ dashLayout, dashCols }); |
| 56 | + return view === 'wide' ? 'full-width' : view; |
| 57 | +} |
| 58 | + |
| 59 | +/** Build the one candidate StoredWorkspaceV1 from the legacy state (steps 2-4). |
| 60 | + * The initial Dashboard's tiles are the PANEL-role favorites in catalog order: |
| 61 | + * a favorited filter/setup-role query cannot be a tile by the #280 contract, |
| 62 | + * so it is not turned into one (it stays in the query collection). The |
| 63 | + * Dashboard is always created so the legacy layout preference is preserved |
| 64 | + * even for a user with no favorites yet; it starts at revision 1. `genId` is |
| 65 | + * called once for the workspace ID, once for the Dashboard ID, and once per |
| 66 | + * tile. The candidate is NOT validated here — the caller validates the whole |
| 67 | + * thing through `repository.commit`. */ |
| 68 | +export function buildLegacyMigrationCandidate( |
| 69 | + legacy: LegacyWorkspaceInput, genId: WorkspaceIdGen, |
| 70 | +): StoredWorkspaceV1 { |
| 71 | + const name = legacy.name.trim() ? legacy.name : DEFAULT_WORKSPACE_NAME; |
| 72 | + const queries = [...legacy.queries]; |
| 73 | + const workspaceId = genId(); |
| 74 | + const dashboardId = genId(); |
| 75 | + const tiles = queries |
| 76 | + .filter((query) => queryFavorite(query) && queryDashboardRole(query) === 'panel') |
| 77 | + .map((query) => ({ id: genId(), queryId: query.id })); |
| 78 | + return { |
| 79 | + storageVersion: CURRENT_STORAGE_VERSION, |
| 80 | + id: workspaceId, |
| 81 | + name, |
| 82 | + queries, |
| 83 | + dashboard: { |
| 84 | + documentVersion: 1, |
| 85 | + id: dashboardId, |
| 86 | + title: name, |
| 87 | + revision: 1, |
| 88 | + layout: { |
| 89 | + type: 'flow', |
| 90 | + version: 1, |
| 91 | + preset: legacyLayoutToFlowPreset(legacy.dashLayout, legacy.dashCols), |
| 92 | + items: {}, |
| 93 | + }, |
| 94 | + filters: [], |
| 95 | + tiles, |
| 96 | + }, |
| 97 | + }; |
| 98 | +} |
| 99 | + |
| 100 | +/** The outcome of `migrateLegacyWorkspaceIfNeeded`. `migrated: false` with |
| 101 | + * `reason: 'aggregate-exists'` means the marker found a record and skipped; |
| 102 | + * `reason: 'commit-failed'` carries the whole-candidate validation or |
| 103 | + * persistence diagnostics (legacy keys were left intact). */ |
| 104 | +export type MigrationResult = |
| 105 | + | { migrated: true; result: Extract<WorkspaceCommitResult, { ok: true }> } |
| 106 | + | { migrated: false; reason: 'aggregate-exists' } |
| 107 | + | { migrated: false; reason: 'commit-failed'; diagnostics: WorkspaceDiagnostic[] }; |
| 108 | + |
| 109 | +export interface MigrationDeps { |
| 110 | + /** Checked for record existence — the migration marker. */ |
| 111 | + store: WorkspaceStore; |
| 112 | + /** The repository whose atomic `commit` validates + persists the candidate. */ |
| 113 | + repository: WorkspaceRepository; |
| 114 | + legacy: LegacyWorkspaceInput; |
| 115 | + genId: WorkspaceIdGen; |
| 116 | +} |
| 117 | + |
| 118 | +/** Run the one-shot migration when — and only when — no aggregate record |
| 119 | + * exists yet. Idempotent: once the aggregate persists, a later call finds the |
| 120 | + * record and returns `aggregate-exists` without rebuilding or rewriting. A |
| 121 | + * failed commit leaves the store (and every legacy key) untouched, so a retry |
| 122 | + * on the next load is safe. */ |
| 123 | +export async function migrateLegacyWorkspaceIfNeeded(deps: MigrationDeps): Promise<MigrationResult> { |
| 124 | + const { store, repository, legacy, genId } = deps; |
| 125 | + const existing = await store.read(); |
| 126 | + if (existing !== null) return { migrated: false, reason: 'aggregate-exists' }; |
| 127 | + const candidate = buildLegacyMigrationCandidate(legacy, genId); |
| 128 | + const result = await repository.commit(candidate); |
| 129 | + if (!result.ok) return { migrated: false, reason: 'commit-failed', diagnostics: result.diagnostics }; |
| 130 | + return { migrated: true, result }; |
| 131 | +} |
0 commit comments