-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseeder.go
85 lines (70 loc) · 1.83 KB
/
seeder.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
// Package seeder provides a set of tools to seed databases.
package seeder
import (
"database/sql"
"fmt"
"log"
"reflect"
)
// Execute executes all the methods given a struct.
func Execute(s interface{}, seedMethodNames ...string) error {
sType := reflect.TypeOf(s)
sKind := sType.Kind()
if sKind != reflect.Struct {
return fmt.Errorf("receive a %s instead of a struct", sType.String())
}
// Execute all seeders if no method name is given.
if len(seedMethodNames) == 0 {
// We are looping over the method on a Seed struct.
for i := 0; i < sType.NumMethod(); i++ {
// Get the method in the current iteration.
method := sType.Method(i)
// Execute seeder.
if err := seed(s, method.Name); err != nil {
return err
}
}
}
// Execute only the given method names
for _, item := range seedMethodNames {
if err := seed(s, item); err != nil {
return err
}
}
return nil
}
// ExecuteFunc execute one of more functions to seed the database
// using the same pool of connections.
func ExecuteFunc(db *sql.DB, funcs ...func(*sql.DB) error) error {
for _, f := range funcs {
if err := f(db); err != nil {
return err
}
}
return nil
}
// ExecuteTxFunc execute one of more functions to seed the database
// using the same pool of connections.
func ExecuteTxFunc(tx *sql.Tx, funcs ...func(*sql.Tx) error) error {
for _, f := range funcs {
if err := f(tx); err != nil {
return err
}
}
return nil
}
func seed(s interface{}, methodName string) error {
m := reflect.ValueOf(s).MethodByName(methodName)
if !m.IsValid() {
return fmt.Errorf("invalid method name: %s", methodName)
}
log.Println("seeding ", methodName, "...")
values := m.Call(nil)
for _, v := range values {
if err, ok := v.Interface().(error); ok && err != nil {
return err
}
}
log.Println("seed ", methodName, "succeed")
return nil
}