github.com/postmates/migrate@v3.0.2-0.20200730201548-1a6ead3e680d+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/mattes/migrate/database/testing"
    13  	mt "github.com/mattes/migrate/testing"
    14  )
    15  
    16  var versions = []mt.Version{
    17  	{"mysql:8", []string{"MYSQL_ROOT_PASSWORD=root", "MYSQL_DATABASE=public"}},
    18  	{"mysql:5.7", []string{"MYSQL_ROOT_PASSWORD=root", "MYSQL_DATABASE=public"}},
    19  	{"mysql:5.6", []string{"MYSQL_ROOT_PASSWORD=root", "MYSQL_DATABASE=public"}},
    20  	{"mysql:5.5", []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  	err = db.Ping()
    30  
    31  	if err == sqldriver.ErrBadConn {
    32  		return false
    33  	}
    34  
    35  	return true
    36  }
    37  
    38  func Test(t *testing.T) {
    39  	// mysql.SetLogger(mysql.Logger(log.New(ioutil.Discard, "", log.Ltime)))
    40  
    41  	mt.ParallelTest(t, versions, isReady,
    42  		func(t *testing.T, i mt.Instance) {
    43  			p := &Mysql{}
    44  			addr := fmt.Sprintf("mysql://root:root@tcp(%v:%v)/public", i.Host(), i.Port())
    45  			d, err := p.Open(addr)
    46  			if err != nil {
    47  				t.Fatalf("%v", err)
    48  			}
    49  			dt.Test(t, d, []byte("SELECT 1"))
    50  
    51  			// check ensureVersionTable
    52  			if err := d.(*Mysql).ensureVersionTable(); err != nil {
    53  				t.Fatal(err)
    54  			}
    55  			// check again
    56  			if err := d.(*Mysql).ensureVersionTable(); err != nil {
    57  				t.Fatal(err)
    58  			}
    59  		})
    60  }