-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
columns.go
98 lines (87 loc) · 2.06 KB
/
columns.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
86
87
88
89
90
91
92
93
94
95
96
97
98
package fizz
import (
"encoding/json"
"fmt"
"sort"
"strings"
)
// Deprecated: Fizz won't force you to have an ID field now.
var INT_ID_COL = Column{
Name: "id",
Primary: true,
ColType: "integer",
Options: Options{},
}
// Deprecated: Fizz won't force you to have an ID field now.
var UUID_ID_COL = Column{
Name: "id",
Primary: true,
ColType: "uuid",
Options: Options{},
}
var CREATED_COL = Column{Name: "created_at", ColType: "timestamp", Options: nil}
var UPDATED_COL = Column{Name: "updated_at", ColType: "timestamp", Options: nil}
type Column struct {
Name string
ColType string
Primary bool
Options map[string]interface{}
}
func (c Column) String() string {
if c.Primary || c.Options != nil {
var opts map[string]interface{}
if c.Options == nil {
opts = make(map[string]interface{})
} else {
opts = c.Options
}
if c.Primary {
opts["primary"] = 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] })
return fmt.Sprintf(`t.Column("%s", "%s", {%s})`, c.Name, c.ColType, strings.Join(o, ", "))
}
return fmt.Sprintf(`t.Column("%s", "%s")`, c.Name, c.ColType)
}
func (f fizzer) ChangeColumn(table, name, ctype string, options Options) error {
t := Table{
Name: table,
Columns: []Column{
{Name: name, ColType: ctype, Options: options},
},
}
return f.add(f.Bubbler.ChangeColumn(t))
}
func (f fizzer) AddColumn(table, name, ctype string, options Options) error {
t := Table{
Name: table,
Columns: []Column{
{Name: name, ColType: ctype, Options: options},
},
}
return f.add(f.Bubbler.AddColumn(t))
}
func (f fizzer) DropColumn(table, name string) error {
t := Table{
Name: table,
Columns: []Column{
{Name: name},
},
}
return f.add(f.Bubbler.DropColumn(t))
}
func (f fizzer) RenameColumn(table, old, new string) error {
t := Table{
Name: table,
Columns: []Column{
{Name: old},
{Name: new},
},
}
return f.add(f.Bubbler.RenameColumn(t))
}