forked from gobuffalo/fizz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
foreign_keys.go
75 lines (65 loc) · 1.57 KB
/
foreign_keys.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
package fizz
import (
"fmt"
"strings"
"github.com/pkg/errors"
)
type ForeignKeyRef struct {
Table string
Columns []string
}
type ForeignKey struct {
Name string
Column string
References ForeignKeyRef
Options Options
}
func (f fizzer) AddForeignKey(table string, column string, refs interface{}, options Options) error {
fkr, err := parseForeignKeyRef(refs)
if err != nil {
return errors.WithStack(err)
}
fk := ForeignKey{
Column: column,
References: fkr,
Options: options,
}
if options["name"] != nil {
fk.Name = options["name"].(string)
} else {
fk.Name = fmt.Sprintf("%s_%s_%s_fk", table, fk.References.Table, strings.Join(fk.References.Columns, "_"))
}
f.add(f.Bubbler.AddForeignKey(Table{
Name: table,
ForeignKeys: []ForeignKey{fk},
}))
return nil
}
func (f fizzer) DropForeignKey(table string, fk string, options Options) {
f.add(f.Bubbler.DropForeignKey(Table{
Name: table,
ForeignKeys: []ForeignKey{
{
Name: fk,
Options: options,
},
},
}))
}
func parseForeignKeyRef(refs interface{}) (ForeignKeyRef, error) {
fkr := ForeignKeyRef{}
refMap, ok := refs.(map[string]interface{})
if !ok {
return fkr, errors.Errorf(`invalid references format %s\nmust be "{"table": ["colum1", "column2"]}"`, refs)
}
if len(refMap) != 1 {
return fkr, errors.Errorf("only one table is supported as Foreign key reference")
}
for table, columns := range refMap {
fkr.Table = table
for _, c := range columns.([]interface{}) {
fkr.Columns = append(fkr.Columns, fmt.Sprintf("%s", c))
}
}
return fkr, nil
}