@@ -11,7 +11,17 @@ import { existsSync } from "fs"
1111export namespace JsonMigration {
1212 const log = Log . create ( { service : "json-migration" } )
1313
14- export async function run ( sqlite : Database ) {
14+ export type Progress = {
15+ current : number
16+ total : number
17+ label : string
18+ }
19+
20+ type Options = {
21+ progress ?: ( event : Progress ) => void
22+ }
23+
24+ export async function run ( sqlite : Database , options ?: Options ) {
1525 const storageDir = path . join ( Global . Path . data , "storage" )
1626
1727 if ( ! existsSync ( storageDir ) ) {
@@ -120,6 +130,25 @@ export namespace JsonMigration {
120130 shares : shareFiles . length ,
121131 } )
122132
133+ const total = Math . max (
134+ 1 ,
135+ projectFiles . length +
136+ sessionFiles . length +
137+ messageFiles . length +
138+ partFiles . length +
139+ todoFiles . length +
140+ permFiles . length +
141+ shareFiles . length ,
142+ )
143+ const progress = options ?. progress
144+ let current = 0
145+ const step = ( label : string , count : number ) => {
146+ current = Math . min ( total , current + count )
147+ progress ?.( { current, total, label } )
148+ }
149+
150+ progress ?.( { current, total, label : "starting" } )
151+
123152 sqlite . exec ( "BEGIN TRANSACTION" )
124153
125154 // Migrate projects first (no FK deps)
@@ -151,6 +180,7 @@ export namespace JsonMigration {
151180 } )
152181 }
153182 stats . projects += insert ( projectValues , ProjectTable , "project" )
183+ step ( "projects" , end - i )
154184 }
155185 log . info ( "migrated projects" , { count : stats . projects , duration : Math . round ( performance . now ( ) - start ) } )
156186
@@ -195,6 +225,7 @@ export namespace JsonMigration {
195225 } )
196226 }
197227 stats . sessions += insert ( sessionValues , SessionTable , "session" )
228+ step ( "sessions" , end - i )
198229 }
199230 log . info ( "migrated sessions" , { count : stats . sessions } )
200231 if ( orphans . sessions > 0 ) {
@@ -241,6 +272,7 @@ export namespace JsonMigration {
241272 }
242273 values . length = count
243274 stats . messages += insert ( values , MessageTable , "message" )
275+ step ( "messages" , end - i )
244276 }
245277 log . info ( "migrated messages" , { count : stats . messages } )
246278
@@ -281,6 +313,7 @@ export namespace JsonMigration {
281313 }
282314 values . length = count
283315 stats . parts += insert ( values , PartTable , "part" )
316+ step ( "parts" , end - i )
284317 }
285318 log . info ( "migrated parts" , { count : stats . parts } )
286319
@@ -317,6 +350,7 @@ export namespace JsonMigration {
317350 }
318351 }
319352 stats . todos += insert ( values , TodoTable , "todo" )
353+ step ( "todos" , end - i )
320354 }
321355 log . info ( "migrated todos" , { count : stats . todos } )
322356 if ( orphans . todos > 0 ) {
@@ -341,6 +375,7 @@ export namespace JsonMigration {
341375 permValues . push ( { project_id : projectID , data } )
342376 }
343377 stats . permissions += insert ( permValues , PermissionTable , "permission" )
378+ step ( "permissions" , end - i )
344379 }
345380 log . info ( "migrated permissions" , { count : stats . permissions } )
346381 if ( orphans . permissions > 0 ) {
@@ -369,6 +404,7 @@ export namespace JsonMigration {
369404 shareValues . push ( { session_id : sessionID , id : data . id , secret : data . secret , url : data . url } )
370405 }
371406 stats . shares += insert ( shareValues , SessionShareTable , "session_share" )
407+ step ( "shares" , end - i )
372408 }
373409 log . info ( "migrated session shares" , { count : stats . shares } )
374410 if ( orphans . shares > 0 ) {
@@ -393,6 +429,8 @@ export namespace JsonMigration {
393429 log . warn ( "migration errors" , { errors : stats . errors . slice ( 0 , 20 ) } )
394430 }
395431
432+ progress ?.( { current : total , total, label : "complete" } )
433+
396434 return stats
397435 }
398436}
0 commit comments