Skip to content

Commit 1593164

Browse files
authored
Mysql - Fix crash with ansi_quotes sql_mode enabled (#476)
* Fix crash with ansi_quotes * Run lint
1 parent fedbe42 commit 1593164

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

database/mysql/mysql.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ func (m *Mysql) ensureVersionTable() (err error) {
449449

450450
// check if migration table exists
451451
var result string
452-
query := `SHOW TABLES LIKE "` + m.config.MigrationsTable + `"`
452+
query := `SHOW TABLES LIKE '` + m.config.MigrationsTable + `'`
453453
if err := m.conn.QueryRowContext(context.Background(), query).Scan(&result); err != nil {
454454
if err != sql.ErrNoRows {
455455
return &database.Error{OrigErr: err, Query: []byte(query)}

database/mysql/mysql_test.go

+49
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,24 @@ var (
3939
Env: map[string]string{"MYSQL_ROOT_PASSWORD": "root", "MYSQL_DATABASE": "public"},
4040
PortRequired: true, ReadyFunc: isReady,
4141
}
42+
optsAnsiQuotes = dktest.Options{
43+
Env: map[string]string{"MYSQL_ROOT_PASSWORD": "root", "MYSQL_DATABASE": "public"},
44+
PortRequired: true, ReadyFunc: isReady,
45+
Cmd: []string{"--sql-mode=ANSI_QUOTES"},
46+
}
4247
// Supported versions: https://www.mysql.com/support/supportedplatforms/database.html
4348
specs = []dktesting.ContainerSpec{
4449
{ImageName: "mysql:5.5", Options: opts},
4550
{ImageName: "mysql:5.6", Options: opts},
4651
{ImageName: "mysql:5.7", Options: opts},
4752
{ImageName: "mysql:8", Options: opts},
4853
}
54+
specsAnsiQuotes = []dktesting.ContainerSpec{
55+
{ImageName: "mysql:5.5", Options: optsAnsiQuotes},
56+
{ImageName: "mysql:5.6", Options: optsAnsiQuotes},
57+
{ImageName: "mysql:5.7", Options: optsAnsiQuotes},
58+
{ImageName: "mysql:8", Options: optsAnsiQuotes},
59+
}
4960
)
5061

5162
func isReady(ctx context.Context, c dktest.ContainerInfo) bool {
@@ -147,6 +158,44 @@ func TestMigrate(t *testing.T) {
147158
})
148159
}
149160

161+
func TestMigrateAnsiQuotes(t *testing.T) {
162+
// mysql.SetLogger(mysql.Logger(log.New(ioutil.Discard, "", log.Ltime)))
163+
164+
dktesting.ParallelTest(t, specsAnsiQuotes, func(t *testing.T, c dktest.ContainerInfo) {
165+
ip, port, err := c.Port(defaultPort)
166+
if err != nil {
167+
t.Fatal(err)
168+
}
169+
170+
addr := fmt.Sprintf("mysql://root:root@tcp(%v:%v)/public", ip, port)
171+
p := &Mysql{}
172+
d, err := p.Open(addr)
173+
if err != nil {
174+
t.Fatal(err)
175+
}
176+
defer func() {
177+
if err := d.Close(); err != nil {
178+
t.Error(err)
179+
}
180+
}()
181+
182+
m, err := migrate.NewWithDatabaseInstance("file://./examples/migrations", "public", d)
183+
if err != nil {
184+
t.Fatal(err)
185+
}
186+
dt.TestMigrate(t, m)
187+
188+
// check ensureVersionTable
189+
if err := d.(*Mysql).ensureVersionTable(); err != nil {
190+
t.Fatal(err)
191+
}
192+
// check again
193+
if err := d.(*Mysql).ensureVersionTable(); err != nil {
194+
t.Fatal(err)
195+
}
196+
})
197+
}
198+
150199
func TestLockWorks(t *testing.T) {
151200
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
152201
ip, port, err := c.Port(defaultPort)

0 commit comments

Comments
 (0)