github.com/matcornic/migrate@v3.3.2-0.20180717234201-feea45c20506+incompatible/database/mysql/mysql_test.go (about) 1 package mysql 2 3 import ( 4 "database/sql" 5 sqldriver "database/sql/driver" 6 "fmt" 7 // "io/ioutil" 8 // "log" 9 "testing" 10 11 "github.com/go-sql-driver/mysql" 12 dt "github.com/golang-migrate/migrate/database/testing" 13 mt "github.com/golang-migrate/migrate/testing" 14 ) 15 16 var versions = []mt.Version{ 17 {Image: "mysql:8", ENV: []string{"MYSQL_ROOT_PASSWORD=root", "MYSQL_DATABASE=public"}}, 18 {Image: "mysql:5.7", ENV: []string{"MYSQL_ROOT_PASSWORD=root", "MYSQL_DATABASE=public"}}, 19 {Image: "mysql:5.6", ENV: []string{"MYSQL_ROOT_PASSWORD=root", "MYSQL_DATABASE=public"}}, 20 {Image: "mysql:5.5", ENV: []string{"MYSQL_ROOT_PASSWORD=root", "MYSQL_DATABASE=public"}}, 21 } 22 23 func isReady(i mt.Instance) bool { 24 db, err := sql.Open("mysql", fmt.Sprintf("root:root@tcp(%v:%v)/public", i.Host(), i.Port())) 25 if err != nil { 26 return false 27 } 28 defer db.Close() 29 if err = db.Ping(); err != nil { 30 switch err { 31 case sqldriver.ErrBadConn, mysql.ErrInvalidConn: 32 return false 33 default: 34 fmt.Println(err) 35 } 36 return false 37 } 38 39 return true 40 } 41 42 func Test(t *testing.T) { 43 // mysql.SetLogger(mysql.Logger(log.New(ioutil.Discard, "", log.Ltime))) 44 45 mt.ParallelTest(t, versions, isReady, 46 func(t *testing.T, i mt.Instance) { 47 p := &Mysql{} 48 addr := fmt.Sprintf("mysql://root:root@tcp(%v:%v)/public", i.Host(), i.Port()) 49 d, err := p.Open(addr) 50 if err != nil { 51 t.Fatalf("%v", err) 52 } 53 defer d.Close() 54 dt.Test(t, d, []byte("SELECT 1")) 55 56 // check ensureVersionTable 57 if err := d.(*Mysql).ensureVersionTable(); err != nil { 58 t.Fatal(err) 59 } 60 // check again 61 if err := d.(*Mysql).ensureVersionTable(); err != nil { 62 t.Fatal(err) 63 } 64 }) 65 } 66 67 func TestLockWorks(t *testing.T) { 68 mt.ParallelTest(t, versions, isReady, 69 func(t *testing.T, i mt.Instance) { 70 p := &Mysql{} 71 addr := fmt.Sprintf("mysql://root:root@tcp(%v:%v)/public", i.Host(), i.Port()) 72 d, err := p.Open(addr) 73 if err != nil { 74 t.Fatalf("%v", err) 75 } 76 dt.Test(t, d, []byte("SELECT 1")) 77 78 ms := d.(*Mysql) 79 80 err = ms.Lock() 81 if err != nil { 82 t.Fatal(err) 83 } 84 err = ms.Unlock() 85 if err != nil { 86 t.Fatal(err) 87 } 88 89 // make sure the 2nd lock works (RELEASE_LOCK is very finicky) 90 err = ms.Lock() 91 if err != nil { 92 t.Fatal(err) 93 } 94 err = ms.Unlock() 95 if err != nil { 96 t.Fatal(err) 97 } 98 }) 99 }