-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
142 lines (116 loc) · 3.66 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
package main
import (
"context"
"os"
"github.com/jackc/pgx/v5"
"github.com/meilisearch/meilisearch-go"
"golang.org/x/exp/slog"
)
func main() {
conn, err := pgx.Connect(context.Background(), os.Getenv("POSTGRES_URL"))
if err != nil {
slog.Error("Unable to connect to database.", slog.Any("error", err))
os.Exit(1)
}
defer conn.Close(context.Background())
ms := meilisearch.NewClient(meilisearch.ClientConfig{Host: os.Getenv("MEILISEARCH_URL")})
update_tags(conn, ms)
update_videos(conn, ms)
slog.Info("Succeedded to sync.")
}
func update_tags(conn *pgx.Conn, ms *meilisearch.Client) {
rows, err := conn.Query(context.Background(), `SELECT "id","name","tagId" FROM "TagName"`)
defer rows.Close()
if err != nil {
slog.Error("Unable to query tags.", slog.Any("error", err))
os.Exit(1)
}
var docs []map[string]interface{}
for rows.Next() {
var id string
var name string
var tagId string
err = rows.Scan(&id, &name, &tagId)
if err != nil {
slog.Error("Unable to scan the row.", slog.Any("error", err))
os.Exit(1)
}
docs = append(docs, map[string]interface{}{"id": id, "name": name, "tag_id": tagId})
}
slog.Info("Fetched tags.", slog.Int("count", len(docs)))
index := ms.Index("tags")
task, err := index.UpdateIndex("id")
if err != nil {
slog.Error("Unable to set primary key in tags index.", slog.Any("error", err))
os.Exit(1)
}
slog.Info("Set primary key in tags index", slog.Int64("task_id", task.TaskUID))
task, err = index.UpdateDistinctAttribute("tag_id")
if err != nil {
slog.Error("Unable to set distinct attribute in tags index.", slog.Any("error", err))
os.Exit(1)
}
slog.Info("Update distinct attribute in tags index", slog.Int64("task_id", task.TaskUID))
/*
_, err = index.DeleteAllDocuments()
if err != nil {
slog.Error("Unable to delete all documents in tag index", err)
os.Exit(1)
}
*/
task, err = index.AddDocuments(docs)
if err != nil {
slog.Error("Unable to add documents in tags index.", slog.Any("error", err))
os.Exit(1)
}
slog.Info("Add tags documents", slog.Int64("task_id", task.TaskUID))
slog.Info("Succeedded to update tags.")
}
func update_videos(conn *pgx.Conn, ms *meilisearch.Client) {
rows, err := conn.Query(context.Background(), `SELECT "id","title","videoId" FROM "VideoTitle"`)
defer rows.Close()
if err != nil {
slog.Error("Unable to query tags.", slog.Any("error", err))
os.Exit(1)
}
var docs []map[string]interface{}
for rows.Next() {
var id string
var title string
var videoId string
err = rows.Scan(&id, &title, &videoId)
if err != nil {
slog.Error("Unable to scan the row.", slog.Any("error", err))
os.Exit(1)
}
docs = append(docs, map[string]interface{}{"id": id, "title": title, "video_id": videoId})
}
slog.Info("Fetched videos.", slog.Int("count", len(docs)))
index := ms.Index("videos")
task, err := index.UpdateIndex("id")
if err != nil {
slog.Error("Unable to set primary key in videos index.", slog.Any("error", err))
os.Exit(1)
}
slog.Info("Set primary key in video index", slog.Int64("task_id", task.TaskUID))
task, err = index.UpdateDistinctAttribute("video_id")
if err != nil {
slog.Error("Unable to set distinct attribute in videos index.", slog.Any("error", err))
os.Exit(1)
}
slog.Info("Update distinct attribute in video index", slog.Int64("task_id", task.TaskUID))
/*
_, err = index.DeleteAllDocuments()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
*/
task, err = index.AddDocuments(docs)
if err != nil {
slog.Error("Unable to add documents in videos index.", slog.Any("error", err))
os.Exit(1)
}
slog.Info("Add videos documents", slog.Int64("task_id", task.TaskUID))
slog.Info("Succeedded to update videos.")
}