forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 2
/
dump.go
506 lines (469 loc) · 13.1 KB
/
dump.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package cli
import (
"bytes"
"database/sql/driver"
"fmt"
"io"
"os"
"sort"
"strings"
"time"
"golang.org/x/net/context"
"golang.org/x/sync/errgroup"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// dumpCmd dumps SQL tables.
var dumpCmd = &cobra.Command{
Use: "dump [options] <database> [<table> [<table>...]]",
Short: "dump sql tables\n",
Long: `
Dump SQL tables of a cockroach database. If the table name
is omitted, dump all tables in the database.
`,
RunE: MaybeDecorateGRPCError(runDump),
}
func runDump(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return usageAndError(cmd)
}
conn, err := getPasswordAndMakeSQLClient()
if err != nil {
return err
}
defer conn.Close()
dbName := args[0]
var tableNames []string
if len(args) > 1 {
tableNames = args[1:]
}
mds, ts, err := getDumpMetadata(conn, dbName, tableNames, dumpCtx.asOf)
if err != nil {
return err
}
byID := make(map[int64]tableMetadata)
for _, md := range mds {
byID[md.ID] = md
}
// First sort by name to guarantee stable output.
sort.Slice(mds, func(i, j int) bool {
return mds[i].name.String() < mds[j].name.String()
})
// Collect transitive dependencies in topological order into collected.
var collected []int64
seen := make(map[int64]bool)
for _, md := range mds {
collect(md.ID, byID, seen, &collected)
}
// collectOrder maps a table ID to its collection index. This is needed
// instead of just using range over collected because collected may contain
// table IDs not present in the dump spec. It is simpler to sort mds correctly
// to skip over these referenced-but-not-dumped tables.
collectOrder := make(map[int64]int)
for i, id := range collected {
collectOrder[id] = i
}
// Second sort dumped tables by dependency order.
sort.SliceStable(mds, func(i, j int) bool {
return collectOrder[mds[i].ID] < collectOrder[mds[j].ID]
})
w := os.Stdout
if dumpCtx.dumpMode != dumpDataOnly {
for i, md := range mds {
if i > 0 {
fmt.Fprintln(w)
}
if err := dumpCreateTable(w, md); err != nil {
return err
}
}
}
if dumpCtx.dumpMode != dumpSchemaOnly {
for _, md := range mds {
if md.isView {
continue
}
if err := dumpTableData(w, conn, ts, md); err != nil {
return err
}
}
}
return nil
}
func collect(tid int64, byID map[int64]tableMetadata, seen map[int64]bool, collected *[]int64) {
// has this table already been collected previously?
if seen[tid] {
return
}
// no: mark it as seen.
seen[tid] = true
for _, dep := range byID[tid].dependsOn {
// depth-first collection of dependencies
collect(dep, byID, seen, collected)
}
// Only add it after its dependencies.
*collected = append(*collected, tid)
}
// tableMetadata describes one table to dump.
type tableMetadata struct {
ID int64
name *parser.TableName
columnNames string
columnTypes map[string]string
createStmt string
dependsOn []int64
isView bool
}
// getDumpMetadata retrieves the table information for the specified table(s).
// It also retrieves the cluster timestamp at which the metadata was
// retrieved.
func getDumpMetadata(
conn *sqlConn, dbName string, tableNames []string, asOf string,
) (mds []tableMetadata, clusterTS string, err error) {
if asOf == "" {
vals, err := conn.QueryRow("SELECT cluster_logical_timestamp()", nil)
if err != nil {
return nil, "", err
}
clusterTS = string(vals[0].([]byte))
} else {
// Validate the timestamp. This prevents SQL injection.
if _, err := parser.ParseDTimestamp(asOf, time.Nanosecond); err != nil {
return nil, "", err
}
clusterTS = asOf
}
if tableNames == nil {
tableNames, err = getTableNames(conn, dbName, clusterTS)
if err != nil {
return nil, "", err
}
}
mds = make([]tableMetadata, len(tableNames))
for i, tableName := range tableNames {
md, err := getMetadataForTable(conn, dbName, tableName, clusterTS)
if err != nil {
return nil, "", err
}
mds[i] = md
}
return mds, clusterTS, nil
}
// getTableNames retrieves all tables names in the given database.
func getTableNames(conn *sqlConn, dbName string, ts string) (tableNames []string, err error) {
rows, err := conn.Query(fmt.Sprintf(`
SELECT TABLE_NAME
FROM "".information_schema.tables
AS OF SYSTEM TIME '%s'
WHERE TABLE_SCHEMA = $1
`, ts), []driver.Value{dbName})
if err != nil {
return nil, err
}
vals := make([]driver.Value, 1)
for {
if err := rows.Next(vals); err == io.EOF {
break
} else if err != nil {
return nil, err
}
nameI := vals[0]
name, ok := nameI.(string)
if !ok {
return nil, fmt.Errorf("unexpected value: %T", nameI)
}
tableNames = append(tableNames, name)
}
if err := rows.Close(); err != nil {
return nil, err
}
return tableNames, nil
}
func getMetadataForTable(
conn *sqlConn, dbName, tableName string, ts string,
) (tableMetadata, error) {
name := &parser.TableName{DatabaseName: parser.Name(dbName), TableName: parser.Name(tableName)}
// Fetch table ID.
vals, err := conn.QueryRow(fmt.Sprintf(`
SELECT table_id
FROM %s.crdb_internal.tables
AS OF SYSTEM TIME '%s'
WHERE DATABASE_NAME = $1
AND NAME = $2
`, parser.Name(dbName).String(), ts), []driver.Value{dbName, tableName})
if err != nil {
if err == io.EOF {
return tableMetadata{}, errors.Errorf("relation %s does not exist", name)
}
return tableMetadata{}, err
}
tableID := vals[0].(int64)
// Fetch column types.
rows, err := conn.Query(fmt.Sprintf(`
SELECT COLUMN_NAME, DATA_TYPE
FROM "".information_schema.columns
AS OF SYSTEM TIME '%s'
WHERE TABLE_SCHEMA = $1
AND TABLE_NAME = $2
`, ts), []driver.Value{dbName, tableName})
if err != nil {
return tableMetadata{}, err
}
vals = make([]driver.Value, 2)
coltypes := make(map[string]string)
var colnames bytes.Buffer
for {
if err := rows.Next(vals); err == io.EOF {
break
} else if err != nil {
return tableMetadata{}, err
}
nameI, typI := vals[0], vals[1]
name, ok := nameI.(string)
if !ok {
return tableMetadata{}, fmt.Errorf("unexpected value: %T", nameI)
}
typ, ok := typI.(string)
if !ok {
return tableMetadata{}, fmt.Errorf("unexpected value: %T", typI)
}
coltypes[name] = typ
if colnames.Len() > 0 {
colnames.WriteString(", ")
}
parser.FormatNode(&colnames, parser.FmtSimple, parser.Name(name))
}
if err := rows.Close(); err != nil {
return tableMetadata{}, err
}
vals, err = conn.QueryRow(fmt.Sprintf(`
SELECT create_statement, descriptor_type = 'view'
FROM %s.crdb_internal.create_statements
AS OF SYSTEM TIME '%s'
WHERE descriptor_name = $1
AND database_name = $2
`, parser.Name(dbName).String(), ts), []driver.Value{tableName, dbName})
if err != nil {
return tableMetadata{}, err
}
create := vals[0].(string)
descType := vals[1].(bool)
rows, err = conn.Query(fmt.Sprintf(`
SELECT dependson_id
FROM %s.crdb_internal.backward_dependencies
AS OF SYSTEM TIME '%s'
WHERE descriptor_id = $1
`, parser.Name(dbName).String(), ts), []driver.Value{tableID})
if err != nil {
return tableMetadata{}, err
}
vals = make([]driver.Value, 1)
var refs []int64
for {
if err := rows.Next(vals); err == io.EOF {
break
} else if err != nil {
return tableMetadata{}, err
}
id := vals[0].(int64)
refs = append(refs, id)
}
if err := rows.Close(); err != nil {
return tableMetadata{}, err
}
return tableMetadata{
ID: tableID,
name: name,
columnNames: colnames.String(),
columnTypes: coltypes,
createStmt: create,
dependsOn: refs,
isView: descType,
}, nil
}
// dumpCreateTable dumps the CREATE statement of the specified table to w.
func dumpCreateTable(w io.Writer, md tableMetadata) error {
if _, err := w.Write([]byte(md.createStmt)); err != nil {
return err
}
if _, err := w.Write([]byte(";\n")); err != nil {
return err
}
return nil
}
const (
// insertRows is the number of rows per INSERT statement.
insertRows = 100
)
// dumpTableData dumps the data of the specified table to w.
func dumpTableData(w io.Writer, conn *sqlConn, clusterTS string, md tableMetadata) error {
bs := fmt.Sprintf("SELECT * FROM %s AS OF SYSTEM TIME '%s' ORDER BY PRIMARY KEY %[1]s",
md.name,
clusterTS,
)
inserts := make([]string, 0, insertRows)
rows, err := conn.Query(bs, nil)
if err != nil {
return err
}
cols := rows.Columns()
// Make 2 []driver.Values and alternate sending them on the chan. This is
// needed so val encoding can proceed at the same time as fetching a new
// row. There's no benefit to having more than 2 because that's all we can
// encode at once if we want to preserve the select order.
var valArray [2][]driver.Value
for i := range valArray {
valArray[i] = make([]driver.Value, len(cols))
}
g, ctx := errgroup.WithContext(context.Background())
done := ctx.Done()
valsCh := make(chan []driver.Value)
// stringsCh receives VALUES lines and batches them before writing to the
// output. Buffering this chan allows the val encoding to proceed during
// writes.
stringsCh := make(chan string, insertRows)
g.Go(func() error {
// Fetch SQL rows and put them onto valsCh.
defer close(valsCh)
for i := 0; ; i++ {
vals := valArray[i%len(valArray)]
if err := rows.Next(vals); err == io.EOF {
return rows.Close()
} else if err != nil {
return err
}
select {
case <-done:
return ctx.Err()
case valsCh <- vals:
}
}
})
g.Go(func() error {
// Convert SQL rows into VALUE strings.
defer close(stringsCh)
var ivals bytes.Buffer
for vals := range valsCh {
ivals.Reset()
// Values need to be correctly encoded for INSERT statements in a text file.
for si, sv := range vals {
if si > 0 {
ivals.WriteString(", ")
}
var d parser.Datum
switch t := sv.(type) {
case nil:
d = parser.DNull
case bool:
d = parser.MakeDBool(parser.DBool(t))
case int64:
d = parser.NewDInt(parser.DInt(t))
case float64:
d = parser.NewDFloat(parser.DFloat(t))
case string:
d = parser.NewDString(t)
case []byte:
switch ct := md.columnTypes[cols[si]]; ct {
case "INTERVAL":
d, err = parser.ParseDInterval(string(t))
if err != nil {
return err
}
case "BYTES":
d = parser.NewDBytes(parser.DBytes(t))
case "UUID":
d, err = parser.ParseDUuidFromString(string(t))
if err != nil {
return err
}
default:
// STRING and DECIMAL types can have optional length
// suffixes, so only examine the prefix of the type.
// In addition, we can only observe ARRAY types by their [] suffix.
if strings.HasSuffix(md.columnTypes[cols[si]], "[]") {
typ := strings.TrimRight(md.columnTypes[cols[si]], "[]")
elemType, err := parser.StringToColType(typ)
if err != nil {
return err
}
d, err = parser.ParseDArrayFromString(parser.NewTestingEvalContext(), string(t), elemType)
if err != nil {
return err
}
} else if strings.HasPrefix(md.columnTypes[cols[si]], "STRING") {
d = parser.NewDString(string(t))
} else if strings.HasPrefix(md.columnTypes[cols[si]], "DECIMAL") {
d, err = parser.ParseDDecimal(string(t))
if err != nil {
return err
}
} else {
return errors.Errorf("unknown []byte type: %s, %v: %s", t, cols[si], md.columnTypes[cols[si]])
}
}
case time.Time:
ct := md.columnTypes[cols[si]]
switch ct {
case "DATE":
d = parser.NewDDateFromTime(t, time.UTC)
case "TIMESTAMP":
d = parser.MakeDTimestamp(t, time.Nanosecond)
case "TIMESTAMP WITH TIME ZONE":
d = parser.MakeDTimestampTZ(t, time.Nanosecond)
default:
return errors.Errorf("unknown timestamp type: %s, %v: %s", t, cols[si], md.columnTypes[cols[si]])
}
default:
return errors.Errorf("unknown field type: %T (%s)", t, cols[si])
}
d.Format(&ivals, parser.FmtParsable)
}
select {
case <-done:
return ctx.Err()
case stringsCh <- ivals.String():
}
}
return nil
})
g.Go(func() error {
// Batch SQL strings into groups and write to output.
for s := range stringsCh {
inserts = append(inserts, s)
if len(inserts) == cap(inserts) {
writeInserts(w, md, inserts)
inserts = inserts[:0]
}
}
if len(inserts) != 0 {
writeInserts(w, md, inserts)
inserts = inserts[:0]
}
return nil
})
return g.Wait()
}
func writeInserts(w io.Writer, md tableMetadata, inserts []string) {
fmt.Fprintf(w, "\nINSERT INTO %s (%s) VALUES", md.name.TableName, md.columnNames)
for idx, values := range inserts {
if idx > 0 {
fmt.Fprint(w, ",")
}
fmt.Fprintf(w, "\n\t(%s)", values)
}
fmt.Fprintln(w, ";")
}