Skip to content

Commit d0b4b1a

Browse files
committed
fix migration dialog
1 parent 2257ca2 commit d0b4b1a

File tree

6 files changed

+55
-24
lines changed

6 files changed

+55
-24
lines changed

apps/web/src/core/managers/project-manager.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
CURRENT_PROJECT_VERSION,
2525
migrations,
2626
runStorageMigrations,
27+
type MigrationProgress,
2728
} from "@/services/storage/migrations";
2829
import { DEFAULT_TIMELINE_VIEW_STATE } from "@/constants/timeline-constants";
2930

@@ -58,7 +59,13 @@ export class ProjectManager {
5859
}
5960

6061
this.storageMigrationPromise = (async () => {
61-
await runStorageMigrations({ migrations });
62+
await runStorageMigrations({
63+
migrations,
64+
onProgress: (progress: MigrationProgress) => {
65+
this.migrationState = progress;
66+
this.notify();
67+
},
68+
});
6269
})();
6370

6471
await this.storageMigrationPromise;

apps/web/src/services/storage/migrations/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { V0toV1Migration } from "./v0-to-v1";
33
import { V1toV2Migration } from "./v1-to-v2";
44
import { V2toV3Migration } from "./v2-to-v3";
55
export { runStorageMigrations } from "./runner";
6+
export type { MigrationProgress } from "./runner";
67

78
export const CURRENT_PROJECT_VERSION = 3;
89

apps/web/src/services/storage/migrations/runner.ts

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,27 @@ import {
44
} from "@/services/storage/indexeddb-adapter";
55
import type { StorageMigration } from "./base";
66
import type { ProjectRecord } from "./transformers/types";
7-
import { getProjectId } from "./transformers/utils";
7+
import { getProjectId, isRecord } from "./transformers/utils";
88

99
export interface StorageMigrationResult {
1010
migratedCount: number;
1111
}
1212

13+
export interface MigrationProgress {
14+
isMigrating: boolean;
15+
fromVersion: number | null;
16+
toVersion: number | null;
17+
projectName: string | null;
18+
}
19+
1320
let hasCleanedUpMetaDb = false;
1421

1522
export async function runStorageMigrations({
1623
migrations,
24+
onProgress,
1725
}: {
1826
migrations: StorageMigration[];
27+
onProgress?: (progress: MigrationProgress) => void;
1928
}): Promise<StorageMigrationResult> {
2029
// One-time cleanup: delete the old global version database
2130
if (!hasCleanedUpMetaDb) {
@@ -44,8 +53,20 @@ export async function runStorageMigrations({
4453

4554
let projectRecord = project as ProjectRecord;
4655
let currentVersion = getProjectVersion({ project: projectRecord });
56+
const targetVersion = orderedMigrations.at(-1)?.to ?? currentVersion;
57+
58+
if (currentVersion >= targetVersion) {
59+
continue;
60+
}
61+
62+
const projectName = getProjectName({ project: projectRecord });
63+
onProgress?.({
64+
isMigrating: true,
65+
fromVersion: currentVersion,
66+
toVersion: targetVersion,
67+
projectName,
68+
});
4769

48-
// Apply migrations sequentially until project is up to date
4970
for (const migration of orderedMigrations) {
5071
if (migration.from !== currentVersion) {
5172
continue;
@@ -54,24 +75,28 @@ export async function runStorageMigrations({
5475
const result = await migration.transform(projectRecord);
5576

5677
if (result.skipped) {
57-
break; // Project is already at this version or higher
78+
break;
5879
}
5980

60-
// Update project with migrated version
6181
const projectId = getProjectId({ project: result.project });
6282
if (!projectId) {
63-
break; // Can't save without ID
83+
break;
6484
}
6585

6686
await projectsAdapter.set(projectId, result.project);
6787
migratedCount++;
6888
currentVersion = migration.to;
69-
70-
// Use migrated project for next iteration
7189
projectRecord = result.project;
7290
}
7391
}
7492

93+
onProgress?.({
94+
isMigrating: false,
95+
fromVersion: null,
96+
toVersion: null,
97+
projectName: null,
98+
});
99+
75100
return { migratedCount };
76101
}
77102

@@ -92,3 +117,17 @@ function getProjectVersion({ project }: { project: ProjectRecord }): number {
92117
// v0 - no scenes
93118
return 0;
94119
}
120+
121+
function getProjectName({ project }: { project: ProjectRecord }): string | null {
122+
const metadata = project.metadata;
123+
if (isRecord(metadata) && typeof metadata.name === "string") {
124+
return metadata.name;
125+
}
126+
127+
// v0 had name directly on project
128+
if (typeof project.name === "string") {
129+
return project.name;
130+
}
131+
132+
return null;
133+
}

apps/web/src/services/storage/migrations/transformers/v1-to-v2.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ async function migrateProject({
148148

149149
const transformedTracks = await transformTracks({
150150
tracks,
151-
projectId,
152151
loadMediaAsset,
153152
});
154153

@@ -252,11 +251,9 @@ async function loadTracksFromLegacyDB({
252251

253252
async function transformTracks({
254253
tracks,
255-
projectId,
256254
loadMediaAsset,
257255
}: {
258256
tracks: unknown[];
259-
projectId: string;
260257
loadMediaAsset?: ({
261258
mediaId,
262259
}: {
@@ -279,7 +276,6 @@ async function transformTracks({
279276
if (trackType === "media") {
280277
const videoTrack = await transformMediaTrack({
281278
track: track as LegacyMediaTrack,
282-
projectId,
283279
loadMediaAsset,
284280
isMain: !isFirstVideoTrackFound,
285281
});
@@ -306,12 +302,10 @@ async function transformTracks({
306302

307303
async function transformMediaTrack({
308304
track,
309-
projectId,
310305
loadMediaAsset,
311306
isMain,
312307
}: {
313308
track: LegacyMediaTrack;
314-
projectId: string;
315309
loadMediaAsset?: ({
316310
mediaId,
317311
}: {

apps/web/src/services/storage/migrations/v0-to-v1.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
/*
2-
* Ensures every project has at least one scene
3-
* Adds a default "Main scene" if none exist
4-
* Sets currentSceneId to the new scene's id
5-
*/
6-
71
import { StorageMigration } from "./base";
82
import type { ProjectRecord } from "./transformers/types";
93
import { transformProjectV0ToV1 } from "./transformers/v0-to-v1";

apps/web/src/services/storage/migrations/v2-to-v3.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/*
2-
* Adds a "duration" field to each project's metadata
3-
*/
4-
51
import { StorageMigration } from "./base";
62
import type { ProjectRecord } from "./transformers/types";
73
import { transformProjectV2ToV3 } from "./transformers/v2-to-v3";

0 commit comments

Comments
 (0)