forked from hellofresh/klepto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_test.go
193 lines (150 loc) · 5.21 KB
/
mysql_test.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
189
190
191
192
193
package features
import (
"database/sql"
"fmt"
"io/ioutil"
"os"
"path"
"testing"
"time"
"github.com/go-sql-driver/mysql"
"github.com/hellofresh/klepto/pkg/config"
"github.com/hellofresh/klepto/pkg/dumper"
_ "github.com/hellofresh/klepto/pkg/dumper/mysql"
"github.com/hellofresh/klepto/pkg/reader"
_ "github.com/hellofresh/klepto/pkg/reader/mysql"
"github.com/stretchr/testify/suite"
)
type MysqlTestSuite struct {
suite.Suite
rootDSN string
rootConnection *sql.DB
databases []string
timeout time.Duration
}
func TestMysqlTestSuite(t *testing.T) {
s := &MysqlTestSuite{timeout: time.Second * 3}
suite.Run(t, s)
}
func (s *MysqlTestSuite) TestExample() {
readDSN := s.createDatabase("simple")
dumpDSN := s.createDatabase("simple_dump")
s.loadFixture(readDSN, "mysql_simple.sql")
rdr, err := reader.Connect(reader.ConnOpts{DSN: readDSN, Timeout: s.timeout})
s.Require().NoError(err, "Unable to create reader")
defer rdr.Close()
dmp, err := dumper.NewDumper(dumper.ConnOpts{DSN: dumpDSN}, rdr)
s.Require().NoError(err, "Unable to create dumper")
defer dmp.Close()
done := make(chan struct{})
defer close(done)
s.Require().NoError(dmp.Dump(done, new(config.Spec), 4), "Failed to dump")
<-done
s.assertDatabaseAreTheSame(readDSN, dumpDSN)
}
func (s *MysqlTestSuite) SetupSuite() {
rootDSN, ok := os.LookupEnv("TEST_MYSQL")
if !ok {
s.T().Skip("TEST_MYSQL env is not defined")
}
rootCfg, err := mysql.ParseDSN(rootDSN)
s.Require().NoError(err, "TEST_MYSQL failed to parse")
rootCfg.MultiStatements = true
s.rootDSN = rootCfg.FormatDSN()
s.rootConnection, err = sql.Open("mysql", rootDSN)
s.Require().NoError(err, "Failed to connect to mysql")
s.Require().NoError(s.rootConnection.Ping(), "Failed to ping mysql")
}
func (s *MysqlTestSuite) TearDownSuite() {
for _, db := range s.databases {
s.dropDatabase(db)
}
s.rootConnection.Close()
}
func (s *MysqlTestSuite) createDatabase(name string) string {
s.databases = append(s.databases, name)
s.dropDatabase(name)
_, err := s.rootConnection.Exec(fmt.Sprintf("CREATE DATABASE %s", name))
s.Require().NoError(err, "Unable to create db")
dbUrl, _ := mysql.ParseDSN(s.rootDSN)
dbUrl.DBName = name
_, err = s.rootConnection.Exec(fmt.Sprintf("GRANT ALL PRIVILEGES ON %s.* TO '%s'@'%%'", name, dbUrl.User))
s.Require().NoError(err, "Unable to grant db permissions")
return dbUrl.FormatDSN()
}
func (s *MysqlTestSuite) dropDatabase(name string) {
_, err := s.rootConnection.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", name))
s.NoError(err, "Unable to drop db")
}
func (s *MysqlTestSuite) loadFixture(dsn string, file string) {
data, err := ioutil.ReadFile(path.Join("../fixtures/", file))
s.Require().NoError(err, "Unable to load fixture file")
conn, err := sql.Open("mysql", dsn)
defer conn.Close()
s.Require().NoError(err, "Unable to open db connection to load fixture")
_, err = conn.Exec(string(data))
s.Require().NoError(err, "Unable to execute fixture")
}
func (s *MysqlTestSuite) assertDatabaseAreTheSame(expectedDSN string, dumpDSN string) {
sourceConn, err := sql.Open("mysql", expectedDSN)
s.Require().NoError(err, "Unable to connect to source db")
defer sourceConn.Close()
targetConn, err := sql.Open("mysql", dumpDSN)
s.Require().NoError(err, "Unable to connect to target db")
defer targetConn.Close()
tables := s.fetchTableRowCount(sourceConn)
s.Require().Equal(tables, s.fetchTableRowCount(targetConn))
for _, table := range tables {
s.compareTable(sourceConn, targetConn, table.name, table.columnCount)
}
}
func (s *MysqlTestSuite) fetchTableRowCount(db *sql.DB) []tableInfo {
tableRows, err := db.Query(
`SELECT
t.TABLE_NAME AS name,
t.TABLE_ROWS AS count,
COUNT(c.COLUMN_NAME) AS columnCount
FROM information_schema.TABLES AS t
LEFT JOIN information_schema.COLUMNS AS c ON
c.TABLE_SCHEMA = t.TABLE_SCHEMA AND
c.TABLE_NAME = t.TABLE_NAME
WHERE t.TABLE_SCHEMA = DATABASE()
GROUP BY t.TABLE_NAME`,
)
s.Require().NoError(err, "Unable to fetch table info")
defer tableRows.Close()
tables := []tableInfo{}
for tableRows.Next() {
table := tableInfo{}
s.Require().NoError(
tableRows.Scan(&table.name, &table.count, &table.columnCount),
"Unable to fetch table info row",
)
tables = append(tables, table)
}
return tables
}
func (s *MysqlTestSuite) compareTable(source *sql.DB, target *sql.DB, table string, columnCount int) {
assert := s.Require()
query := fmt.Sprintf("SELECT * FROM %s", table)
expectedRows, err := source.Query(query)
assert.NoError(err, "Unable to query source table")
defer expectedRows.Close()
rows, err := target.Query(query)
assert.NoError(err, "Unable to query target table")
defer rows.Close()
for expectedRows.Next() {
assert.True(rows.Next(), "target row mismatch")
expectedFields := make([]interface{}, columnCount)
targetFields := make([]interface{}, columnCount)
for i := 0; i < columnCount; i++ {
var sourceValue interface{}
expectedFields[i] = &sourceValue
var targetValue interface{}
targetFields[i] = &targetValue
}
assert.NoError(expectedRows.Scan(expectedFields...), "failed to fetch expected rows")
assert.NoError(rows.Scan(targetFields...), "failed to fetch target rows")
assert.Equal(expectedFields, targetFields)
}
}