-
Notifications
You must be signed in to change notification settings - Fork 4
/
key.go
53 lines (39 loc) · 974 Bytes
/
key.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
package migrator
import "strings"
type keys []Key
func (k keys) render() string {
values := []string{}
for _, key := range k {
value := key.render()
if value != "" {
values = append(values, value)
}
}
return strings.Join(values, ", ")
}
// Key represents an instance to handle key (index) interactions
type Key struct {
Name string
Type string // primary, unique
Columns []string
}
var keyTypes = list{"PRIMARY", "UNIQUE"}
func (k Key) render() string {
if len(k.Columns) == 0 {
return ""
}
sql := ""
if keyTypes.has(strings.ToUpper(k.Type)) {
sql += strings.ToUpper(k.Type) + " "
}
sql += "KEY"
if k.Name != "" {
sql += " `" + k.Name + "`"
}
sql += " (`" + strings.Join(k.Columns, "`, `") + "`)"
return sql
}
// BuildUniqueKeyNameOnTable builds a name for the foreign key on the table
func BuildUniqueKeyNameOnTable(table string, columns ...string) string {
return table + "_" + strings.Join(columns, "_") + "_unique"
}