@@ -396,58 +396,82 @@ func (r *RestoreJob) parsePlainFile(dumpPath string) (string, error) {
396
396
397
397
// discoverDumpLocation discovers dump location to find databases ready to restore.
398
398
func (r * RestoreJob ) discoverDumpLocation (ctx context.Context , contID string ) (map [string ]DumpDefinition , error ) {
399
+ dbList := make (map [string ]DumpDefinition )
400
+
401
+ // Check the dumpLocation directory.
402
+ if dumpDefinition , err := r .getDirectoryDumpDefinition (ctx , contID , r .RestoreOptions .DumpLocation ); err == nil {
403
+ dbList ["" ] = dumpDefinition // empty string because of the root directory.
404
+
405
+ return dbList , nil
406
+ }
407
+
408
+ log .Msg (fmt .Sprintf ("Directory dump not found in %q" , r .RestoreOptions .DumpLocation ))
409
+
399
410
fileInfos , err := ioutil .ReadDir (r .RestoreOptions .DumpLocation )
400
411
if err != nil {
401
412
return nil , errors .Wrap (err , "failed to discover dump location" )
402
413
}
403
414
404
- dbList := make (map [string ]DumpDefinition )
405
-
406
415
for _ , info := range fileInfos {
407
416
log .Dbg ("Explore: " , info .Name ())
408
417
409
418
if info .IsDir () {
410
- dumpMetafilePath := path .Join (r .RestoreOptions .DumpLocation , info .Name (), dumpMetafile )
411
- if _ , err := os .Stat (dumpMetafilePath ); err != nil {
412
- log .Msg (fmt .Sprintf ("TOC file not found: %v. Skip directory: %s" , err , info .Name ()))
413
- continue
414
- }
419
+ dumpDirectory := path .Join (r .RestoreOptions .DumpLocation , info .Name ())
415
420
416
- dbName , err := r .extractDBNameFromDump (ctx , contID , path . Join ( r . RestoreOptions . DumpLocation , info . Name ()) )
421
+ dumpDefinition , err := r .getDirectoryDumpDefinition (ctx , contID , dumpDirectory )
417
422
if err != nil {
418
- log .Msg (fmt .Sprintf ("Invalid dump : %v. Skip directory: %s" , err , info .Name ()))
423
+ log .Msg (fmt .Sprintf ("Dump not found : %v. Skip directory: %s" , err , info .Name ()))
419
424
continue
420
425
}
421
426
422
- dbList [info .Name ()] = DumpDefinition {
423
- Format : directoryFormat ,
424
- dbName : dbName ,
425
- }
427
+ dbList [info .Name ()] = dumpDefinition
426
428
427
429
log .Msg ("Found the directory dump: " , info .Name ())
428
430
429
431
continue
430
432
}
431
433
432
- dbDefinition , err := r .exploreDumpFile (ctx , contID , path .Join (r .RestoreOptions .DumpLocation , info .Name ()))
434
+ dumpDefinition , err := r .exploreDumpFile (ctx , contID , path .Join (r .RestoreOptions .DumpLocation , info .Name ()))
433
435
if err != nil {
434
436
log .Dbg (fmt .Sprintf ("Skip file %q due to failure to find a database to restore: %v" , info .Name (), err ))
435
437
continue
436
438
}
437
439
438
- if dbDefinition == nil {
440
+ if dumpDefinition == nil {
439
441
log .Dbg (fmt .Sprintf ("Skip file %q because the database definition is empty" , info .Name ()))
440
442
continue
441
443
}
442
444
443
- dbList [info .Name ()] = * dbDefinition
445
+ dbList [info .Name ()] = * dumpDefinition
444
446
445
- log .Msg (fmt .Sprintf ("Found the %s dump file: %s" , dbDefinition .Format , info .Name ()))
447
+ log .Msg (fmt .Sprintf ("Found the %s dump file: %s" , dumpDefinition .Format , info .Name ()))
446
448
}
447
449
448
450
return dbList , nil
449
451
}
450
452
453
+ func (r * RestoreJob ) getDirectoryDumpDefinition (ctx context.Context , contID , dumpDir string ) (DumpDefinition , error ) {
454
+ dumpMetafilePath := path .Join (dumpDir , dumpMetafile )
455
+
456
+ if _ , err := os .Stat (dumpMetafilePath ); err != nil {
457
+ log .Msg (fmt .Sprintf ("TOC file not found: %v. Skip directory: %s" , err , dumpDir ))
458
+ return DumpDefinition {}, err
459
+ }
460
+
461
+ log .Msg (fmt .Sprintf ("TOC file has been found: %q" , dumpMetafilePath ))
462
+
463
+ dbName , err := r .extractDBNameFromDump (ctx , contID , dumpDir )
464
+ if err != nil {
465
+ log .Err ("Invalid dump: " , err )
466
+ return DumpDefinition {}, errors .Wrap (err , "invalid database name" )
467
+ }
468
+
469
+ return DumpDefinition {
470
+ Format : directoryFormat ,
471
+ dbName : dbName ,
472
+ }, nil
473
+ }
474
+
451
475
func (r * RestoreJob ) restoreDB (ctx context.Context , contID , dbName string , dbDefinition DumpDefinition ) error {
452
476
// The dump contains no database creation requests, so create a new database by ourselves.
453
477
if dbDefinition .Format == plainFormat && dbDefinition .dbName == "" {
0 commit comments