@@ -396,58 +396,82 @@ func (r *RestoreJob) parsePlainFile(dumpPath string) (string, error) {
396396
397397// discoverDumpLocation discovers dump location to find databases ready to restore.
398398func (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+
399410 fileInfos , err := ioutil .ReadDir (r .RestoreOptions .DumpLocation )
400411 if err != nil {
401412 return nil , errors .Wrap (err , "failed to discover dump location" )
402413 }
403414
404- dbList := make (map [string ]DumpDefinition )
405-
406415 for _ , info := range fileInfos {
407416 log .Dbg ("Explore: " , info .Name ())
408417
409418 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 ())
415420
416- dbName , err := r .extractDBNameFromDump (ctx , contID , path . Join ( r . RestoreOptions . DumpLocation , info . Name ()) )
421+ dumpDefinition , err := r .getDirectoryDumpDefinition (ctx , contID , dumpDirectory )
417422 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 ()))
419424 continue
420425 }
421426
422- dbList [info .Name ()] = DumpDefinition {
423- Format : directoryFormat ,
424- dbName : dbName ,
425- }
427+ dbList [info .Name ()] = dumpDefinition
426428
427429 log .Msg ("Found the directory dump: " , info .Name ())
428430
429431 continue
430432 }
431433
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 ()))
433435 if err != nil {
434436 log .Dbg (fmt .Sprintf ("Skip file %q due to failure to find a database to restore: %v" , info .Name (), err ))
435437 continue
436438 }
437439
438- if dbDefinition == nil {
440+ if dumpDefinition == nil {
439441 log .Dbg (fmt .Sprintf ("Skip file %q because the database definition is empty" , info .Name ()))
440442 continue
441443 }
442444
443- dbList [info .Name ()] = * dbDefinition
445+ dbList [info .Name ()] = * dumpDefinition
444446
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 ()))
446448 }
447449
448450 return dbList , nil
449451}
450452
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+
451475func (r * RestoreJob ) restoreDB (ctx context.Context , contID , dbName string , dbDefinition DumpDefinition ) error {
452476 // The dump contains no database creation requests, so create a new database by ourselves.
453477 if dbDefinition .Format == plainFormat && dbDefinition .dbName == "" {
0 commit comments