-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
283 lines (246 loc) · 7.57 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package main
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"time"
"github.com/jackc/pgx/v5"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type Config struct {
Source DB `yaml:"source"`
Destination DB `yaml:"destination"`
Condition string `yaml:"condition"`
}
type DB struct {
DB string `yaml:"db"`
Table string `yaml:"table"`
}
var config Config
func main() {
cobra.OnInitialize(initConfig)
rootCmd.AddCommand(testCmd)
rootCmd.AddCommand(dumpCmd)
rootCmd.AddCommand(loadCmd)
rootCmd.AddCommand(transferCmd)
rootCmd.AddCommand(transferNamesCmd)
cobra.CheckErr(rootCmd.Execute())
}
func initConfig() {
viper.AddConfigPath("config")
viper.SetConfigName("local")
viper.SetConfigType("yaml")
viper.AutomaticEnv()
err := viper.ReadInConfig()
if err != nil {
return
}
err = viper.Unmarshal(&config)
if err != nil {
return
}
}
var dumpCmd = &cobra.Command{
Use: "dump",
Short: "Dump data from source database to a file",
Run: func(cmd *cobra.Command, args []string) {
sourceConn := openDB(config.Source)
defer sourceConn.Close(context.Background())
filename := fmt.Sprintf("dump-%s.txt", config.Source.Table)
file, err := os.Create(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
absPath, err := filepath.Abs(file.Name())
if err != nil {
fmt.Println("Error getting absolute path:", err)
return
}
dump(sourceConn, config.Source.Table, absPath, config.Condition)
fmt.Printf("Data dumped to %s\n", absPath)
},
}
var loadCmd = &cobra.Command{
Use: "load",
Short: "Load data from a file to destination database",
Run: func(cmd *cobra.Command, args []string) {
destConn := openDB(config.Destination)
defer destConn.Close(context.Background())
filename := fmt.Sprintf("dump-%s.txt", config.Destination.Table)
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
absPath, err := filepath.Abs(file.Name())
if err != nil {
fmt.Println("Error getting absolute path:", err)
return
}
load(destConn, config.Destination.Table, absPath)
fmt.Printf("Data loaded from %s\n", absPath)
},
}
// Define a new command called "transfer"
var testCmd = &cobra.Command{
Use: "test",
Short: "Check if database connection is working",
Run: func(cmd *cobra.Command, args []string) {
sourceConn := openDB(config.Source)
destConn := openDB(config.Destination)
defer sourceConn.Close(context.Background())
defer destConn.Close(context.Background())
},
}
var transferCmd = &cobra.Command{
Use: "transfer",
Short: "Transfer data from source database to destination database",
Run: func(cmd *cobra.Command, args []string) {
sourceConn := openDB(config.Source)
destConn := openDB(config.Destination)
fmt.Println("Database connection is working")
defer sourceConn.Close(context.Background())
defer destConn.Close(context.Background())
filename := fmt.Sprintf("dump-%s.txt", config.Source.Table)
file, err := os.Create(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
absPath, err := filepath.Abs(file.Name())
if err != nil {
fmt.Println("Error getting absolute path:", err)
return
}
dump(sourceConn, config.Source.Table, absPath, config.Condition)
load(destConn, config.Destination.Table, absPath)
fmt.Println("Data transfered successfully")
},
}
var transferNamesCmd = &cobra.Command{
Use: "transfer-names",
Short: "Transfer names from source database to destination database",
Run: func(cmd *cobra.Command, args []string) {
sourceConn := openDB(config.Source)
destConn := openDB(config.Destination)
fmt.Println("Database connection is working")
defer sourceConn.Close(context.Background())
defer destConn.Close(context.Background())
rows, err := readFromSourceTable(sourceConn)
if err != nil {
log.Fatalf("Error reading from source table: %v", err)
}
for _, row := range rows {
err = writeToTargetTable(destConn, row)
if err != nil {
log.Printf("Error writing to target table: %v", err)
}
}
fmt.Println("Data transfered successfully")
},
}
var rootCmd = &cobra.Command{
Use: "blockchain-vc-transfer",
Short: "Tool to transfer verified contracts data between 2 instance of blockscout dbs",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help() // Trigger help text
os.Exit(0) // Exit after displaying help
},
}
func tableExists(conn *pgx.Conn, table string) bool {
var exists bool
err := conn.QueryRow(context.Background(), "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = $1)", table).Scan(&exists)
if err != nil {
fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
os.Exit(1)
}
return exists
}
func openDB(dbConfig DB) *pgx.Conn {
conn, err := pgx.Connect(context.Background(), dbConfig.DB)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
if !tableExists(conn, dbConfig.Table) {
fmt.Println("Table does not exist")
os.Exit(1)
}
return conn
}
func dump(conn *pgx.Conn, table string, filename string, condition string) {
if config.Condition != "" {
table = fmt.Sprintf("%s WHERE %s", table, config.Condition)
}
copyToQuery := fmt.Sprintf("COPY (SELECT * FROM %s) TO '%s' WITH BINARY", table, filename)
_, err := conn.Exec(context.Background(), copyToQuery)
if err != nil {
fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
os.Exit(1)
}
}
func load(conn *pgx.Conn, table string, filename string) {
copyFromQuery := fmt.Sprintf("COPY %s FROM '%s' WITH BINARY", table, filename)
_, err := conn.Exec(context.Background(), copyFromQuery)
if err != nil {
fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
os.Exit(1)
}
}
func readFromSourceTable(conn *pgx.Conn) ([]map[string]interface{}, error) {
query := "SELECT address_hash, name, \"primary\", inserted_at, updated_at, metadata, id FROM address_names"
rows, err := conn.Query(context.Background(), query)
if err != nil {
return nil, fmt.Errorf("query failed: %v", err)
}
defer rows.Close()
var results []map[string]interface{}
for rows.Next() {
var addressHash []byte
var name string
var primary bool
var insertedAt, updatedAt time.Time
var metadata map[string]interface{}
var id int
err = rows.Scan(&addressHash, &name, &primary, &insertedAt, &updatedAt, &metadata, &id)
if err != nil {
return nil, fmt.Errorf("row scan failed: %v", err)
}
row := map[string]interface{}{
"address_hash": addressHash,
"name": name,
"primary": primary,
"inserted_at": insertedAt,
"updated_at": updatedAt,
"metadata": metadata,
"id": id,
}
results = append(results, row)
}
return results, nil
}
func writeToTargetTable(conn *pgx.Conn, row map[string]interface{}) error {
// Check if the address_hash already exists in the target table
var exists bool
checkQuery := "SELECT EXISTS(SELECT 1 FROM address_names WHERE address_hash = $1)"
err := conn.QueryRow(context.Background(), checkQuery, row["address_hash"]).Scan(&exists)
if err != nil {
return fmt.Errorf("check query failed: %v", err)
}
if exists {
return nil // If the address_hash already exists, do nothing
}
fmt.Printf("Inserting row with address_hash: %x\n", row["address_hash"])
// Insert the row into the target table
insertQuery := `INSERT INTO address_names (address_hash, name, "primary", inserted_at, updated_at, metadata, id)
VALUES ($1, $2, $3, $4, $5, $6, $7)`
_, err = conn.Exec(context.Background(), insertQuery,
row["address_hash"], row["name"], row["primary"], row["inserted_at"], row["updated_at"], row["metadata"], row["id"])
if err != nil {
return fmt.Errorf("insert query failed: %v", err)
}
return nil
}