forked from gobuffalo/fizz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
73 lines (67 loc) · 1.47 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
63
64
65
66
67
68
69
70
71
72
73
package fizz
import (
"encoding/json"
"fmt"
"sort"
"strings"
)
// Index is the index definition for fizz.
type Index struct {
Name string
Columns []string
Unique bool
Options Options
}
func (i Index) String() string {
var opts map[string]interface{}
if i.Options == nil {
opts = make(map[string]interface{}, 0)
} else {
opts = i.Options
}
if i.Name != "" {
opts["name"] = i.Name
}
if i.Unique {
opts["unique"] = true
}
o := make([]string, 0, len(opts))
for k, v := range opts {
vv, _ := json.Marshal(v)
o = append(o, fmt.Sprintf("%s: %s", k, string(vv)))
}
sort.SliceStable(o, func(i, j int) bool { return o[i] < o[j] })
if len(i.Columns) > 1 {
cols := make([]string, len(i.Columns))
for k, v := range i.Columns {
cols[k] = `"` + v + `"`
}
return fmt.Sprintf(`t.Index([%s], {%s})`, strings.Join(cols, ", "), strings.Join(o, ", "))
}
return fmt.Sprintf(`t.Index("%s", {%s})`, i.Columns[0], strings.Join(o, ", "))
}
func (f fizzer) AddIndex(table string, columns interface{}, options Options) error {
t := NewTable(table, nil)
if err := t.Index(columns, options); err != nil {
return err
}
f.add(f.Bubbler.AddIndex(t))
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},
},
}))
}