Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f6a3c4c

Browse filesBrowse files
committed
fix: set up recovery configuration depends on the database version (#206)
Signed-off-by: akartasov <agneum@gmail.com>
1 parent d53718f commit f6a3c4c
Copy full SHA for f6a3c4c

File tree

6 files changed

+73
-12
lines changed
Filter options

6 files changed

+73
-12
lines changed

‎pkg/retrieval/engine/postgres/physical/custom.go

Copy file name to clipboardExpand all lines: pkg/retrieval/engine/postgres/physical/custom.go
+7-2Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package physical
77
import (
88
"bytes"
99
"fmt"
10+
11+
"gitlab.com/postgres-ai/database-lab/pkg/retrieval/engine/postgres/tools/defaults"
1012
)
1113

1214
const (
@@ -34,13 +36,16 @@ func (c *custom) GetRestoreCommand() string {
3436
}
3537

3638
// GetRecoveryConfig returns a recovery config to restore data.
37-
func (c *custom) GetRecoveryConfig() []byte {
39+
func (c *custom) GetRecoveryConfig(pgVersion float64) []byte {
3840
buffer := bytes.Buffer{}
3941

4042
if c.options.RestoreCommand != "" {
4143
buffer.WriteString("\n")
42-
buffer.WriteString("standby_mode = 'on'\n")
4344
buffer.WriteString(fmt.Sprintf("restore_command = '%s'\n", c.options.RestoreCommand))
45+
46+
if pgVersion < defaults.PGVersion12 {
47+
buffer.WriteString("standby_mode = 'on'\n")
48+
}
4449
}
4550

4651
return buffer.Bytes()
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package physical
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestCustomRecoveryConfig(t *testing.T) {
10+
customTool := newCustomTool(customOptions{
11+
RestoreCommand: "pg_basebackup -X stream -D dataDirectory",
12+
})
13+
14+
recoveryConfig := customTool.GetRecoveryConfig(11.7)
15+
expectedResponse11 := `
16+
restore_command = 'pg_basebackup -X stream -D dataDirectory'
17+
standby_mode = 'on'
18+
`
19+
assert.Equal(t, []byte(expectedResponse11), recoveryConfig)
20+
21+
recoveryConfig = customTool.GetRecoveryConfig(12.3)
22+
expectedResponse12 := `
23+
restore_command = 'pg_basebackup -X stream -D dataDirectory'
24+
`
25+
assert.Equal(t, []byte(expectedResponse12), recoveryConfig)
26+
}

‎pkg/retrieval/engine/postgres/physical/physical.go

Copy file name to clipboardExpand all lines: pkg/retrieval/engine/postgres/physical/physical.go
+6-8Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ type restorer interface {
9494
GetRestoreCommand() string
9595

9696
// GetRecoveryConfig returns a recovery config to restore data.
97-
GetRecoveryConfig() []byte
97+
GetRecoveryConfig(version float64) []byte
9898
}
9999

100100
// NewJob creates a new physical restore job.
@@ -419,18 +419,16 @@ func (r *RestoreJob) adjustRecoveryConfiguration(pgVersion, pgDataDir string) er
419419
// Replication mode.
420420
var recoveryFilename string
421421

422-
if len(r.restorer.GetRecoveryConfig()) == 0 {
423-
return nil
424-
}
425-
426422
version, err := strconv.ParseFloat(pgVersion, 64)
427423
if err != nil {
428424
return errors.Wrap(err, "failed to parse PostgreSQL version")
429425
}
430426

431-
const pgVersion12 = 12
427+
if len(r.restorer.GetRecoveryConfig(version)) == 0 {
428+
return nil
429+
}
432430

433-
if version >= pgVersion12 {
431+
if version >= defaults.PGVersion12 {
434432
if err := tools.TouchFile(path.Join(pgDataDir, "standby.signal")); err != nil {
435433
return err
436434
}
@@ -440,7 +438,7 @@ func (r *RestoreJob) adjustRecoveryConfiguration(pgVersion, pgDataDir string) er
440438
recoveryFilename = "recovery.conf"
441439
}
442440

443-
return fs.AppendFile(path.Join(pgDataDir, recoveryFilename), r.restorer.GetRecoveryConfig())
441+
return fs.AppendFile(path.Join(pgDataDir, recoveryFilename), r.restorer.GetRecoveryConfig(version))
444442
}
445443

446444
func (r *RestoreJob) applyInitParams(ctx context.Context, contID, pgVersion, dataDir string) error {

‎pkg/retrieval/engine/postgres/physical/wal_g.go

Copy file name to clipboardExpand all lines: pkg/retrieval/engine/postgres/physical/wal_g.go
+7-2Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package physical
77
import (
88
"bytes"
99
"fmt"
10+
11+
"gitlab.com/postgres-ai/database-lab/pkg/retrieval/engine/postgres/tools/defaults"
1012
)
1113

1214
const (
@@ -36,12 +38,15 @@ func (w *walg) GetRestoreCommand() string {
3638
}
3739

3840
// GetRecoveryConfig returns a recovery config to restore data.
39-
func (w *walg) GetRecoveryConfig() []byte {
41+
func (w *walg) GetRecoveryConfig(pgVersion float64) []byte {
4042
buffer := bytes.Buffer{}
4143

4244
buffer.WriteString("\n")
43-
buffer.WriteString("standby_mode = 'on'\n")
4445
buffer.WriteString("restore_command = 'wal-g wal-fetch %f %p'\n")
4546

47+
if pgVersion < defaults.PGVersion12 {
48+
buffer.WriteString("standby_mode = 'on'\n")
49+
}
50+
4651
return buffer.Bytes()
4752
}
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package physical
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestWALGRecoveryConfig(t *testing.T) {
10+
walg := newWALG("dataDir", walgOptions{})
11+
12+
recoveryConfig := walg.GetRecoveryConfig(11.7)
13+
expectedResponse11 := `
14+
restore_command = 'wal-g wal-fetch %f %p'
15+
standby_mode = 'on'
16+
`
17+
assert.Equal(t, []byte(expectedResponse11), recoveryConfig)
18+
19+
recoveryConfig = walg.GetRecoveryConfig(12.3)
20+
expectedResponse12 := `
21+
restore_command = 'wal-g wal-fetch %f %p'
22+
`
23+
assert.Equal(t, []byte(expectedResponse12), recoveryConfig)
24+
}

‎pkg/retrieval/engine/postgres/tools/defaults/defaults.go

Copy file name to clipboardExpand all lines: pkg/retrieval/engine/postgres/tools/defaults/defaults.go
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ const (
1414

1515
// DBName defines a default database name.
1616
DBName = "postgres"
17+
18+
// PGVersion12 defines the PostgreSQL 12 version.
19+
PGVersion12 = 12
1720
)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.