-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
71 lines (56 loc) · 1.81 KB
/
main.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
package main
import (
"context"
"database/sql"
"flag"
"fmt"
"github.com/dearcode/crab/log"
"github.com/dearcode/crab/uuid"
"github.com/dearcode/archiver/harvester"
_ "github.com/dearcode/archiver/harvester/mysql"
"github.com/dearcode/archiver/purger"
_ "github.com/dearcode/archiver/purger/mysql"
)
var (
srcType = flag.String("t", "mysql", "source db type, default is mysql")
source = flag.String("source", "", "DSN specifying the table to archive from (required), username:password@tcp(address)/dbname")
dest = flag.String("dest", "", "DSN specifying the table to archive, username:password@tcp(address)/dbname")
table = flag.String("T", "", "table name")
where = flag.String("W", "", "where clause to limit which rows to archiv")
purge = flag.Bool("-purge", false, "purge data")
limit = flag.Int("-limit", 1000, "select limit")
)
func main() {
flag.StringVar(where, "-where", "", "where clause to limit which rows to archiv")
flag.StringVar(table, "-table", "", "table name")
flag.Parse()
ctx := context.Background()
session := uuid.String()
ctx = context.WithValue(ctx, "session", session)
h, err := harvester.New(*srcType)
if err != nil {
log.Fatalf("%v harvester New error:%v", session, err)
}
p, err := purger.New(*srcType)
if err != nil {
log.Fatalf("%v purger New error:%v", session, err)
}
rows, err := h.Start(ctx, *source, *table, *where, *limit)
if err != nil {
log.Fatalf("%v harvester Start error:%v", session, err)
}
stmt := fmt.Sprintf("delete from %s where %s = ?", *table, h.PrimaryKey())
pc, err := p.Start(ctx, *host, *port, *user, *password, *db, stmt)
if err != nil {
log.Fatalf("%v purger Start error:%v", session, err)
}
idx := 0
for i, c := range h.TableDef() {
if c.Key == "PRI" {
idx = i
}
}
for row := range rows {
pc <- row[idx].String
}
}