Skip to content

Commit 20c970d

Browse files
committed
fix(engine): use the correct path to the data directory for the promote instance (#342)
1 parent 21759a2 commit 20c970d

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

engine/.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ linters:
8282
- megacheck
8383
- misspell
8484
- prealloc
85-
# - revive # temporarily disabled: https://gitlab.com/postgres-ai/database-lab/-/merge_requests/498
85+
- revive
8686
- structcheck
8787
- stylecheck
8888
- unconvert

engine/internal/retrieval/engine/postgres/physical/pgbackrest.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,36 @@ const (
1616

1717
// pgbackrest defines a pgBackRest as an archival restoration tool.
1818
type pgbackrest struct {
19-
pgDataDir string
20-
options pgbackrestOptions
19+
options pgbackrestOptions
2120
}
2221

2322
type pgbackrestOptions struct {
2423
Stanza string `yaml:"stanza"`
2524
ForceInit bool `yaml:"forceInit"`
2625
}
2726

28-
func newPgBackRest(pgDataDir string, options pgbackrestOptions) *pgbackrest {
27+
func newPgBackRest(options pgbackrestOptions) *pgbackrest {
2928
return &pgbackrest{
30-
pgDataDir: pgDataDir,
31-
options: options,
29+
options: options,
3230
}
3331
}
3432

3533
// GetRestoreCommand returns a command to restore data.
3634
func (p *pgbackrest) GetRestoreCommand() string {
35+
restoreCmd := fmt.Sprintf("sudo -Eu postgres pgbackrest --type=standby --pg1-path=${PGDATA} --stanza=%[1]s restore "+
36+
"--recovery-option=restore_command='pgbackrest --pg1-path=${PGDATA} --stanza=%[1]s archive-get %%f %%p'", p.options.Stanza)
37+
3738
if p.options.ForceInit {
38-
return fmt.Sprintf("sudo -Eu postgres pgbackrest --delta --type=standby --pg1-path=%s --stanza=%s restore", p.pgDataDir, p.options.Stanza)
39+
restoreCmd += " --delta"
3940
}
4041

41-
return fmt.Sprintf("sudo -Eu postgres pgbackrest --type=standby --pg1-path=%s --stanza=%s restore", p.pgDataDir, p.options.Stanza)
42+
return restoreCmd
4243
}
4344

4445
// GetRecoveryConfig returns a recovery config to restore data.
4546
func (p *pgbackrest) GetRecoveryConfig(pgVersion float64) map[string]string {
4647
recoveryCfg := map[string]string{
47-
"restore_command": fmt.Sprintf("pgbackrest --pg1-path=%s --stanza=%s archive-get %%f %%p", p.pgDataDir, p.options.Stanza),
48+
"restore_command": fmt.Sprintf("pgbackrest --pg1-path=${PGDATA} --stanza=%s archive-get %%f %%p", p.options.Stanza),
4849
}
4950

5051
if pgVersion < defaults.PGVersion12 {

engine/internal/retrieval/engine/postgres/physical/pgbackrest_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,33 @@ import (
77
)
88

99
func TestPgBackRestRecoveryConfig(t *testing.T) {
10-
pgbackrest := newPgBackRest("dataDir", pgbackrestOptions{Stanza: "stanzaName"})
10+
pgbackrest := newPgBackRest(pgbackrestOptions{Stanza: "stanzaName"})
1111

1212
recoveryConfig := pgbackrest.GetRecoveryConfig(11.7)
1313
expectedResponse11 := map[string]string{
14-
"restore_command": "pgbackrest --pg1-path=dataDir --stanza=stanzaName archive-get %f %p",
14+
"restore_command": "pgbackrest --pg1-path=${PGDATA} --stanza=stanzaName archive-get %f %p",
1515
"recovery_target_timeline": "latest",
1616
}
1717
assert.Equal(t, expectedResponse11, recoveryConfig)
1818

1919
recoveryConfig = pgbackrest.GetRecoveryConfig(12.3)
2020
expectedResponse12 := map[string]string{
21-
"restore_command": "pgbackrest --pg1-path=dataDir --stanza=stanzaName archive-get %f %p",
21+
"restore_command": "pgbackrest --pg1-path=${PGDATA} --stanza=stanzaName archive-get %f %p",
2222
}
2323
assert.Equal(t, expectedResponse12, recoveryConfig)
2424
}
2525

2626
func TestPgBackRestRestoreCommand(t *testing.T) {
27-
pgbackrest := newPgBackRest("dataDir", pgbackrestOptions{Stanza: "stanzaName"})
27+
pgbackrest := newPgBackRest(pgbackrestOptions{Stanza: "stanzaName"})
2828

2929
restoreCmd := pgbackrest.GetRestoreCommand()
30-
expectedResponse := "sudo -Eu postgres pgbackrest --type=standby --pg1-path=dataDir --stanza=stanzaName restore"
30+
expectedResponse := "sudo -Eu postgres pgbackrest --type=standby --pg1-path=${PGDATA} --stanza=stanzaName restore " +
31+
"--recovery-option=restore_command='pgbackrest --pg1-path=${PGDATA} --stanza=stanzaName archive-get %f %p'"
3132
assert.Equal(t, expectedResponse, restoreCmd)
3233

3334
pgbackrest.options.ForceInit = true
3435
restoreCmd = pgbackrest.GetRestoreCommand()
35-
expectedResponse = "sudo -Eu postgres pgbackrest --delta --type=standby --pg1-path=dataDir --stanza=stanzaName restore"
36+
expectedResponse = "sudo -Eu postgres pgbackrest --type=standby --pg1-path=${PGDATA} --stanza=stanzaName restore " +
37+
"--recovery-option=restore_command='pgbackrest --pg1-path=${PGDATA} --stanza=stanzaName archive-get %f %p' --delta"
3638
assert.Equal(t, expectedResponse, restoreCmd)
3739
}

engine/internal/retrieval/engine/postgres/physical/physical.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (r *RestoreJob) getRestorer(tool string) (restorer, error) {
132132
return newWALG(r.fsPool.DataDir(), r.WALG), nil
133133

134134
case pgbackrestTool:
135-
return newPgBackRest(r.fsPool.DataDir(), r.PgBackRest), nil
135+
return newPgBackRest(r.PgBackRest), nil
136136

137137
case customTool:
138138
return newCustomTool(r.CustomTool), nil

0 commit comments

Comments
 (0)