Skip to content

Commit 037a694

Browse files
committed
Merge branch '277-restore-from-dump' into 'master'
fix: restore command depends on type of the dumpLocation (#277) Closes #277 See merge request postgres-ai/database-lab!324
2 parents 49ef0f1 + d3578b1 commit 037a694

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ func (r *RestoreJob) buildPGRestoreCommand(dumpName string, definition DumpDefin
724724

725725
func (r *RestoreJob) getDumpLocation(dumpFormat, dbName string) string {
726726
switch dumpFormat {
727-
case customFormat:
727+
case customFormat, plainFormat:
728728
if r.isDumpLocationDir {
729729
return path.Join(r.RestoreOptions.DumpLocation, dbName)
730730
}

pkg/retrieval/engine/postgres/logical/restore_test.go

+45-26
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ func TestRestoreCommandBuilding(t *testing.T) {
2828
}
2929

3030
testCases := []struct {
31-
CopyOptions RestoreOptions
32-
Command []string
31+
copyOptions RestoreOptions
32+
command []string
33+
isDumpLocationDir bool
3334
}{
3435
{
35-
CopyOptions: RestoreOptions{
36+
copyOptions: RestoreOptions{
3637
ParallelJobs: 1,
3738
ForceInit: false,
3839
Databases: map[string]DumpDefinition{
@@ -42,26 +43,26 @@ func TestRestoreCommandBuilding(t *testing.T) {
4243
},
4344
DumpLocation: "/tmp/db.dump",
4445
},
45-
Command: []string{"pg_restore", "--username", "john", "--dbname", "postgres", "--no-privileges", "--no-owner", "--create", "--jobs", "1", "/tmp/db.dump"},
46+
command: []string{"pg_restore", "--username", "john", "--dbname", "postgres", "--no-privileges", "--no-owner", "--create", "--jobs", "1", "/tmp/db.dump"},
4647
},
4748
{
48-
CopyOptions: RestoreOptions{
49+
copyOptions: RestoreOptions{
4950
ParallelJobs: 4,
5051
ForceInit: true,
5152
},
52-
Command: []string{"pg_restore", "--username", "john", "--dbname", "postgres", "--no-privileges", "--no-owner", "--create", "--clean", "--if-exists", "--jobs", "4", ""},
53+
command: []string{"pg_restore", "--username", "john", "--dbname", "postgres", "--no-privileges", "--no-owner", "--create", "--clean", "--if-exists", "--jobs", "4", ""},
5354
},
5455
{
55-
CopyOptions: RestoreOptions{
56+
copyOptions: RestoreOptions{
5657
ParallelJobs: 2,
5758
ForceInit: false,
5859
Databases: map[string]DumpDefinition{"testDB": {}},
5960
DumpLocation: "/tmp/db.dump",
6061
},
61-
Command: []string{"pg_restore", "--username", "john", "--dbname", "postgres", "--no-privileges", "--no-owner", "--create", "--jobs", "2", "/tmp/db.dump/testDB"},
62+
command: []string{"pg_restore", "--username", "john", "--dbname", "postgres", "--no-privileges", "--no-owner", "--create", "--jobs", "2", "/tmp/db.dump/testDB"},
6263
},
6364
{
64-
CopyOptions: RestoreOptions{
65+
copyOptions: RestoreOptions{
6566
ParallelJobs: 1,
6667
Databases: map[string]DumpDefinition{
6768
"testDB": {
@@ -71,10 +72,10 @@ func TestRestoreCommandBuilding(t *testing.T) {
7172
},
7273
DumpLocation: "/tmp/db.dump",
7374
},
74-
Command: []string{"pg_restore", "--username", "john", "--dbname", "postgres", "--no-privileges", "--no-owner", "--create", "--jobs", "1", "--table", "test", "--table", "users", "/tmp/db.dump/testDB"},
75+
command: []string{"pg_restore", "--username", "john", "--dbname", "postgres", "--no-privileges", "--no-owner", "--create", "--jobs", "1", "--table", "test", "--table", "users", "/tmp/db.dump/testDB"},
7576
},
7677
{
77-
CopyOptions: RestoreOptions{
78+
copyOptions: RestoreOptions{
7879
Databases: map[string]DumpDefinition{
7980
"testDB.dump": {
8081
Format: plainFormat,
@@ -83,26 +84,41 @@ func TestRestoreCommandBuilding(t *testing.T) {
8384
},
8485
DumpLocation: "/tmp/db.dump",
8586
},
86-
Command: []string{"sh", "-c", "cat /tmp/db.dump/testDB.dump | psql --username john --dbname postgres"},
87+
isDumpLocationDir: true,
88+
command: []string{"sh", "-c", "cat /tmp/db.dump/testDB.dump | psql --username john --dbname postgres"},
8789
},
8890
{
89-
CopyOptions: RestoreOptions{
91+
copyOptions: RestoreOptions{
9092
Databases: map[string]DumpDefinition{
9193
"testDB.dump": {
9294
Format: plainFormat,
9395
},
9496
},
9597
DumpLocation: "/tmp/db.dump",
9698
},
97-
Command: []string{"sh", "-c", "cat /tmp/db.dump/testDB.dump | psql --username john --dbname testDB"},
99+
isDumpLocationDir: true,
100+
command: []string{"sh", "-c", "cat /tmp/db.dump/testDB.dump | psql --username john --dbname testDB"},
101+
},
102+
{
103+
copyOptions: RestoreOptions{
104+
Databases: map[string]DumpDefinition{
105+
"testDB.dump": {
106+
Format: plainFormat,
107+
},
108+
},
109+
DumpLocation: "/tmp/db.dump",
110+
},
111+
isDumpLocationDir: false,
112+
command: []string{"sh", "-c", "cat /tmp/db.dump | psql --username john --dbname testDB"},
98113
},
99114
}
100115

101116
for _, tc := range testCases {
102-
logicalJob.RestoreOptions = tc.CopyOptions
103-
for dbName, definition := range tc.CopyOptions.Databases {
117+
logicalJob.RestoreOptions = tc.copyOptions
118+
logicalJob.isDumpLocationDir = tc.isDumpLocationDir
119+
for dbName, definition := range tc.copyOptions.Databases {
104120
restoreCommand := logicalJob.buildLogicalRestoreCommand(dbName, definition)
105-
assert.Equal(t, restoreCommand, tc.Command)
121+
assert.Equal(t, restoreCommand, tc.command)
106122
}
107123
}
108124
}
@@ -163,20 +179,23 @@ func TestDiscoverDumpDirectories(t *testing.T) {
163179
}
164180

165181
func TestDumpLocation(t *testing.T) {
166-
r := &RestoreJob{}
167-
r.RestoreOptions.DumpLocation = "/tmp/dblab_test"
168-
169182
testCases := []struct {
170-
format string
171-
dbname string
172-
expectedLocation string
183+
format string
184+
isDumpLocationDir bool
185+
dbname string
186+
expectedLocation string
173187
}{
174-
{format: directoryFormat, dbname: "postgres", expectedLocation: "/tmp/dblab_test/postgres"},
175-
{format: customFormat, dbname: "postgres", expectedLocation: "/tmp/dblab_test"},
176-
{format: plainFormat, dbname: "postgres", expectedLocation: "/tmp/dblab_test/postgres"},
188+
{format: directoryFormat, dbname: "postgresDir", expectedLocation: "/tmp/dblab_test/postgresDir"},
189+
{format: customFormat, dbname: "postgresCustom", isDumpLocationDir: true, expectedLocation: "/tmp/dblab_test/postgresCustom"},
190+
{format: customFormat, dbname: "postgresCustom", expectedLocation: "/tmp/dblab_test"},
191+
{format: plainFormat, dbname: "postgresPlain", isDumpLocationDir: true, expectedLocation: "/tmp/dblab_test/postgresPlain"},
192+
{format: plainFormat, dbname: "postgresPlain", expectedLocation: "/tmp/dblab_test"},
177193
}
178194

179195
for _, tc := range testCases {
196+
r := &RestoreJob{}
197+
r.RestoreOptions.DumpLocation = "/tmp/dblab_test"
198+
r.isDumpLocationDir = tc.isDumpLocationDir
180199
dumpLocation := r.getDumpLocation(tc.format, tc.dbname)
181200
assert.Equal(t, tc.expectedLocation, dumpLocation)
182201
}

0 commit comments

Comments
 (0)