-
Notifications
You must be signed in to change notification settings - Fork 141
/
client.go
111 lines (96 loc) · 2.92 KB
/
client.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
// Copyright 2021 The PipeCD Authors.
//
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ensurer
import (
"context"
"database/sql"
_ "embed"
"fmt"
"strings"
driver "github.com/go-sql-driver/mysql"
"go.uber.org/zap"
"github.com/pipe-cd/pipecd/pkg/datastore/mysql"
)
var (
//go:embed schema.sql
mysqlDatabaseSchema string
//go:embed indexes.sql
mysqlDatabaseIndexes string
)
const (
mysqlErrorCodeDuplicateColumnName = 1060
mysqlErrorCodeDuplicateKeyName = 1061
mysqlErrorCodeColumnOrKeyNotExists = 1091
)
type mysqlEnsurer struct {
client *sql.DB
logger *zap.Logger
}
func NewMySQLEnsurer(url, database, usernameFile, passwordFile string, logger *zap.Logger) (SQLEnsurer, error) {
dataSourceName, err := mysql.BuildDataSourceName(url, database, usernameFile, passwordFile)
if err != nil {
return nil, fmt.Errorf("failed to connect to sql database: %w", err)
}
// Enable run multi statements at once.
db, err := sql.Open("mysql", fmt.Sprintf("%s?multiStatements=true", dataSourceName))
if err != nil {
return nil, fmt.Errorf("failed to connect to sql database: %w", err)
}
return &mysqlEnsurer{
client: db,
logger: logger.Named("mysql-ensurer"),
}, nil
}
func (m *mysqlEnsurer) EnsureIndexes(ctx context.Context) error {
for _, stmt := range makeCreateIndexStatements(mysqlDatabaseIndexes) {
_, err := m.client.ExecContext(ctx, stmt)
// Ignore in case error inc case:
// - Duplicate key name or column name occurred.
// - Try to remove not exists column or key.
if mysqlErr, ok := err.(*driver.MySQLError); ok && (mysqlErr.Number == mysqlErrorCodeDuplicateKeyName ||
mysqlErr.Number == mysqlErrorCodeDuplicateColumnName ||
mysqlErr.Number == mysqlErrorCodeColumnOrKeyNotExists) {
continue
}
if err != nil {
return err
}
}
return nil
}
func (m *mysqlEnsurer) EnsureSchema(ctx context.Context) error {
_, err := m.client.ExecContext(ctx, mysqlDatabaseSchema)
if err != nil {
return err
}
return nil
}
func (m *mysqlEnsurer) Close() error {
return m.client.Close()
}
func (m *mysqlEnsurer) Ping() error {
return m.client.Ping()
}
func makeCreateIndexStatements(indexesStatements string) []string {
items := strings.Split(strings.TrimSpace(indexesStatements), ";")
statements := make([]string, 0, len(items))
for _, item := range items {
// Ignore dummy statement.
if item == "" {
continue
}
statements = append(statements, strings.TrimSpace(item))
}
return statements
}