@@ -241,7 +241,9 @@ func (d *DumpJob) Reload(cfg map[string]interface{}) (err error) {
241241func (d * DumpJob ) Run (ctx context.Context ) (err error ) {
242242 log .Msg ("Run job: " , d .Name ())
243243
244- isEmpty , err := tools .IsEmptyDirectory (d .fsPool .DataDir ())
244+ dataDir := d .fsPool .DataDir ()
245+
246+ isEmpty , err := tools .IsEmptyDirectory (dataDir )
245247 if err != nil {
246248 return errors .Wrap (err , "failed to explore the data directory" )
247249 }
@@ -252,6 +254,10 @@ func (d *DumpJob) Run(ctx context.Context) (err error) {
252254 }
253255
254256 log .Msg ("The data directory is not empty. Existing data may be overwritten." )
257+
258+ if err := updateConfigs (dataDir , d .DumpOptions .Restore .Configs ); err != nil {
259+ return fmt .Errorf ("failed to update configs: %w" , err )
260+ }
255261 }
256262
257263 if err := tools .PullImage (ctx , d .dockerClient , d .DockerImage ); err != nil {
@@ -289,12 +295,12 @@ func (d *DumpJob) Run(ctx context.Context) (err error) {
289295 log .Msg (fmt .Sprintf ("Running container: %s. ID: %v" , d .dumpContainerName (), containerID ))
290296
291297 if err := d .dockerClient .ContainerStart (ctx , containerID , types.ContainerStartOptions {}); err != nil {
292- collectDiagnostics (ctx , d .dockerClient , d .dumpContainerName (), d . fsPool . DataDir () )
298+ collectDiagnostics (ctx , d .dockerClient , d .dumpContainerName (), dataDir )
293299 return errors .Wrapf (err , "failed to start container %q" , d .dumpContainerName ())
294300 }
295301
296302 if err := d .setupConnectionOptions (ctx ); err != nil {
297- collectDiagnostics (ctx , d .dockerClient , d .dumpContainerName (), d . fsPool . DataDir () )
303+ collectDiagnostics (ctx , d .dockerClient , d .dumpContainerName (), dataDir )
298304 return errors .Wrap (err , "failed to setup connection options" )
299305 }
300306
@@ -304,8 +310,6 @@ func (d *DumpJob) Run(ctx context.Context) (err error) {
304310 return err
305311 }
306312
307- dataDir := d .fsPool .DataDir ()
308-
309313 pgDataDir := tmpDBLabPGDataDir
310314 if d .DumpOptions .Restore .Enabled {
311315 pgDataDir = dataDir
@@ -317,16 +321,15 @@ func (d *DumpJob) Run(ctx context.Context) (err error) {
317321 return errors .Wrap (err , "failed to readiness check" )
318322 }
319323
320- if err := setupPGData (ctx , d .dockerClient , pgDataDir , containerID ); err != nil {
321- collectDiagnostics (ctx , d .dockerClient , d .dumpContainerName (), pgDataDir )
322- return errors .Wrap (err , "failed to set up Postgres data" )
324+ var configs map [string ]string
325+
326+ if d .DumpOptions .Restore .Enabled {
327+ configs = d .DumpOptions .Restore .Configs
323328 }
324- }
325329
326- if d .DumpOptions .Restore .Enabled && len (d .DumpOptions .Restore .Configs ) > 0 {
327- if err := updateConfigs (ctx , d .dockerClient , dataDir , containerID , d .dumpContainerName (), d .DumpOptions .Restore .Configs ); err != nil {
330+ if err := setupPGData (ctx , d .dockerClient , pgDataDir , containerID , configs ); err != nil {
328331 collectDiagnostics (ctx , d .dockerClient , d .dumpContainerName (), pgDataDir )
329- return errors .Wrap (err , "failed to update configs " )
332+ return errors .Wrap (err , "failed to set up Postgres data " )
330333 }
331334 }
332335
@@ -484,7 +487,7 @@ func (d *DumpJob) dumpDatabase(ctx context.Context, dumpContID, dbName string, d
484487 return nil
485488}
486489
487- func setupPGData (ctx context.Context , dockerClient * client.Client , dataDir string , dumpContID string ) error {
490+ func setupPGData (ctx context.Context , dockerClient * client.Client , dataDir , dumpContID string , configs map [ string ] string ) error {
488491 entryList , err := tools .LsContainerDirectory (ctx , dockerClient , dumpContID , dataDir )
489492 if err != nil {
490493 return errors .Wrap (err , "failed to explore the data directory" )
@@ -507,6 +510,10 @@ func setupPGData(ctx context.Context, dockerClient *client.Client, dataDir strin
507510
508511 log .Dbg ("Database has been initialized" )
509512
513+ if err := updateConfigs (dataDir , configs ); err != nil {
514+ return fmt .Errorf ("failed to update configs: %w" , err )
515+ }
516+
510517 if err := tools .StartPostgres (ctx , dockerClient , dumpContID , tools .DefaultStopTimeout ); err != nil {
511518 return errors .Wrap (err , "failed to init Postgres" )
512519 }
@@ -516,17 +523,11 @@ func setupPGData(ctx context.Context, dockerClient *client.Client, dataDir strin
516523 return nil
517524}
518525
519- func updateConfigs (
520- ctx context.Context ,
521- dockerClient * client.Client ,
522- dataDir , contID , containerName string ,
523- configs map [string ]string ,
524- ) error {
525- log .Dbg ("Stopping container to update configuration" )
526-
527- tools .StopContainer (ctx , dockerClient , contID , cont .StopTimeout )
526+ func updateConfigs (dataDir string , configs map [string ]string ) error {
527+ if len (configs ) == 0 {
528+ return nil
529+ }
528530
529- // Run basic PostgreSQL configuration.
530531 cfgManager , err := pgconfig .NewCorrector (dataDir )
531532 if err != nil {
532533 return errors .Wrap (err , "failed to create a config manager" )
@@ -536,18 +537,6 @@ func updateConfigs(
536537 return errors .Wrap (err , "failed to append general configuration" )
537538 }
538539
539- if err := dockerClient .ContainerStart (ctx , contID , types.ContainerStartOptions {}); err != nil {
540- diagnostic .CollectContainerDiagnostics (ctx , dockerClient , containerName )
541- return errors .Wrapf (err , "failed to start container %q" , contID )
542- }
543-
544- log .Dbg ("Waiting for container readiness" )
545-
546- if err := tools .CheckContainerReadiness (ctx , dockerClient , contID ); err != nil {
547- diagnostic .CollectContainerDiagnostics (ctx , dockerClient , containerName )
548- return errors .Wrap (err , "failed to readiness check" )
549- }
550-
551540 return nil
552541}
553542
0 commit comments