-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.go
49 lines (41 loc) · 1.12 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
package commands
import (
"fmt"
"github.com/pivotal-cf/pg2mysql"
)
type MigrateCommand struct {
Truncate bool `long:"truncate" description:"Truncate destination tables before migrating data"`
}
func (c *MigrateCommand) Execute([]string) error {
mysql := pg2mysql.NewMySQLDB(
PG2MySQL.Config.MySQL.Database,
PG2MySQL.Config.MySQL.Username,
PG2MySQL.Config.MySQL.Password,
PG2MySQL.Config.MySQL.Host,
PG2MySQL.Config.MySQL.Port,
)
err := mysql.Open()
if err != nil {
return fmt.Errorf("failed to open mysql connection: %s", err)
}
defer mysql.Close()
pg := pg2mysql.NewPostgreSQLDB(
PG2MySQL.Config.PostgreSQL.Database,
PG2MySQL.Config.PostgreSQL.Username,
PG2MySQL.Config.PostgreSQL.Password,
PG2MySQL.Config.PostgreSQL.Host,
PG2MySQL.Config.PostgreSQL.Port,
PG2MySQL.Config.PostgreSQL.SSLMode,
)
err = pg.Open()
if err != nil {
return fmt.Errorf("failed to open pg connection: %s", err)
}
defer pg.Close()
watcher := pg2mysql.NewStdoutPrinter()
err = pg2mysql.NewMigrator(pg, mysql, c.Truncate, watcher).Migrate()
if err != nil {
return fmt.Errorf("failed migrating: %s", err)
}
return nil
}