forked from buidl-labs/celo-indexer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (69 loc) · 1.54 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
package main
import (
"context"
"log"
"os"
"github.com/buidl-labs/celo-indexer/indexer"
"github.com/buidl-labs/celo-voting-validator-backend/graph/database"
"github.com/buidl-labs/celo-voting-validator-backend/graph/model"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
"github.com/joho/godotenv"
)
func main() {
if err := godotenv.Load(); err != nil {
log.Println("Error loading .env file")
}
DbURL := os.Getenv("DB_URL")
if DbURL == "" {
log.Fatal("Please provide a DB url.")
}
opts, err := pg.ParseURL(DbURL)
if err != nil {
log.Fatal(err)
}
DB := database.New(opts)
defer DB.Close()
// DB.AddQueryHook(pgdebug.DebugHook{
// Verbose: true,
// })
ctx := context.Background()
if err := DB.Ping(ctx); err != nil {
log.Println(err)
}
// dropAllTables(DB)
// createAllTables(DB)
indexer.Index(DB)
}
func dropAllTables(DB *pg.DB) {
qs := []string{
"drop table if exists epochs",
"drop table if exists validators",
"drop table if exists validator_stats",
"drop table if exists validator_groups",
"drop table if exists validator_group_stats",
}
for _, q := range qs {
_, err := DB.Exec(q)
if err != nil {
panic(err)
}
}
}
func createAllTables(DB *pg.DB) {
models := []interface{}{
(*model.Epoch)(nil),
(*model.ValidatorGroup)(nil),
(*model.ValidatorGroupStats)(nil),
(*model.Validator)(nil),
(*model.ValidatorStats)(nil),
}
for _, model := range models {
err := DB.Model(model).CreateTable(&orm.CreateTableOptions{
Temp: false,
})
if err != nil {
log.Print(err)
}
}
}