forked from gobuffalo/fizz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
62 lines (55 loc) · 1.12 KB
/
index.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
package fizz
import (
"fmt"
"strings"
"github.com/pkg/errors"
)
type Index struct {
Name string
Columns []string
Unique bool
Options Options
}
func (f fizzer) AddIndex(table string, columns interface{}, options Options) error {
i := Index{}
switch t := columns.(type) {
default:
return errors.Errorf("unexpected type for columns %T", t) // %T prints whatever type t has
case string:
i.Columns = []string{t}
case []interface{}:
cl := make([]string, len(t))
for i, c := range t {
cl[i] = c.(string)
}
i.Columns = cl
}
if options["name"] != nil {
i.Name = options["name"].(string)
} else {
i.Name = fmt.Sprintf("%s_%s_idx", table, strings.Join(i.Columns, "_"))
}
i.Unique = options["unique"] != nil
f.add(f.Bubbler.AddIndex(Table{
Name: table,
Indexes: []Index{i},
}))
return nil
}
func (f fizzer) DropIndex(table, name string) {
f.add(f.Bubbler.DropIndex(Table{
Name: table,
Indexes: []Index{
{Name: name},
},
}))
}
func (f fizzer) RenameIndex(table, old, new string) {
f.add(f.Bubbler.RenameIndex(Table{
Name: table,
Indexes: []Index{
{Name: old},
{Name: new},
},
}))
}