-
Notifications
You must be signed in to change notification settings - Fork 2
/
migrate.go
80 lines (65 loc) · 1.77 KB
/
migrate.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
package main
import (
"fmt"
"log"
"github.com/aktau/gomig/db"
"github.com/aktau/gomig/db/common"
)
const (
PATH_CONFIG_DEFAULT = "config.yml"
)
type MigrateCommand struct {
/* config file */
File string `short:"f" long:"file" description:"The path of the configuration file to use" default:"config.yml"`
}
func (x *MigrateCommand) Execute(args []string) error {
verbosity := len(options.Verbose)
conf := LoadConfigOrDie(x.File)
if verbosity > 2 {
fmt.Println("config:", conf)
}
/* open source */
if verbosity > 0 {
log.Println("gomig: connecting to source", conf.Mysql)
}
reader, err := db.OpenReader("mysql", conf.Mysql)
if err != nil {
return fmt.Errorf("gomig: error while creating reader, %v", err)
}
defer reader.Close()
if verbosity > 0 {
log.Println("gomig: succesfully connected to source")
}
/* open destination */
if verbosity > 0 {
log.Println("gomig: connecting to destination", conf.Destination)
}
var writer common.WriteCloser
if conf.Destination.File != "" {
writer, err = db.OpenFileWriter("postgres", conf.Destination.File)
} else {
writer, err = db.OpenWriter("postgres", conf.Destination.Postgres)
}
if err != nil {
return fmt.Errorf("gomig: error while creating writer: %v", err)
}
defer writer.Close()
if verbosity > 0 {
log.Println("gomig: succesfully connected to destination")
}
log.Println("gomig: converting")
err = Convert(reader, writer, conf, verbosity)
if err != nil {
fmt.Println("gomig: could not complete conversion, error:", err)
} else {
log.Println("gomig: done")
}
return nil
}
func init() {
var cmd MigrateCommand
parser.AddCommand("migrate",
"Migrate data from a source database to a destination file/database",
"Migrate data from a source database to a destination file/database",
&cmd)
}