-
Notifications
You must be signed in to change notification settings - Fork 4
/
table.go
221 lines (189 loc) · 6.22 KB
/
table.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package migrator
// Table is an entity to create a table.
//
// - Name table name
// - Engine default: InnoDB
// - Charset default: utf8mb4 or first part of collation (if set)
// - Collation default: utf8mb4_unicode_ci or charset with `_unicode_ci` suffix
// - Comment optional comment on table
type Table struct {
Name string
columns columns
indexes keys
foreigns foreigns
Engine string
Charset string
Collation string
Comment string
}
// Column adds a column to the table
func (t *Table) Column(name string, c columnType) {
t.columns = append(t.columns, column{field: name, definition: c})
}
// ID adds bigint `id` column that is the primary key
func (t *Table) ID(name string) {
t.Column(name, Integer{
Prefix: "big",
Unsigned: true,
Autoincrement: true,
})
t.Primary(name)
}
// UniqueID adds unique id column (represented as UUID) that is the primary key
func (t *Table) UniqueID(name string) {
t.UUID(name, "(UUID())", false)
t.Primary(name)
}
// BinaryID adds unique binary id column (represented as UUID) that is the primary key
func (t *Table) BinaryID(name string) {
t.Column(name, Binary{Fixed: true, Precision: 16, Default: "(UUID_TO_BIN(UUID()))"})
t.Primary(name)
}
// Boolean represented in DB as tinyint
func (t *Table) Boolean(name string, def string) {
// tinyint(1)
t.Column(name, Integer{
Prefix: "tiny",
Unsigned: true,
Precision: 1,
Default: def,
})
}
// UUID adds char(36) column
func (t *Table) UUID(name string, def string, nullable bool) {
// char(36)
t.Column(name, String{
Fixed: true,
Precision: 36,
Default: def,
Nullable: nullable,
})
}
// Timestamps adds default timestamps: `created_at` and `updated_at`
func (t *Table) Timestamps() {
// created_at timestamp(6) not null default CURRENT_TIMESTAMP(6)
t.Column("created_at", Timable{
Type: "timestamp",
Precision: 6,
Default: "CURRENT_TIMESTAMP(6)",
})
// updated_at timestamp(6) not null default CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)
t.Column("updated_at", Timable{
Type: "timestamp",
Precision: 6,
Default: "CURRENT_TIMESTAMP(6)",
OnUpdate: "CURRENT_TIMESTAMP(6)",
})
}
// Int adds int(precision) column to the table
func (t *Table) Int(name string, precision uint16, unsigned bool) {
t.Column(name, Integer{Precision: precision, Unsigned: unsigned})
}
// BigInt adds bigint(precision) column to the table
func (t *Table) BigInt(name string, precision uint16, unsigned bool) {
t.Column(name, Integer{Prefix: "big", Precision: precision, Unsigned: unsigned})
}
// Float adds float(precision,scale) column to the table
func (t *Table) Float(name string, precision uint16, scale uint16) {
t.Column(name, Floatable{Precision: precision, Scale: scale})
}
// FixedFloat is an alias to decimal(precision,scale) column
func (t *Table) FixedFloat(name string, precision uint16, scale uint16) {
t.Decimal(name, precision, scale)
}
// Decimal adds decimal(precision,scale) column to the table
func (t *Table) Decimal(name string, precision uint16, scale uint16) {
t.Column(name, Floatable{Type: "decimal", Precision: precision, Scale: scale})
}
// Varchar adds varchar(precision) column to the table
func (t *Table) Varchar(name string, precision uint16) {
t.Column(name, String{Precision: precision})
}
// Char adds char(precision) column to the table
func (t *Table) Char(name string, precision uint16) {
t.Column(name, String{Fixed: true, Precision: precision})
}
// Text adds text column to the table
func (t *Table) Text(name string, nullable bool) {
t.Column(name, Text{Nullable: nullable})
}
// Blob adds blob column to the table
func (t *Table) Blob(name string, nullable bool) {
t.Column(name, Text{Blob: true, Nullable: nullable})
}
// JSON adds json column to the table
func (t *Table) JSON(name string) {
t.Column(name, JSON{})
}
// Timestamp adds timestamp column to the table
func (t *Table) Timestamp(name string, nullable bool, def string) {
t.Column(name, Timable{Nullable: nullable, Default: def})
}
// PreciseTimestamp adds timestamp column with precision to the table
func (t *Table) PreciseTimestamp(name string, precision uint16, nullable bool, def string) {
t.Column(name, Timable{Precision: precision, Nullable: nullable, Default: def})
}
// Date adds date column to the table
func (t *Table) Date(name string, nullable bool, def string) {
t.Column(name, Timable{Type: "date", Nullable: nullable, Default: def})
}
// Time adds time column to the table
func (t *Table) Time(name string, nullable bool, def string) {
t.Column(name, Timable{Type: "time", Nullable: nullable, Default: def})
}
// Year adds year column to the table
func (t *Table) Year(name string, nullable bool, def string) {
t.Column(name, Timable{Type: "year", Nullable: nullable, Default: def})
}
// Binary adds binary(precision) column to the table
func (t *Table) Binary(name string, precision uint16, nullable bool) {
t.Column(name, Binary{Fixed: true, Precision: precision, Nullable: nullable})
}
// Varbinary adds varbinary(precision) column to the table
func (t *Table) Varbinary(name string, precision uint16, nullable bool) {
t.Column(name, Binary{Precision: precision, Nullable: nullable})
}
// Primary adds primary key
func (t *Table) Primary(columns ...string) {
if len(columns) == 0 {
return
}
t.indexes = append(t.indexes, Key{
Type: "primary",
Columns: columns,
})
}
// Unique adds unique key on selected columns
func (t *Table) Unique(columns ...string) {
if len(columns) == 0 {
return
}
t.indexes = append(t.indexes, Key{
Name: BuildUniqueKeyNameOnTable(t.Name, columns...),
Type: "unique",
Columns: columns,
})
}
// Index adds index (key) on selected columns
func (t *Table) Index(name string, columns ...string) {
if len(columns) == 0 {
return
}
t.indexes = append(t.indexes, Key{Name: name, Columns: columns})
}
// Foreign adds foreign key constraints
func (t *Table) Foreign(column string, reference string, on string, onUpdate string, onDelete string) {
name := BuildForeignNameOnTable(t.Name, column)
t.indexes = append(t.indexes, Key{
Name: name,
Columns: []string{column},
})
t.foreigns = append(t.foreigns, Foreign{
Key: name,
Column: column,
Reference: reference,
On: on,
OnUpdate: onUpdate,
OnDelete: onDelete,
})
}