2025-03-02 20:28:41 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								import  sql  from  "../../src/services/sql" ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								import  utils  from  "../../src/services/utils" ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-03-02 19:39:06 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								interface  NoteContentsRow  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    noteId : string ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    content : string  |  Buffer ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    dateModified : string ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    utcDateModified : string ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								interface  NoteRevisionContents  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    noteRevisionId : string ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    content : string  |  Buffer ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    utcDateModified : string ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-03-02 20:28:41 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								export  default  ( )  = >  {  
						 
					
						
							
								
									
										
										
										
											2023-03-15 22:44:08 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    const  existingBlobIds  =  new  Set ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-04-01 23:30:21 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    for  ( const  noteId  of  sql . getColumn < string > ( /*sql*/ ` SELECT noteId FROM note_contents ` ) )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        const  row  =  sql . getRow < NoteContentsRow > ( /*sql*/ ` SELECT noteId, content, dateModified, utcDateModified FROM note_contents WHERE noteId = ? ` ,  [ noteId ] ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2023-03-15 22:44:08 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        const  blobId  =  utils . hashedBlobId ( row . content ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if  ( ! existingBlobIds . has ( blobId ) )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            existingBlobIds . add ( blobId ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-01-09 18:07:02 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            sql . insert ( "blobs" ,  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-03-15 22:44:08 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								                blobId , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                content : row.content , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                dateModified : row.dateModified , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                utcDateModified : row.utcDateModified 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            sql . execute ( "UPDATE entity_changes SET entityName = 'blobs', entityId = ? WHERE entityName = 'note_contents' AND entityId = ?" ,  [ blobId ,  row . noteId ] ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            // duplicates
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            sql . execute ( "DELETE FROM entity_changes WHERE entityName = 'note_contents' AND entityId = ?" ,  [ row . noteId ] ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-01-09 18:07:02 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        sql . execute ( "UPDATE notes SET blobId = ? WHERE noteId = ?" ,  [ blobId ,  row . noteId ] ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2023-03-15 22:44:08 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-04-01 23:30:21 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    for  ( const  noteRevisionId  of  sql . getColumn ( /*sql*/ ` SELECT noteRevisionId FROM note_revision_contents ` ) )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        const  row  =  sql . getRow < NoteRevisionContents > ( /*sql*/ ` SELECT noteRevisionId, content, utcDateModified FROM note_revision_contents WHERE noteRevisionId = ? ` ,  [ noteRevisionId ] ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2023-03-15 22:44:08 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        const  blobId  =  utils . hashedBlobId ( row . content ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if  ( ! existingBlobIds . has ( blobId ) )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            existingBlobIds . add ( blobId ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-01-09 18:07:02 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            sql . insert ( "blobs" ,  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-03-15 22:44:08 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								                blobId , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                content : row.content , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                dateModified : row.utcDateModified , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                utcDateModified : row.utcDateModified 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            sql . execute ( "UPDATE entity_changes SET entityName = 'blobs', entityId = ? WHERE entityName = 'note_revision_contents' AND entityId = ?" ,  [ blobId ,  row . noteRevisionId ] ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            // duplicates
 
							 
						 
					
						
							
								
									
										
										
										
											2025-03-02 19:39:06 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            sql . execute ( "DELETE FROM entity_changes WHERE entityName = 'note_revision_contents' AND entityId = ?" ,  [ row . noteRevisionId ] ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2023-03-15 22:44:08 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-01-09 18:07:02 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        sql . execute ( "UPDATE note_revisions SET blobId = ? WHERE noteRevisionId = ?" ,  [ blobId ,  row . noteRevisionId ] ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2023-03-15 22:44:08 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const  notesWithoutBlobIds  =  sql . getColumn ( "SELECT noteId FROM notes WHERE blobId IS NULL" ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if  ( notesWithoutBlobIds . length  >  0 )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        throw  new  Error ( "BlobIds were not filled correctly in notes: "  +  JSON . stringify ( notesWithoutBlobIds ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const  noteRevisionsWithoutBlobIds  =  sql . getColumn ( "SELECT noteRevisionId FROM note_revisions WHERE blobId IS NULL" ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if  ( noteRevisionsWithoutBlobIds . length  >  0 )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        throw  new  Error ( "BlobIds were not filled correctly in note revisions: "  +  JSON . stringify ( noteRevisionsWithoutBlobIds ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ;