-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
118 lines (96 loc) · 2.15 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
package main
import (
"context"
"math/rand"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/brianvoe/gofakeit/v6"
"github.com/segmentio/ksuid"
"github.com/si3nloong/sqlike/sqlike"
"github.com/si3nloong/sqlike/sqlike/options"
)
const (
dbName = "toxicquery"
tableUser = "User"
tableCar = "Car"
)
func setup() (*sqlike.Client, *sqlike.Database) {
ctx := context.Background()
client := sqlike.MustConnect(
ctx,
"mysql",
options.Connect().
ApplyURI(`root:abcd1234@tcp()/toxicquery?parseTime=true&loc=UTC&charset=utf8mb4&collation=utf8mb4_general_ci`),
)
client.SetPrimaryKey("ID")
// client.SetLogger(&Logger{})
db := client.Database(dbName)
return client, db
}
type User struct {
ID ksuid.KSUID
Name string
Email string
Created time.Time
}
type Car struct {
ID ksuid.KSUID
Model string
FuelType string
Maker string
UserID ksuid.KSUID `sqlike:",foreign_key=User:ID"`
Purchased time.Time
}
type UserCar struct {
User
Car
ID ksuid.KSUID
}
func newUser() *User {
usr := &User{}
usr.ID = ksuid.New()
usr.Name = gofakeit.Name() // Markus Moen
usr.Email = gofakeit.Email()
usr.Created = time.Now().UTC()
return usr
}
func newCar(user *User) *Car {
car := &Car{}
car.ID = ksuid.New()
car.UserID = user.ID
car.Model = gofakeit.CarModel()
car.Maker = gofakeit.CarMaker()
car.FuelType = gofakeit.CarFuelType()
car.Purchased = gofakeit.Date()
return car
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client, db := setup()
defer client.Close()
db.Table(tableUser).MustUnsafeMigrate(ctx, User{})
db.Table(tableCar).MustUnsafeMigrate(ctx, Car{})
// defer cancel()
for i := 0; i < 100; i++ {
users := []*User{}
for j := 0; j < 500; j++ {
users = append(users, newUser())
}
if _, err := db.Table(tableUser).Insert(ctx, &users); err != nil {
panic(err)
}
noOfUser := len(users)
num := rand.Intn(10)
cars := []*Car{}
for j := 0; j < num; j++ {
user := users[rand.Intn(noOfUser)]
cars = append(cars, newCar(user))
}
if len(cars) > 0 {
if _, err := db.Table(tableCar).Insert(ctx, &cars); err != nil {
panic(err)
}
}
}
}