-
Notifications
You must be signed in to change notification settings - Fork 5
/
partition.go
188 lines (155 loc) · 3.94 KB
/
partition.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
package database
import (
"context"
"fmt"
"reflect"
"time"
"github.com/pkg/errors"
"github.com/uptrace/bun"
)
// PartitionBy -
type PartitionBy int
const (
PartitionByMonth PartitionBy = iota + 1
PartitionByYear
)
type params struct {
id string
start time.Time
end time.Time
success bool
}
// RangePartitionManager -
type RangePartitionManager struct {
conn Database
by PartitionBy
lastId string
}
// NewPartitionManager -
func NewPartitionManager(conn Database, by PartitionBy) RangePartitionManager {
return RangePartitionManager{
conn: conn,
by: by,
}
}
const createPartitionTemplate = `CREATE TABLE IF NOT EXISTS ? PARTITION OF ? FOR VALUES FROM (?) TO (?);`
func monthBoundaries(current time.Time) (time.Time, time.Time) {
start := time.Date(current.Year(), current.Month(), 1, 0, 0, 0, 0, time.UTC)
end := start.AddDate(0, 1, 0)
return start, end
}
func yearBoundaries(current time.Time) (time.Time, time.Time) {
start := time.Date(current.Year(), 1, 1, 0, 0, 0, 0, time.UTC)
end := start.AddDate(1, 0, 0)
return start, end
}
func monthPartitionId(currentTime time.Time) string {
return fmt.Sprintf("%d_%02d", currentTime.Year(), currentTime.Month())
}
func yearPartitionId(currentTime time.Time) string {
return fmt.Sprintf("%d", currentTime.Year())
}
func (pm *RangePartitionManager) getParameters(currentTime time.Time) (params, error) {
var p params
switch pm.by {
case PartitionByMonth:
p.id = monthPartitionId(currentTime)
case PartitionByYear:
p.id = yearPartitionId(currentTime)
default:
return p, errors.Errorf("unknown partition by: %d", pm.by)
}
p.success = p.id != pm.lastId
if !p.success {
return p, nil
}
switch pm.by {
case PartitionByMonth:
p.start, p.end = monthBoundaries(currentTime)
case PartitionByYear:
p.start, p.end = yearBoundaries(currentTime)
default:
return p, errors.Errorf("unknown partition by: %d", pm.by)
}
return p, nil
}
// CreatePartition -
func (pm *RangePartitionManager) CreatePartition(ctx context.Context, currentTime time.Time, tableName string) error {
p, err := pm.getParameters(currentTime)
if err != nil {
return err
}
if !p.success {
return nil
}
partitionName := fmt.Sprintf("%s_%s", tableName, p.id)
if _, err := pm.conn.Exec(
ctx,
createPartitionTemplate,
bun.Ident(partitionName),
bun.Ident(tableName),
p.start.Format(time.RFC3339Nano),
p.end.Format(time.RFC3339Nano),
); err != nil {
return err
}
pm.lastId = p.id
return nil
}
// CreatePartitions -
func (pm *RangePartitionManager) CreatePartitions(ctx context.Context, currentTime time.Time, tableNames ...string) error {
p, err := pm.getParameters(currentTime)
if err != nil {
return err
}
if !p.success {
return nil
}
for _, tableName := range tableNames {
partitionName := fmt.Sprintf("%s_%s", tableName, p.id)
if _, err := pm.conn.Exec(
ctx,
createPartitionTemplate,
bun.Ident(partitionName),
bun.Ident(tableName),
p.start.Format(time.RFC3339Nano),
p.end.Format(time.RFC3339Nano),
); err != nil {
return err
}
}
pm.lastId = p.id
return nil
}
// CreateTables - creates tables by passed models. If partition tag is present table will be partitioned
func CreateTables(ctx context.Context, conn Database, models ...any) error {
if len(models) == 0 {
return nil
}
for i := range models {
if models[i] == nil {
continue
}
modelType := reflect.TypeOf(models[i])
if reflect.ValueOf(models[i]).Kind() == reflect.Ptr {
modelType = modelType.Elem()
}
for j := 0; j < modelType.NumField(); j++ {
fieldType := modelType.Field(j)
if fieldType.Name != fieldTableName && fieldType.Name != fieldBaseModel {
continue
}
options := []CreateTableOption{
WithIfNotExists(),
}
partitionTag, ok := fieldType.Tag.Lookup("partition")
if ok {
options = append(options, WithPartitioning(partitionTag))
}
if err := conn.CreateTable(ctx, models[i], options...); err != nil {
return err
}
}
}
return nil
}