@ -47,14 +47,14 @@ open class Storage {
 
		
	
		
			
				    
 
		
	
		
			
				    public  init ( 
 
		
	
		
			
				        customWriter :  DatabaseWriter ?  =  nil , 
 
		
	
		
			
				        customMigration s:  [ TargetMigrations  ] ?  =  nil 
 
		
	
		
			
				        customMigration Targets:  [ MigratableTarget . Type  ] ?  =  nil 
 
		
	
		
			
				    )  { 
 
		
	
		
			
				        configureDatabase ( customWriter :  customWriter ,  customMigration s:  customMigration s) 
 
		
	
		
			
				        configureDatabase ( customWriter :  customWriter ,  customMigration Target s:  customMigration Target s) 
 
		
	
		
			
				    } 
 
		
	
		
			
				    
 
		
	
		
			
				    private  func  configureDatabase ( 
 
		
	
		
			
				        customWriter :  DatabaseWriter ?  =  nil , 
 
		
	
		
			
				        customMigration s:  [ TargetMigrations  ] ?  =  nil 
 
		
	
		
			
				        customMigration Targets:  [ MigratableTarget . Type  ] ?  =  nil 
 
		
	
		
			
				    )  { 
 
		
	
		
			
				        //   C r e a t e   t h e   d a t a b a s e   d i r e c t o r y   i f   n e e d e d   a n d   e n s u r e   i t ' s   p r o t e c t i o n   l e v e l   i s   s e t   b e f o r e   a t t e m p t i n g   t o 
 
		
	
		
			
				        //   c r e a t e   t h e   d a t a b a s e   K e y S p e c   o r   t h e   d a t a b a s e   i t s e l f 
 
		
	
	
		
			
				
					
						
						
						
							
								 
						
					 
				
			
			@ -66,7 +66,12 @@ open class Storage {
 
		
	
		
			
				            dbWriter  =  customWriter 
 
		
	
		
			
				            isValid  =  true 
 
		
	
		
			
				            Storage . internalHasCreatedValidInstance . mutate  {  $0  =  true  } 
 
		
	
		
			
				            perform ( migrations :  ( customMigrations  ? ?  [ ] ) ,  async :  false ,  onProgressUpdate :  nil ,  onComplete :  {  _ ,  _  in  } ) 
 
		
	
		
			
				            perform ( 
 
		
	
		
			
				                migrationTargets :  ( customMigrationTargets  ? ?  [ ] ) , 
 
		
	
		
			
				                async :  false , 
 
		
	
		
			
				                onProgressUpdate :  nil , 
 
		
	
		
			
				                onComplete :  {  _ ,  _  in  } 
 
		
	
		
			
				            ) 
 
		
	
		
			
				            return 
 
		
	
		
			
				        } 
 
		
	
		
			
				        
 
		
	
	
		
			
				
					
						
							
								 
						
						
							
								 
						
						
					 
				
			
			@ -128,7 +133,7 @@ open class Storage {
 
		
	
		
			
				    } 
 
		
	
		
			
				    
 
		
	
		
			
				    public  func  perform ( 
 
		
	
		
			
				        migration s:  [ TargetMigrations  ] , 
 
		
	
		
			
				        migration Targets:  [ MigratableTarget . Type  ] , 
 
		
	
		
			
				        async :  Bool  =  true , 
 
		
	
		
			
				        onProgressUpdate :  ( ( CGFloat ,  TimeInterval )  ->  ( ) ) ? , 
 
		
	
		
			
				        onComplete :  @ escaping  ( Swift . Result < Void ,  Error > ,  Bool )  ->  ( ) 
 
		
	
	
		
			
				
					
						
						
						
							
								 
						
					 
				
			
			@ -141,18 +146,28 @@ open class Storage {
 
		
	
		
			
				        } 
 
		
	
		
			
				        
 
		
	
		
			
				        typealias  MigrationInfo  =  ( identifier :  TargetMigrations . Identifier ,  migrations :  TargetMigrations . MigrationSet ) 
 
		
	
		
			
				        let  sortedMigrationInfo :  [ MigrationInfo ]  =  migrations 
 
		
	
		
			
				            . sorted ( ) 
 
		
	
		
			
				            . reduce ( into :  [ [ MigrationInfo ] ] ( ) )  {  result ,  next  in 
 
		
	
		
			
				                next . migrations . enumerated ( ) . forEach  {  index ,  migrationSet  in 
 
		
	
		
			
				                    if  result . count  <=  index  { 
 
		
	
		
			
				                        result . append ( [ ] ) 
 
		
	
		
			
				                    } 
 
		
	
		
			
				        let  maybeSortedMigrationInfo :  [ MigrationInfo ] ?  =  try ?  dbWriter 
 
		
	
		
			
				            . read  {  db  ->  [ MigrationInfo ]  in 
 
		
	
		
			
				                migrationTargets 
 
		
	
		
			
				                    . map  {  target  ->  TargetMigrations  in  target . migrations ( db )  } 
 
		
	
		
			
				                    . sorted ( ) 
 
		
	
		
			
				                    . reduce ( into :  [ [ MigrationInfo ] ] ( ) )  {  result ,  next  in 
 
		
	
		
			
				                        next . migrations . enumerated ( ) . forEach  {  index ,  migrationSet  in 
 
		
	
		
			
				                            if  result . count  <=  index  { 
 
		
	
		
			
				                                result . append ( [ ] ) 
 
		
	
		
			
				                            } 
 
		
	
		
			
				
 
		
	
		
			
				                    result [ index ]  =  ( result [ index ]  +  [ ( next . identifier ,  migrationSet ) ] ) 
 
		
	
		
			
				                } 
 
		
	
		
			
				                            result [ index ]  =  ( result [ index ]  +  [ ( next . identifier ,  migrationSet ) ] ) 
 
		
	
		
			
				                        } 
 
		
	
		
			
				                    } 
 
		
	
		
			
				                    . reduce ( into :  [ ] )  {  result ,  next  in  result . append ( contentsOf :  next )  } 
 
		
	
		
			
				            } 
 
		
	
		
			
				            . reduce ( into :  [ ] )  {  result ,  next  in  result . append ( contentsOf :  next )  } 
 
		
	
		
			
				        
 
		
	
		
			
				        guard  let  sortedMigrationInfo :  [ MigrationInfo ]  =  maybeSortedMigrationInfo  else  { 
 
		
	
		
			
				            SNLog ( " [Database Error] Statup failed with error: Unable to prepare migrations " ) 
 
		
	
		
			
				            onComplete ( . failure ( StorageError . startupFailed ) ,  false ) 
 
		
	
		
			
				            return 
 
		
	
		
			
				        } 
 
		
	
		
			
				        
 
		
	
		
			
				        //   S e t u p   a n d   r u n   a n y   r e q u i r e d   m i g r a t i o n s 
 
		
	
		
			
				        migrator  =  {  [ weak  self ]  in