forked from longbridge/gorm-sharding
-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
dialector.go
134 lines (111 loc) · 2.88 KB
/
dialector.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
package sharding
import (
"fmt"
"gorm.io/gorm/migrator"
"gorm.io/gorm/schema"
"gorm.io/gorm"
)
type ShardingDialector struct {
gorm.Dialector
sharding *Sharding
}
type ShardingMigrator struct {
gorm.Migrator
sharding *Sharding
dialector gorm.Dialector
}
func NewShardingDialector(d gorm.Dialector, s *Sharding) ShardingDialector {
return ShardingDialector{
Dialector: d,
sharding: s,
}
}
func (d ShardingDialector) Migrator(db *gorm.DB) gorm.Migrator {
m := d.Dialector.Migrator(db)
return ShardingMigrator{
Migrator: m,
sharding: d.sharding,
dialector: d.Dialector,
}
}
func (m ShardingMigrator) AutoMigrate(dst ...any) error {
shardingDsts, noShardingDsts, err := m.splitShardingDsts(dst...)
if err != nil {
return err
}
stmt := &gorm.Statement{DB: m.sharding.DB}
for _, sd := range shardingDsts {
tx := stmt.DB.Session(&gorm.Session{}).Table(sd.table)
if err := m.dialector.Migrator(tx).AutoMigrate(sd.dst); err != nil {
return err
}
}
if len(noShardingDsts) > 0 {
tx := stmt.DB.Session(&gorm.Session{})
tx.Statement.Settings.Store(ShardingIgnoreStoreKey, nil)
defer tx.Statement.Settings.Delete(ShardingIgnoreStoreKey)
if err := m.dialector.Migrator(tx).AutoMigrate(noShardingDsts...); err != nil {
return err
}
}
return nil
}
// BuildIndexOptions build index options
func (m ShardingMigrator) BuildIndexOptions(opts []schema.IndexOption, stmt *gorm.Statement) (results []interface{}) {
return m.Migrator.(migrator.BuildIndexOptionsInterface).BuildIndexOptions(opts, stmt)
}
func (m ShardingMigrator) DropTable(dst ...any) error {
shardingDsts, noShardingDsts, err := m.splitShardingDsts(dst...)
if err != nil {
return err
}
for _, sd := range shardingDsts {
if err := m.Migrator.DropTable(sd.table); err != nil {
return err
}
}
if len(noShardingDsts) > 0 {
if err := m.Migrator.DropTable(noShardingDsts...); err != nil {
return err
}
}
return nil
}
type shardingDst struct {
table string
dst any
}
// splite sharding or normal dsts
func (m ShardingMigrator) splitShardingDsts(dsts ...any) (shardingDsts []shardingDst,
noShardingDsts []any, err error) {
shardingDsts = make([]shardingDst, 0)
noShardingDsts = make([]any, 0)
for _, model := range dsts {
stmt := &gorm.Statement{DB: m.sharding.DB}
err = stmt.Parse(model)
if err != nil {
return
}
if cfg, ok := m.sharding.configs[stmt.Table]; ok {
// support sharding table
suffixs := cfg.ShardingSuffixs()
if len(suffixs) == 0 {
err = fmt.Errorf("sharding table:%s suffixs is empty", stmt.Table)
return
}
for _, suffix := range suffixs {
shardingTable := stmt.Table + suffix
shardingDsts = append(shardingDsts, shardingDst{
table: shardingTable,
dst: model,
})
}
if cfg.DoubleWrite {
noShardingDsts = append(noShardingDsts, model)
}
} else {
noShardingDsts = append(noShardingDsts, model)
}
}
return
}