@@ -241,7 +241,9 @@ func (d *DumpJob) Reload(cfg map[string]interface{}) (err error) {
241
241
func (d * DumpJob ) Run (ctx context.Context ) (err error ) {
242
242
log .Msg ("Run job: " , d .Name ())
243
243
244
- isEmpty , err := tools .IsEmptyDirectory (d .fsPool .DataDir ())
244
+ dataDir := d .fsPool .DataDir ()
245
+
246
+ isEmpty , err := tools .IsEmptyDirectory (dataDir )
245
247
if err != nil {
246
248
return errors .Wrap (err , "failed to explore the data directory" )
247
249
}
@@ -252,6 +254,10 @@ func (d *DumpJob) Run(ctx context.Context) (err error) {
252
254
}
253
255
254
256
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
+ }
255
261
}
256
262
257
263
if err := tools .PullImage (ctx , d .dockerClient , d .DockerImage ); err != nil {
@@ -289,12 +295,12 @@ func (d *DumpJob) Run(ctx context.Context) (err error) {
289
295
log .Msg (fmt .Sprintf ("Running container: %s. ID: %v" , d .dumpContainerName (), containerID ))
290
296
291
297
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 )
293
299
return errors .Wrapf (err , "failed to start container %q" , d .dumpContainerName ())
294
300
}
295
301
296
302
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 )
298
304
return errors .Wrap (err , "failed to setup connection options" )
299
305
}
300
306
@@ -304,8 +310,6 @@ func (d *DumpJob) Run(ctx context.Context) (err error) {
304
310
return err
305
311
}
306
312
307
- dataDir := d .fsPool .DataDir ()
308
-
309
313
pgDataDir := tmpDBLabPGDataDir
310
314
if d .DumpOptions .Restore .Enabled {
311
315
pgDataDir = dataDir
@@ -317,16 +321,15 @@ func (d *DumpJob) Run(ctx context.Context) (err error) {
317
321
return errors .Wrap (err , "failed to readiness check" )
318
322
}
319
323
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
323
328
}
324
- }
325
329
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 {
328
331
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 " )
330
333
}
331
334
}
332
335
@@ -484,7 +487,7 @@ func (d *DumpJob) dumpDatabase(ctx context.Context, dumpContID, dbName string, d
484
487
return nil
485
488
}
486
489
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 {
488
491
entryList , err := tools .LsContainerDirectory (ctx , dockerClient , dumpContID , dataDir )
489
492
if err != nil {
490
493
return errors .Wrap (err , "failed to explore the data directory" )
@@ -507,6 +510,10 @@ func setupPGData(ctx context.Context, dockerClient *client.Client, dataDir strin
507
510
508
511
log .Dbg ("Database has been initialized" )
509
512
513
+ if err := updateConfigs (dataDir , configs ); err != nil {
514
+ return fmt .Errorf ("failed to update configs: %w" , err )
515
+ }
516
+
510
517
if err := tools .StartPostgres (ctx , dockerClient , dumpContID , tools .DefaultStopTimeout ); err != nil {
511
518
return errors .Wrap (err , "failed to init Postgres" )
512
519
}
@@ -516,17 +523,11 @@ func setupPGData(ctx context.Context, dockerClient *client.Client, dataDir strin
516
523
return nil
517
524
}
518
525
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
+ }
528
530
529
- // Run basic PostgreSQL configuration.
530
531
cfgManager , err := pgconfig .NewCorrector (dataDir )
531
532
if err != nil {
532
533
return errors .Wrap (err , "failed to create a config manager" )
@@ -536,18 +537,6 @@ func updateConfigs(
536
537
return errors .Wrap (err , "failed to append general configuration" )
537
538
}
538
539
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
-
551
540
return nil
552
541
}
553
542
0 commit comments