-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump_sequencer.go
65 lines (58 loc) · 1.3 KB
/
dump_sequencer.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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"sync"
"github.com/hashicorp/hcl/v2"
"github.com/heimdalr/dag"
"github.com/pingcap/tidb/parser"
_ "github.com/pingcap/tidb/types/parser_driver"
)
type DumpSequencer struct {
Config *Config
}
func NewDumpSequencer(config *Config) *DumpSequencer {
return &DumpSequencer{Config: config}
}
func (s *DumpSequencer) Dump() error {
for _, database := range s.Config.Databases {
if err := s.DumpDatabase(database); err != nil {
return err
}
}
return nil
}
func (s *DumpSequencer) DumpDatabase(database *Database) error {
dumper, err := NewDumper(s.Config.Options)
if err != nil {
return err
}
for _, table := range database.Tables {
outFile, err := ioutil.TempFile("", table.Name)
if err != nil {
log.Fatal(err)
}
table.OutFile = outFile
defer os.Remove(table.OutFile.Name())
}
var diags hcl.Diagnostics
visitor := &Rewriter{
Database: database,
Dumper: dumper,
Parser: parser.New(),
Diagnostics: diags,
Wg: &sync.WaitGroup{},
}
dag.BFSWalk(database.DAG, visitor)
var sb strings.Builder
wr := hcl.NewDiagnosticTextWriter(&sb, nil, 78, true)
wr.WriteDiagnostics(visitor.Diagnostics)
if diags.HasErrors() {
err = fmt.Errorf("Could not read config")
}
log.Println(sb.String())
return err
}