forked from mkabilov/pg2ch
/
main.go
92 lines (78 loc) · 2.18 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"flag"
"fmt"
"os"
"runtime"
"github.com/mkabilov/pg2ch/pkg/config"
"github.com/mkabilov/pg2ch/pkg/replicator"
)
var (
configFile = flag.String("config", "config.yaml", "path to the config file")
generateChDDL = flag.Bool("generate-ch-ddl", false, "generates clickhouse's tables ddl")
printTablesLSN = flag.Bool("print-lsn", false, "print saved LSNs for replicated tables")
onlySync = flag.Bool("sync", false, "sync tables and exit")
preparePgTables = flag.Bool("prepare-pg-tables", false, "Set replica identity for the tables, add tables to the publication")
Version = "devel"
Revision = "devel"
GoVersion = runtime.Version()
)
func buildInfo() string {
return fmt.Sprintf("Postgresql to Clickhouse replicator %s git revision %s go version %s", Version, Revision, GoVersion)
}
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "%s\n", buildInfo())
fmt.Fprintf(os.Stderr, "\nUsage:\n")
flag.PrintDefaults()
}
flag.Parse()
if *configFile == "" {
flag.Usage()
os.Exit(1)
}
}
func main() {
cfg, err := config.New(*configFile)
if err != nil {
fmt.Fprintf(os.Stderr, "could not load config: %v\n", err)
os.Exit(1)
}
cfg.Print()
repl := replicator.New(cfg)
switch {
case *generateChDDL:
if err := repl.GenerateChDDL(); err != nil {
fmt.Fprintf(os.Stderr, "could not create tables on the clickhouse side: %v\n", err)
os.Exit(1)
}
case *printTablesLSN:
if err := repl.Init(); err != nil {
fmt.Fprintf(os.Stderr, "could not init tables: %v\n", err)
os.Exit(1)
}
repl.PrintTablesLSN()
case *onlySync:
var err error
if err = repl.Init(); err == nil {
var tablesToSync []config.PgTableName
if tablesToSync, err = repl.GetTablesToSync(); err == nil {
err = repl.SyncTables(tablesToSync, false)
}
}
if err != nil {
fmt.Fprintf(os.Stderr, "could not sync tables: %v\n", err)
os.Exit(1)
}
case *preparePgTables:
if err := repl.PreparePgTables(); err != nil {
fmt.Fprintf(os.Stderr, "could not set replica identity: %v\n", err)
os.Exit(1)
}
default:
if err := repl.Run(); err != nil {
fmt.Fprintf(os.Stderr, "could not start: %v\n", err)
os.Exit(1)
}
}
}