-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathinit_db.go
43 lines (38 loc) · 1014 Bytes
/
init_db.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
package main
import (
"context"
"math/rand"
"time"
opbeansdb "github.com/elastic/opbeans-go/db"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
func initDatabase(db *sqlx.DB, driver string) error {
if orders, err := getOrders(context.Background(), db); err == nil {
if len(orders) != 0 {
return nil
}
}
filenames := []string{
"schema_" + driver + ".sql",
"customers.sql",
"products.sql",
}
logrus.Infof("initializing %q database", driver)
for _, filename := range filenames {
logrus.Infof("executing %q", filename)
f, err := opbeansdb.SQL.Open(filename)
if err != nil {
return err
}
defer f.Close()
if err := opbeansdb.ExecCommands(context.Background(), db, f); err != nil {
return errors.Wrapf(err, "executing %q", filename)
}
}
const numOrders = 5000
logrus.Infof("generating %d random orders", numOrders)
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
return opbeansdb.GenerateOrders(db, driver, numOrders, rng)
}