-
Notifications
You must be signed in to change notification settings - Fork 269
/
util.go
139 lines (121 loc) · 3.86 KB
/
util.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
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package orm
import (
"context"
"database/sql"
"strings"
"time"
"github.com/glebarez/sqlite"
"github.com/pingcap/failpoint"
dmutils "github.com/pingcap/tiflow/dm/pkg/conn"
metaModel "github.com/pingcap/tiflow/engine/pkg/meta/model"
ormModel "github.com/pingcap/tiflow/engine/pkg/orm/model"
"github.com/pingcap/tiflow/pkg/errors"
"github.com/pingcap/tiflow/pkg/logutil"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var defaultSlowLogThreshold = 200 * time.Millisecond
// IsNotFoundError checks whether the error is ErrMetaEntryNotFound
// TODO: refine me, need wrap error for api
func IsNotFoundError(err error) bool {
if err == nil {
return false
}
return strings.Contains(err.Error(), "ErrMetaEntryNotFound")
}
// InitAllFrameworkModels will create all framework-related tables in SQL backend
// NOT thread-safe.
// TODO: What happen if we upgrade the definition of model when rolling update?
// TODO: need test: change column definition/add column/drop column?
func InitAllFrameworkModels(ctx context.Context, cc metaModel.ClientConn) error {
gormDB, err := genGormDBFromClientConn(cc)
if err != nil {
return err
}
failpoint.InjectContext(ctx, "initializedDelay", nil)
if err := gormDB.WithContext(ctx).
AutoMigrate(globalModels...); err != nil {
return errors.ErrMetaOpFail.Wrap(err)
}
return nil
}
// InitEpochModel creates the backend logic epoch table if not exists
// Only use for business meta currently
// NOT thread-safe
func InitEpochModel(ctx context.Context, cc metaModel.ClientConn) error {
gormDB, err := genGormDBFromClientConn(cc)
if err != nil {
return err
}
if err := gormDB.WithContext(ctx).
AutoMigrate(&ormModel.LogicEpoch{}); err != nil {
return errors.ErrMetaOpFail.Wrap(err)
}
return nil
}
func genGormDBFromClientConn(cc metaModel.ClientConn) (*gorm.DB, error) {
if cc == nil {
return nil, errors.ErrMetaParamsInvalid.GenWithStackByArgs("input client conn is nil")
}
conn, err := cc.GetConn()
if err != nil {
return nil, err
}
// check if a sql.DB
db, ok := conn.(*sql.DB)
if !ok {
return nil, errors.ErrMetaParamsInvalid.GenWithStackByArgs("client conn is not sql DB")
}
gormDB, err := NewGormDB(db, cc.StoreType())
if err != nil {
return nil, err
}
return gormDB, nil
}
// NewGormDB news a gorm.DB
func NewGormDB(sqlDB *sql.DB, storeType metaModel.StoreType) (*gorm.DB, error) {
var dialector gorm.Dialector
if storeType == metaModel.StoreTypeMySQL {
dialector = mysql.New(mysql.Config{
Conn: sqlDB,
SkipInitializeWithVersion: false,
})
} else if storeType == metaModel.StoreTypeSQLite {
dialector = sqlite.Dialector{
Conn: sqlDB,
}
} else {
return nil, errors.ErrMetaParamsInvalid.GenWithStack("SQL subtype is not supported yet:%s", storeType)
}
db, err := gorm.Open(dialector, &gorm.Config{
SkipDefaultTransaction: true,
Logger: NewOrmLogger(logutil.WithComponent("gorm"),
WithSlowThreshold(defaultSlowLogThreshold),
WithIgnoreTraceRecordNotFoundErr()),
})
if err != nil {
return nil, errors.ErrMetaNewClientFail.Wrap(err)
}
return db, nil
}
// IsDuplicateEntryError checks whether error contains DuplicateEntry(MySQL) error
// or UNIQUE constraint failed(SQLite) error underlying.
func IsDuplicateEntryError(err error) bool {
if err == nil {
return false
}
return dmutils.IsErrDuplicateEntry(err) ||
strings.Contains(err.Error(), "UNIQUE constraint failed")
}