Skip to content

Commit 757125e

Browse files
committed
Merge branch '373-apply-restore-config' into 'master'
fix: apply the configuration and then start Postgres on the restore stage (#373) Closes #373 See merge request postgres-ai/database-lab!572
2 parents 3e210d1 + 6426e0e commit 757125e

File tree

2 files changed

+36
-49
lines changed

2 files changed

+36
-49
lines changed

engine/internal/retrieval/engine/postgres/logical/dump.go

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,9 @@ func (d *DumpJob) Reload(cfg map[string]interface{}) (err error) {
241241
func (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

engine/internal/retrieval/engine/postgres/logical/restore.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,24 @@ func (r *RestoreJob) Reload(cfg map[string]interface{}) (err error) {
166166
func (r *RestoreJob) Run(ctx context.Context) (err error) {
167167
log.Msg("Run job: ", r.Name())
168168

169-
isEmpty, err := tools.IsEmptyDirectory(r.fsPool.DataDir())
169+
dataDir := r.fsPool.DataDir()
170+
171+
isEmpty, err := tools.IsEmptyDirectory(dataDir)
170172
if err != nil {
171-
return errors.Wrapf(err, "failed to explore the data directory %q", r.fsPool.DataDir())
173+
return fmt.Errorf("failed to explore the data directory %q: %w", dataDir, err)
172174
}
173175

174176
if !isEmpty {
175177
if !r.ForceInit {
176-
return errors.Errorf("the data directory %q is not empty. Use 'forceInit' or empty the data directory",
177-
r.fsPool.DataDir())
178+
return fmt.Errorf("the data directory %q is not empty. Use 'forceInit' or empty the data directory: %w",
179+
dataDir, err)
178180
}
179181

180-
log.Msg(fmt.Sprintf("The data directory %q is not empty. Existing data may be overwritten.", r.fsPool.DataDir()))
182+
log.Msg(fmt.Sprintf("The data directory %q is not empty. Existing data may be overwritten.", dataDir))
183+
184+
if err := updateConfigs(dataDir, r.RestoreOptions.Configs); err != nil {
185+
return fmt.Errorf("failed to update configuration: %w", err)
186+
}
181187
}
182188

183189
if err := tools.PullImage(ctx, r.dockerClient, r.RestoreOptions.DockerImage); err != nil {
@@ -214,8 +220,6 @@ func (r *RestoreJob) Run(ctx context.Context) (err error) {
214220
return errors.Wrapf(err, "failed to start container %q", r.restoreContainerName())
215221
}
216222

217-
dataDir := r.fsPool.DataDir()
218-
219223
log.Msg("Waiting for container readiness")
220224

221225
if err := tools.CheckContainerReadiness(ctx, r.dockerClient, containerID); err != nil {
@@ -224,17 +228,11 @@ func (r *RestoreJob) Run(ctx context.Context) (err error) {
224228
return errors.Wrap(err, "failed to readiness check")
225229
}
226230

227-
if err := setupPGData(ctx, r.dockerClient, dataDir, containerID); err != nil {
231+
if err := setupPGData(ctx, r.dockerClient, dataDir, containerID, r.RestoreOptions.Configs); err != nil {
228232
return errors.Wrap(err, "failed to set up Postgres data")
229233
}
230234
}
231235

232-
if len(r.RestoreOptions.Configs) > 0 {
233-
if err := updateConfigs(ctx, r.dockerClient, dataDir, containerID, r.restoreContainerName(), r.RestoreOptions.Configs); err != nil {
234-
return errors.Wrap(err, "failed to update configs")
235-
}
236-
}
237-
238236
dbList, err := r.getDBList(ctx, containerID)
239237
if err != nil {
240238
return err

0 commit comments

Comments
 (0)