partition.go
8.8 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package database
import (
"fmt"
"github.com/jinzhu/now"
"github.com/zeromicro/go-zero/core/stores/redis"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/migrator"
"gorm.io/gorm/schema"
"strings"
"time"
)
var (
// PartitionByRangeTime 按unix时间戳分区
PartitionByRangeTime = 1
// PartitionByHash 按系统的hash值分区
PartitionByHash = 2
// PartitionByList 按List包含值分区
PartitionByList = 3
)
type PartitionTable interface {
TableName() string
}
type PartitionMigrator struct {
ServiceName string
DB *gorm.DB
Redis *redis.Redis
}
func NewPartitionMigrator(serviceName string, db *gorm.DB, redis *redis.Redis) *PartitionMigrator {
return &PartitionMigrator{
DB: db,
ServiceName: serviceName,
Redis: redis,
}
}
func (c *PartitionMigrator) AutoMigrate(t PartitionTable, option ...PartitionOptionFunc) error {
options := NewPartitionOptions()
for i := range option {
option[i](options)
}
tableName := t.TableName()
if !c.DB.Migrator().HasTable(tableName) {
migrator := Migrator{migrator.Migrator{
migrator.Config{
CreateIndexAfterCreateTable: true,
DB: c.DB,
Dialector: c.DB.Dialector,
},
}}
if err := migrator.CreatePartitionTable(options, t); err != nil {
panic(err)
}
}
rk := fmt.Sprintf("%s:auto-partition:%s", c.ServiceName, tableName)
lock := redis.NewRedisLock(c.Redis, rk)
ok, err := lock.Acquire()
if !ok || err != nil {
return nil
}
defer lock.Release()
switch options.Type {
case PartitionByRangeTime:
begin := options.TimeBegin
end := options.TimeEnd
for {
if begin.Unix() > end.Unix() {
break
}
pTable := fmt.Sprintf("%s_%s", tableName, options.FormatTimeSubFunc(begin))
sql := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s PARTITION OF %s FOR VALUES FROM (%d) TO (%d);",
pTable, tableName, begin.Unix(), begin.AddDate(0, options.TimeSpanMonth, 0).Unix())
tx := c.DB.Exec(sql)
if tx.Error != nil {
return tx.Error
}
c.log(t, pTable)
begin = begin.AddDate(0, options.TimeSpanMonth, 0)
}
break
case PartitionByHash:
for i := 0; i < options.Modulus; i++ {
pTable := fmt.Sprintf("%s_%d", tableName, i)
sql := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s PARTITION OF %s FOR VALUES WITH (MODULUS %d, REMAINDER %d);",
pTable, tableName, options.Modulus, i)
tx := c.DB.Exec(sql)
if tx.Error != nil {
return tx.Error
}
c.log(t, pTable)
}
break
case PartitionByList:
for i := 0; i < len(options.ListRange); i++ {
pTable := fmt.Sprintf("%s_%d", tableName, i)
sql := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s PARTITION OF %s FOR VALUES IN %s;",
pTable, tableName, InArgs(options.ListRange[i]))
tx := c.DB.Exec(sql)
if tx.Error != nil {
return tx.Error
}
c.log(t, pTable)
}
break
default:
return nil
}
return nil
}
func (c *PartitionMigrator) log(t PartitionTable, pTable string) {
fmt.Println("【自动分区】 create partition table", pTable, "on table", t.TableName())
}
type PartitionOptions struct {
// 分区类型 1:Hash 2:RangeTime
Type int
// 分区列
Column string
// Hash分区
Modulus int
// List 范围
ListRange []interface{}
// Range时间分区
TimeBegin time.Time
TimeEnd time.Time
TimeSpanMonth int
FormatTimeSubFunc func(time.Time) string
// 禁用PrimaryKey生成
// 分区字段有函数表达式的,需要禁用掉PrimaryKey,使用自定义的唯一ID生成规则
DisablePrimaryKey bool
}
func NewPartitionOptions() *PartitionOptions {
return &PartitionOptions{
Type: PartitionByRangeTime,
FormatTimeSubFunc: func(t time.Time) string {
return t.Format("200601")
},
}
}
func (c *PartitionOptions) Sql() string {
if c.Type == PartitionByHash {
return fmt.Sprintf("PARTITION BY HASH(%s)", c.Column)
}
if c.Type == PartitionByRangeTime {
return fmt.Sprintf("PARTITION BY RANGE(%s)", c.Column)
}
if c.Type == PartitionByList {
return fmt.Sprintf("PARTITION BY LIST(%s)", c.Column)
}
return ""
}
type PartitionOptionFunc func(*PartitionOptions)
func WithPartitionType(t int) PartitionOptionFunc {
return func(options *PartitionOptions) {
options.Type = t
}
}
func WithPartitionColumn(c string) PartitionOptionFunc {
return func(options *PartitionOptions) {
options.Column = c
}
}
func WithPartitionHash(modulus int) PartitionOptionFunc {
return func(options *PartitionOptions) {
options.Modulus = modulus
}
}
func WithPartitionRangeTime(begin, end time.Time, spanMonth int) PartitionOptionFunc {
return func(options *PartitionOptions) {
options.TimeBegin = begin
options.TimeEnd = end
options.TimeSpanMonth = spanMonth
}
}
func WithPartitionList(list ...interface{}) PartitionOptionFunc {
return func(options *PartitionOptions) {
options.ListRange = list
}
}
func WithDisablePrimaryKey(disablePrimaryKey bool) PartitionOptionFunc {
return func(options *PartitionOptions) {
options.DisablePrimaryKey = disablePrimaryKey
}
}
func Date(date string) time.Time {
return now.MustParse(date)
}
type Migrator struct {
migrator.Migrator
}
// CreatePartitionTable create table in database for values
func (m Migrator) CreatePartitionTable(options *PartitionOptions, values ...interface{}) error {
for _, value := range m.ReorderModels(values, false) {
tx := m.DB.Session(&gorm.Session{})
if err := m.RunWithValue(value, func(stmt *gorm.Statement) (errr error) {
var (
createTableSQL = "CREATE TABLE ? ("
values = []interface{}{m.CurrentTable(stmt)}
hasPrimaryKeyInDataType bool
)
for _, dbName := range stmt.Schema.DBNames {
field := stmt.Schema.FieldsByDBName[dbName]
if !field.IgnoreMigration {
createTableSQL += "? ?"
hasPrimaryKeyInDataType = hasPrimaryKeyInDataType || strings.Contains(strings.ToUpper(string(field.DataType)), "PRIMARY KEY")
values = append(values, clause.Column{Name: dbName}, m.DB.Migrator().FullDataTypeOf(field))
createTableSQL += ","
}
}
if !hasPrimaryKeyInDataType && len(stmt.Schema.PrimaryFields) > 0 && !options.DisablePrimaryKey {
createTableSQL += "PRIMARY KEY ?,"
primaryKeys := []interface{}{}
for _, field := range stmt.Schema.PrimaryFields {
primaryKeys = append(primaryKeys, clause.Column{Name: field.DBName})
}
values = append(values, primaryKeys)
}
for _, idx := range stmt.Schema.ParseIndexes() {
if m.CreateIndexAfterCreateTable {
defer func(value interface{}, name string) {
if errr == nil {
errr = tx.Migrator().CreateIndex(value, name)
}
}(value, idx.Name)
} else {
if idx.Class != "" {
createTableSQL += idx.Class + " "
}
createTableSQL += "INDEX ? ?"
if idx.Comment != "" {
createTableSQL += fmt.Sprintf(" COMMENT '%s'", idx.Comment)
}
if idx.Option != "" {
createTableSQL += " " + idx.Option
}
createTableSQL += ","
values = append(values, clause.Column{Name: idx.Name}, tx.Migrator().(migrator.BuildIndexOptionsInterface).BuildIndexOptions(idx.Fields, stmt))
}
}
if !m.DB.DisableForeignKeyConstraintWhenMigrating && !m.DB.IgnoreRelationshipsWhenMigrating {
for _, rel := range stmt.Schema.Relationships.Relations {
if rel.Field.IgnoreMigration {
continue
}
if constraint := rel.ParseConstraint(); constraint != nil {
if constraint.Schema == stmt.Schema {
sql, vars := buildConstraint(constraint)
createTableSQL += sql + ","
values = append(values, vars...)
}
}
}
}
for _, chk := range stmt.Schema.ParseCheckConstraints() {
createTableSQL += "CONSTRAINT ? CHECK (?),"
values = append(values, clause.Column{Name: chk.Name}, clause.Expr{SQL: chk.Constraint})
}
createTableSQL = strings.TrimSuffix(createTableSQL, ",")
createTableSQL += ")"
if options != nil {
createTableSQL += options.Sql()
}
if tableOption, ok := m.DB.Get("gorm:table_options"); ok {
createTableSQL += fmt.Sprint(tableOption)
}
errr = tx.Exec(createTableSQL, values...).Error
return errr
}); err != nil {
return err
}
}
return nil
}
func buildConstraint(constraint *schema.Constraint) (sql string, results []interface{}) {
sql = "CONSTRAINT ? FOREIGN KEY ? REFERENCES ??"
if constraint.OnDelete != "" {
sql += " ON DELETE " + constraint.OnDelete
}
if constraint.OnUpdate != "" {
sql += " ON UPDATE " + constraint.OnUpdate
}
var foreignKeys, references []interface{}
for _, field := range constraint.ForeignKeys {
foreignKeys = append(foreignKeys, clause.Column{Name: field.DBName})
}
for _, field := range constraint.References {
references = append(references, clause.Column{Name: field.DBName})
}
results = append(results, clause.Table{Name: constraint.Name}, foreignKeys, clause.Table{Name: constraint.ReferenceSchema.Table}, references)
return
}