-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrations.go
54 lines (46 loc) · 1.08 KB
/
migrations.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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"io"
"log"
"os"
)
type Car struct {
ID int `json:"id"`
BrandName string `json:"brand_name"`
Name string `json:"name"`
AveragePrice int `json:"average_price"`
}
func RunMigrations(db *sql.DB) {
jsonFile, err := os.Open("models.json")
if err != nil {
log.Fatal(err)
}
defer jsonFile.Close()
byteValue, err := io.ReadAll(jsonFile)
if err != nil {
log.Fatal(err)
}
var cars []Car
if err := json.Unmarshal(byteValue, &cars); err != nil {
log.Fatal(err)
}
for _, car := range cars {
var brandID int
err := db.QueryRow("SELECT id FROM brands WHERE name = $1", car.BrandName).Scan(&brandID)
if err != nil {
err = db.QueryRow("INSERT INTO brands (name) VALUES ($1) RETURNING id", car.BrandName).Scan(&brandID)
if err != nil {
log.Fatal(err)
}
}
_, err = db.Exec("INSERT INTO models (name, average_price, brand_id) VALUES ($1, $2, $3)",
car.Name, car.AveragePrice, brandID)
if err != nil {
log.Fatal(err)
}
}
fmt.Println("Datos insertados exitosamente")
}