migrate/driver/mysql/mysql_test.go

107 lines
2.2 KiB
Go
Raw Normal View History

2014-09-16 19:44:34 +00:00
package mysql
2014-09-16 16:04:50 +00:00
import (
"database/sql"
2015-10-15 16:49:43 +00:00
"os"
"strings"
"testing"
2014-09-16 16:04:50 +00:00
"github.com/mattes/migrate/file"
"github.com/mattes/migrate/migrate/direction"
pipep "github.com/mattes/migrate/pipe"
)
// TestMigrate runs some additional tests on Migrate().
// Basic testing is already done in migrate/migrate_test.go
func TestMigrate(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
2015-10-15 16:49:43 +00:00
host := os.Getenv("MYSQL_PORT_3306_TCP_ADDR")
port := os.Getenv("MYSQL_PORT_3306_TCP_PORT")
driverUrl := "mysql://root@tcp(" + host + ":" + port + ")/migratetest"
2014-09-16 16:04:50 +00:00
// prepare clean database
2014-10-17 18:50:39 +00:00
connection, err := sql.Open("mysql", strings.SplitN(driverUrl, "mysql://", 2)[1])
2014-09-16 16:04:50 +00:00
if err != nil {
t.Fatal(err)
}
2014-09-16 19:44:34 +00:00
2014-10-10 23:09:40 +00:00
if _, err := connection.Exec(`DROP TABLE IF EXISTS yolo, yolo1, ` + tableName); err != nil {
2014-09-16 16:04:50 +00:00
t.Fatal(err)
}
d := &Driver{}
if err := d.Initialize(driverUrl); err != nil {
t.Fatal(err)
}
files := []file.File{
{
Path: "/foobar",
FileName: "001_foobar.up.sql",
Version: 1,
Name: "foobar",
Direction: direction.Up,
Content: []byte(`
CREATE TABLE yolo (
2014-09-16 19:44:34 +00:00
id int(11) not null primary key auto_increment
2014-09-16 16:04:50 +00:00
);
2014-09-16 19:44:34 +00:00
CREATE TABLE yolo1 (
2014-10-10 23:17:24 +00:00
id int(11) not null primary key auto_increment
2014-09-16 19:44:34 +00:00
);
2014-09-16 16:04:50 +00:00
`),
},
{
Path: "/foobar",
FileName: "002_foobar.down.sql",
Version: 1,
Name: "foobar",
Direction: direction.Down,
Content: []byte(`
DROP TABLE yolo;
`),
},
{
Path: "/foobar",
FileName: "002_foobar.up.sql",
Version: 2,
Name: "foobar",
Direction: direction.Up,
Content: []byte(`
2014-09-16 19:44:34 +00:00
// a comment
CREATE TABLE error (
2014-09-16 16:04:50 +00:00
id THIS WILL CAUSE AN ERROR
2014-09-16 19:44:34 +00:00
);
2014-09-16 16:04:50 +00:00
`),
},
}
pipe := pipep.New()
go d.Migrate(files[0], pipe)
errs := pipep.ReadErrors(pipe)
if len(errs) > 0 {
t.Fatal(errs)
}
pipe = pipep.New()
go d.Migrate(files[1], pipe)
errs = pipep.ReadErrors(pipe)
if len(errs) > 0 {
t.Fatal(errs)
}
pipe = pipep.New()
go d.Migrate(files[2], pipe)
errs = pipep.ReadErrors(pipe)
if len(errs) == 0 {
t.Error("Expected test case to fail")
}
if err := d.Close(); err != nil {
t.Fatal(err)
}
}