-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmysql.go
More file actions
136 lines (115 loc) · 3.35 KB
/
mysql.go
File metadata and controls
136 lines (115 loc) · 3.35 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
// Copyright (c) 2016 Betalo AB
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package main
import (
"database/sql"
"errors"
"fmt"
"net/url"
"strings"
"context"
_ "github.com/go-sql-driver/mysql" // Register MySQL driver
)
type mysqlResource struct {
url.URL
}
func (r *mysqlResource) Await(ctx context.Context) error {
tags := parseTags(r.URL.Fragment)
database := strings.TrimPrefix(r.URL.Path, "/")
if strings.Contains(database, "/") {
return fmt.Errorf("invalid database name: %s", database)
}
if database == "" {
if _, ok := tags["tables"]; ok {
return fmt.Errorf("database name required for awaiting tables")
}
// Special database default which usually exists.
database = "information_schema"
}
dsnURL := r.URL
dsnURL.Fragment = ""
dsnURL.Path = database
dsnURL.Host = "tcp(" + dsnURL.Host + ")"
dsn := dsnURL.String()
// Comply to Go's MySQL driver DSN convention
dsn = strings.TrimPrefix(dsn, "mysql://")
db, err := sql.Open(dsnURL.Scheme, dsn)
if err != nil {
return err
}
defer db.Close()
if err := db.Ping(); err != nil {
return &unavailableError{err}
}
if val, ok := tags["tables"]; ok {
var tables []string
if val != "" {
tables = strings.Split(val, ",")
}
if err := awaitMySQLTables(db, database, tables); err != nil {
return err
}
}
return nil
}
func awaitMySQLTables(db *sql.DB, dbName string, tables []string) error {
if len(tables) == 0 {
const stmt = `SELECT count(*) FROM information_schema.tables WHERE table_schema=?`
var tableCnt int
if err := db.QueryRow(stmt, dbName).Scan(&tableCnt); err != nil {
return err
}
if tableCnt == 0 {
return &unavailableError{errors.New("no tables found")}
}
return nil
}
const stmt = `SELECT table_name FROM information_schema.tables WHERE table_schema=?`
rows, err := db.Query(stmt, dbName)
if err != nil {
return err
}
defer rows.Close()
var actualTables []string
for rows.Next() {
var t string
if err := rows.Scan(&t); err != nil {
return err
}
actualTables = append(actualTables, t)
}
if err := rows.Err(); err != nil {
return err
}
contains := func(l []string, s string) bool {
for _, i := range l {
if i == s {
return true
}
}
return false
}
for _, t := range tables {
if !contains(actualTables, t) {
return &unavailableError{fmt.Errorf("table not found: %s", t)}
}
}
return nil
}