@@ -4,18 +4,27 @@ import {
44} from "@/services/storage/indexeddb-adapter" ;
55import type { StorageMigration } from "./base" ;
66import type { ProjectRecord } from "./transformers/types" ;
7- import { getProjectId } from "./transformers/utils" ;
7+ import { getProjectId , isRecord } from "./transformers/utils" ;
88
99export 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+
1320let hasCleanedUpMetaDb = false ;
1421
1522export 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+ }
0 commit comments