-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtablespace_test.go
236 lines (203 loc) · 8.67 KB
/
tablespace_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package end_to_end_test
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/cloudberrydb/cbcopy/internal/dbconn"
"github.com/cloudberrydb/cbcopy/internal/testhelper"
"github.com/cloudberrydb/cbcopy/testutils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func checkTableInTablespace(conn *dbconn.DBConn, tableName string, tablespaceName string) bool {
var schema, table string
s := strings.Split(tableName, ".")
if len(s) == 2 {
schema, table = s[0], s[1]
} else if len(s) == 1 {
schema = "public"
table = s[0]
} else {
Fail(fmt.Sprintf("Table %s is not in a valid format", tableName))
}
query := fmt.Sprintf(`
SELECT EXISTS (
SELECT 1
FROM pg_class c
JOIN pg_namespace n ON c.relnamespace = n.oid
JOIN pg_tablespace t ON c.reltablespace = t.oid
WHERE n.nspname = '%s'
AND c.relname = '%s'
AND t.spcname = '%s'
) AS exists`, schema, table, tablespaceName)
exists := dbconn.MustSelectString(conn, query)
return exists == "true"
}
func assertTableInTablespace(conn *dbconn.DBConn, tableName string, tablespaceName string) {
if !checkTableInTablespace(conn, tableName, tablespaceName) {
Fail(fmt.Sprintf("Table %s is not in tablespace %s when it should be",
tableName, tablespaceName))
}
}
func assertTablesInTablespace(conn *dbconn.DBConn, tables []string, tablespaceName string) {
for _, tableName := range tables {
assertTableInTablespace(conn, tableName, tablespaceName)
}
}
var _ = Describe("Tablespace migration tests", func() {
BeforeEach(func() {
end_to_end_setup()
})
AfterEach(func() {
end_to_end_teardown()
})
It("migrates tables with same tablespace name in source and target", func() {
// Connect to source and target databases
srcTestConn := testutils.SetupTestDbConn("source_db")
destTestConn := testutils.SetupTestDbConn("target_db")
// Cleanup function to drop schemas and databases
defer func() {
// Drop source schema in source database
testhelper.AssertQueryRuns(srcTestConn, "DROP SCHEMA IF EXISTS source_schema CASCADE")
// Drop target schema in target database
testhelper.AssertQueryRuns(destTestConn, "DROP SCHEMA IF EXISTS target_schema CASCADE")
// Close database connections
srcTestConn.Close()
destTestConn.Close()
}()
// Create source schema and test tables in source database
testhelper.AssertQueryRuns(srcTestConn, "CREATE SCHEMA source_schema")
testhelper.AssertQueryRuns(srcTestConn, "CREATE TABLE source_schema.test_table1 (i int) TABLESPACE e2e_test_same_tablespace")
testhelper.AssertQueryRuns(srcTestConn, "CREATE TABLE source_schema.test_table2 (i int) TABLESPACE e2e_test_same_tablespace")
testhelper.AssertQueryRuns(srcTestConn, "INSERT INTO source_schema.test_table1 SELECT generate_series(1,100)")
testhelper.AssertQueryRuns(srcTestConn, "INSERT INTO source_schema.test_table2 SELECT generate_series(1,200)")
// Execute schema migration from source_schema to target_schema
cbcopy(cbcopyPath,
"--source-host", sourceConn.Host,
"--source-port", strconv.Itoa(sourceConn.Port),
"--source-user", sourceConn.User,
"--dest-host", destConn.Host,
"--dest-port", strconv.Itoa(destConn.Port),
"--dest-user", destConn.User,
"--schema", fmt.Sprintf("%s.source_schema", "source_db"),
"--dest-schema", fmt.Sprintf("%s.target_schema", "target_db"),
"--truncate")
assertTablesRestored(destTestConn, []string{
"target_schema.test_table1",
"target_schema.test_table2",
})
assertDataRestored(destTestConn, map[string]int{
"target_schema.test_table1": 100,
"target_schema.test_table2": 200,
})
assertTablesInTablespace(destTestConn, []string{
"target_schema.test_table1",
"target_schema.test_table2",
}, "e2e_test_same_tablespace")
})
It("migrates tables with different tablespace names", func() {
// Connect to source and target databases
srcTestConn := testutils.SetupTestDbConn("source_db")
destTestConn := testutils.SetupTestDbConn("target_db")
// Cleanup function to drop schemas and databases
defer func() {
// Drop source schema in source database
testhelper.AssertQueryRuns(srcTestConn, "DROP SCHEMA IF EXISTS source_schema CASCADE")
// Drop target schema in target database
testhelper.AssertQueryRuns(destTestConn, "DROP SCHEMA IF EXISTS target_schema CASCADE")
// Close database connections
srcTestConn.Close()
destTestConn.Close()
}()
// Create source schema and test tables in source database with source tablespace
testhelper.AssertQueryRuns(srcTestConn, "CREATE SCHEMA source_schema")
testhelper.AssertQueryRuns(srcTestConn, "CREATE TABLE source_schema.test_table1 (i int) TABLESPACE e2e_test_source_tablespace")
testhelper.AssertQueryRuns(srcTestConn, "CREATE TABLE source_schema.test_table2 (i int) TABLESPACE e2e_test_source_tablespace")
testhelper.AssertQueryRuns(srcTestConn, "INSERT INTO source_schema.test_table1 SELECT generate_series(1,100)")
testhelper.AssertQueryRuns(srcTestConn, "INSERT INTO source_schema.test_table2 SELECT generate_series(1,200)")
// Create tablespace mapping file
mappingFile := "/tmp/tablespace_mapping.txt"
content := "e2e_test_source_tablespace,e2e_test_dest_tablespace"
err := os.WriteFile(mappingFile, []byte(content), 0644)
Expect(err).NotTo(HaveOccurred())
defer os.Remove(mappingFile)
// Execute schema migration from source_schema to target_schema
cbcopy(cbcopyPath,
"--source-host", sourceConn.Host,
"--source-port", strconv.Itoa(sourceConn.Port),
"--source-user", sourceConn.User,
"--dest-host", destConn.Host,
"--dest-port", strconv.Itoa(destConn.Port),
"--dest-user", destConn.User,
"--schema", fmt.Sprintf("%s.source_schema", "source_db"),
"--dest-schema", fmt.Sprintf("%s.target_schema", "target_db"),
"--tablespace-mapping-file", mappingFile,
"--truncate")
// Verify tables are restored
assertTablesRestored(destTestConn, []string{
"target_schema.test_table1",
"target_schema.test_table2",
})
// Verify data is restored correctly
assertDataRestored(destTestConn, map[string]int{
"target_schema.test_table1": 100,
"target_schema.test_table2": 200,
})
// Verify tables are in the destination tablespace
assertTablesInTablespace(destTestConn, []string{
"target_schema.test_table1",
"target_schema.test_table2",
}, "e2e_test_dest_tablespace")
})
It("migrates tables to specified destination tablespace using --dest-tablespace", func() {
// Connect to source and target databases
srcTestConn := testutils.SetupTestDbConn("source_db")
destTestConn := testutils.SetupTestDbConn("target_db")
// Cleanup function to drop schemas and databases
defer func() {
// Drop source schema in source database
testhelper.AssertQueryRuns(srcTestConn, "DROP SCHEMA IF EXISTS source_schema CASCADE")
// Drop target schema in target database
testhelper.AssertQueryRuns(destTestConn, "DROP SCHEMA IF EXISTS target_schema CASCADE")
// Close database connections
srcTestConn.Close()
destTestConn.Close()
}()
// Create source schema and test tables in source database with source tablespace
testhelper.AssertQueryRuns(srcTestConn, "CREATE SCHEMA source_schema")
// Create tables in different source tablespaces
testhelper.AssertQueryRuns(srcTestConn, "CREATE TABLE source_schema.test_table1 (i int) TABLESPACE e2e_test_source_tablespace")
testhelper.AssertQueryRuns(srcTestConn, "CREATE TABLE source_schema.test_table2 (i int) TABLESPACE e2e_test_same_tablespace")
testhelper.AssertQueryRuns(srcTestConn, "INSERT INTO source_schema.test_table1 SELECT generate_series(1,100)")
testhelper.AssertQueryRuns(srcTestConn, "INSERT INTO source_schema.test_table2 SELECT generate_series(1,200)")
// Execute schema migration from source_schema to target_schema
// Use --dest-tablespace to move all tables to the destination tablespace
cbcopy(cbcopyPath,
"--source-host", sourceConn.Host,
"--source-port", strconv.Itoa(sourceConn.Port),
"--source-user", sourceConn.User,
"--dest-host", destConn.Host,
"--dest-port", strconv.Itoa(destConn.Port),
"--dest-user", destConn.User,
"--schema", fmt.Sprintf("%s.source_schema", "source_db"),
"--dest-schema", fmt.Sprintf("%s.target_schema", "target_db"),
"--dest-tablespace", "e2e_test_dest_tablespace",
"--truncate")
// Verify tables are restored
assertTablesRestored(destTestConn, []string{
"target_schema.test_table1",
"target_schema.test_table2",
})
// Verify data is restored correctly
assertDataRestored(destTestConn, map[string]int{
"target_schema.test_table1": 100,
"target_schema.test_table2": 200,
})
// Verify all tables are in the destination tablespace
assertTablesInTablespace(destTestConn, []string{
"target_schema.test_table1",
"target_schema.test_table2",
}, "e2e_test_dest_tablespace")
})
})