-
Notifications
You must be signed in to change notification settings - Fork 8
/
etl.go
398 lines (321 loc) · 9.35 KB
/
etl.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package main
import (
"database/sql"
"encoding/json"
_ "github.com/lib/pq"
"log"
"os"
"sort"
"strings"
)
func CreateStringArray(values []string) string {
return "{" + strings.Join(values, ",") + "}"
}
func ToSortedLower(things []string) []string {
sorted := []string{}
for _, thing := range things {
sorted = append(sorted, strings.ToLower(strings.Replace(thing, ",", "", -1)))
}
sort.Strings(sorted)
return sorted
}
func ToUniqueLower(things []string) []string {
seen := map[string]bool{}
sorted := []string{}
for _, thing := range things {
if _, found := seen[thing]; !found {
sorted = append(sorted, strings.ToLower(thing))
seen[thing] = true
}
}
sort.Strings(sorted)
return sorted
}
func transformRarity(rarity string) string {
r := strings.ToLower(rarity)
if r == "mythic rare" {
return "mythic"
}
if r == "basic land" {
return "basic"
}
return r
}
func TransformEdition(s MTGSet, c MTGCard) Edition {
return Edition{
Set: s.Name,
SetId: s.Code,
Flavor: c.Flavor,
MultiverseId: c.MultiverseId,
Watermark: c.Watermark,
Rarity: transformRarity(c.Rarity),
Artist: c.Artist,
Border: c.Border,
Layout: c.Layout,
Number: c.Number,
CardId: c.Id(),
}
}
// FIXME: Add released dates
func TransformSet(s MTGSet) Set {
return Set{
Name: s.Name,
Id: s.Code,
Border: s.Border,
Type: s.Type,
}
}
func TransformCard(c MTGCard) Card {
return Card{
Name: c.Name,
Id: c.Id(),
Text: c.Text,
Colors: ToSortedLower(c.Colors),
Types: ToSortedLower(c.Types),
Supertypes: ToSortedLower(c.Supertypes),
Subtypes: ToSortedLower(c.Subtypes),
Power: c.Power,
Toughness: c.Toughness,
Loyalty: c.Loyalty,
ManaCost: c.ManaCost,
ConvertedCost: int(c.ConvertedCost),
}
}
func TransformCollection(collection MTGCollection, formats []MTGFormat) ([]Set, []Card) {
cards := []Card{}
ids := map[string]Card{}
editions := []Edition{}
sets := []Set{}
for _, set := range collection {
sets = append(sets, TransformSet(set))
for _, card := range set.Cards {
newcard := TransformCard(card)
newedition := TransformEdition(set, card)
if _, found := ids[newcard.Id]; !found {
ids[newcard.Id] = newcard
cards = append(cards, newcard)
}
editions = append(editions, newedition)
}
}
for i, c := range cards {
for _, edition := range editions {
if edition.CardId == c.Id {
cards[i].Editions = append(cards[i].Editions, edition)
}
}
cards[i].Fill()
}
for _, format := range formats {
for i, _ := range cards {
AddFormat(&cards[i], &format)
}
}
return sets, cards
}
func AddFormat(c *Card, f *MTGFormat) {
if c.FormatMap == nil {
c.FormatMap = map[string]string{}
}
for _, special := range []string{"phenomenon", "plane", "scheme", "vanguard"} {
for _, t := range c.Types {
if t == special {
return
}
}
}
for _, edition := range c.Editions {
if edition.SetId == "UNH" || edition.SetId == "UGL" {
return
}
}
for _, b := range f.Banned {
if c.Id == b.Id {
c.FormatMap[f.Name] = "banned"
return
}
}
for _, r := range f.Restricted {
if c.Id == r.Id {
c.FormatMap[f.Name] = "restricted"
return
}
}
if len(f.Sets) == 0 {
c.FormatMap[f.Name] = "legal"
return
}
for _, edition := range c.Editions {
for _, format_set := range f.Sets {
if strings.ToUpper(format_set) == strings.ToUpper(edition.SetId) {
c.FormatMap[f.Name] = "legal"
return
}
}
}
}
// FIXME: Add Sets
func CreateCollection(db *sql.DB, collection MTGCollection) error {
tx, err := db.Begin()
if err != nil {
return err
}
formats, err := LoadFormats()
if err != nil {
return err
}
sets, cards := TransformCollection(collection, formats)
for _, s := range sets {
_, err := tx.Exec("INSERT INTO sets (id, name, border, type) VALUES ($1, $2, $3, $4)",
s.Id, s.Name, s.Border, s.Type)
if err != nil {
tx.Rollback()
return err
}
}
for _, c := range cards {
blob, err := json.Marshal(c)
if err != nil {
tx.Rollback()
return err
}
columns := []string{
"id", "name", "record", "rules", "mana_cost", "cmc",
"power", "toughness", "loyalty", "multicolor", "rarities",
"types", "subtypes", "supertypes", "colors", "sets",
"formats", "status", "mids",
}
q := Insert(columns, "cards")
_, err = tx.Exec(q, c.Id, c.Name, blob, c.Text, c.ManaCost, c.ConvertedCost,
c.Power, c.Toughness, c.Loyalty, c.Multicolor(),
CreateStringArray(c.Rarities()), CreateStringArray(c.Types),
CreateStringArray(c.Subtypes), CreateStringArray(c.Supertypes),
CreateStringArray(c.Colors), CreateStringArray(c.Sets()),
CreateStringArray(c.Formats()), CreateStringArray(c.Status()),
CreateStringArray(c.MultiverseIds()))
if err != nil {
tx.Rollback()
return err
}
}
return tx.Commit()
}
func LoadFormats() ([]MTGFormat, error) {
paths := []string{
"formats/vintage.json", "formats/legacy.json",
"formats/commander.json", "formats/standard.json",
"formats/modern.json",
}
formats := []MTGFormat{}
for _, path := range paths {
f, err := LoadFormat(path)
if err != nil {
return formats, err
}
formats = append(formats, f)
}
return formats, nil
}
func exec(db *sql.DB, query string, args ...interface{}) {
if _, err := db.Exec(query, args...); err != nil {
log.Fatal(query, " ", err)
}
}
func CreateDatabase(db *sql.DB) {
user := os.Getenv("DATABASE_USER")
pass := os.Getenv("DATABASE_PASSWORD")
if user == "" || pass == "" {
log.Fatal("DATABASE_USER and DATABASE_PASSWORD must be set")
}
exec(db, "DROP DATABASE IF EXISTS deckbrew")
exec(db, "DROP USER IF EXISTS "+user)
exec(db, "CREATE DATABASE deckbrew WITH template=template0 encoding='UTF8'")
exec(db, "CREATE USER "+user+" WITH PASSWORD '"+pass+"'")
exec(db, "GRANT ALL PRIVILEGES ON DATABASE deckbrew TO "+user)
}
func CreateTables(db *sql.DB) {
user := os.Getenv("DATABASE_USER")
exec(db, "CREATE EXTENSION pg_trgm")
exec(db, `CREATE TABLE cards (
id varchar(32) primary key,
name varchar(200) DEFAULT '',
mana_cost varchar(45) DEFAULT '',
toughness varchar(6) DEFAULT '',
power varchar(6) DEFAULT '',
types varchar(20)[] DEFAULT '{}',
rarities varchar(15)[] DEFAULT '{}',
sets varchar(3)[] DEFAULT '{}',
subtypes varchar(100)[] DEFAULT '{}',
supertypes varchar(100)[] DEFAULT '{}',
colors varchar(5)[] DEFAULT '{}',
formats varchar(9)[] DEFAULT '{}',
status varchar(10)[] DEFAULT '{}',
mids varchar(20)[] DEFAULT '{}',
multicolor boolean DEFAULT false,
record text DEFAULT '',
rules text DEFAULT '',
loyalty integer DEFAULT 0,
cmc integer DEFAULT 0)`)
exec(db, `CREATE TABLE sets (
id varchar(3) primary key,
name varchar(200) DEFAULT '',
border varchar(40) DEFAULT '',
type varchar(32) DEFAULT '')`)
exec(db, "CREATE INDEX cards_name_id ON cards(id)")
exec(db, "CREATE INDEX cards_power_index ON cards(power)")
exec(db, "CREATE INDEX cards_toughness_index ON cards(toughness)")
exec(db, "CREATE INDEX cards_names_sort_index ON cards(name)")
exec(db, "CREATE INDEX cards_multicolor_index ON cards(multicolor)")
exec(db, "CREATE INDEX cards_types_index ON cards USING GIN(types)")
exec(db, "CREATE INDEX cards_subtypes_index ON cards USING GIN(subtypes)")
exec(db, "CREATE INDEX cards_supertypes_index ON cards USING GIN(supertypes)")
exec(db, "CREATE INDEX cards_colors_index ON cards USING GIN(colors)")
exec(db, "CREATE INDEX cards_sets_index ON cards USING GIN(sets)")
exec(db, "CREATE INDEX cards_rarities_index ON cards USING GIN(rarities)")
exec(db, "CREATE INDEX cards_status_index ON cards USING GIN(status)")
exec(db, "CREATE INDEX cards_formats_index ON cards USING GIN(formats)")
exec(db, "CREATE INDEX cards_mids_index ON cards USING GIN(mids)")
exec(db, "CREATE INDEX cards_names_index ON cards USING GIN(name gin_trgm_ops)")
exec(db, "CREATE INDEX cards_rules_index ON cards USING GIN(rules gin_trgm_ops)")
exec(db, "CREATE INDEX sets_type_index ON sets(type)")
exec(db, "CREATE INDEX sets_border_index ON sets(border)")
exec(db, "GRANT ALL PRIVILEGES ON TABLE cards TO "+user)
exec(db, "GRANT ALL PRIVILEGES ON TABLE sets TO "+user)
}
func SyncDatabase(path string) error {
host := os.Getenv("DATABASE_HOST")
if host == "" {
host = "localhost"
}
master, err := sql.Open("postgres", "host="+host+" sslmode=disable")
if err != nil {
return err
}
if master.Ping() != nil {
log.Println("Can't create database")
return master.Ping()
}
CreateDatabase(master)
sdb, err := sql.Open("postgres", "host="+host+" dbname=deckbrew sslmode=disable")
if err != nil {
return err
}
if sdb.Ping() != nil {
log.Println("Can't create tables")
return sdb.Ping()
}
CreateTables(sdb)
collection, err := LoadCollection(path)
if err != nil {
return err
}
db, err := GetDatabase()
if err != nil {
return err
}
err = CreateCollection(db, collection)
if err != nil {
return err
}
return nil
}