fix mysql tests

This commit is contained in:
mattes 2014-10-11 01:09:40 +02:00
parent ff8020a6a6
commit 24dff377cc
2 changed files with 46 additions and 49 deletions

View File

@ -94,6 +94,7 @@ func (driver *Driver) Migrate(f file.File, pipe chan interface{}) {
sqlStmts := bytes.Split(f.Content, []byte(";")) sqlStmts := bytes.Split(f.Content, []byte(";"))
for _, sqlStmt := range sqlStmts { for _, sqlStmt := range sqlStmts {
if len(bytes.TrimSpace(sqlStmt)) > 0 {
if _, err := tx.Exec(string(sqlStmt)); err != nil { if _, err := tx.Exec(string(sqlStmt)); err != nil {
mysqlErr := err.(*mysql.MySQLError) mysqlErr := err.(*mysql.MySQLError)
@ -113,8 +114,9 @@ func (driver *Driver) Migrate(f file.File, pipe chan interface{}) {
if err == nil { if err == nil {
// get white-space offset // get white-space offset
// TODO this is broken, because we use sqlStmt instead of f.Content
wsLineOffset := 0 wsLineOffset := 0
b := bufio.NewReader(bytes.NewBuffer(f.Content)) b := bufio.NewReader(bytes.NewBuffer(sqlStmt))
for { for {
line, _, err := b.ReadLine() line, _, err := b.ReadLine()
if err != nil { if err != nil {
@ -130,7 +132,7 @@ func (driver *Driver) Migrate(f file.File, pipe chan interface{}) {
message := mysqlErr.Error() message := mysqlErr.Error()
message = re.ReplaceAllString(message, fmt.Sprintf("at line %v", lineNo+wsLineOffset)) message = re.ReplaceAllString(message, fmt.Sprintf("at line %v", lineNo+wsLineOffset))
errorPart := file.LinesBeforeAndAfter(f.Content, lineNo, 5, 5, true) errorPart := file.LinesBeforeAndAfter(sqlStmt, lineNo, 5, 5, true)
pipe <- errors.New(fmt.Sprintf("%s\n\n%s", message, string(errorPart))) pipe <- errors.New(fmt.Sprintf("%s\n\n%s", message, string(errorPart)))
} else { } else {
pipe <- errors.New(mysqlErr.Error()) pipe <- errors.New(mysqlErr.Error())
@ -142,6 +144,7 @@ func (driver *Driver) Migrate(f file.File, pipe chan interface{}) {
return return
} }
} }
}
if err := tx.Commit(); err != nil { if err := tx.Commit(); err != nil {
pipe <- err pipe <- err

View File

@ -19,10 +19,7 @@ func TestMigrate(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if _, err := connection.Exec(` if _, err := connection.Exec(`DROP TABLE IF EXISTS yolo, yolo1, ` + tableName); err != nil {
DROP TABLE IF EXISTS yolo;
DROP TABLE IF EXISTS yolo1;
DROP TABLE IF EXISTS ` + tableName + `;`); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -44,7 +41,7 @@ func TestMigrate(t *testing.T) {
); );
CREATE TABLE yolo1 ( CREATE TABLE yolo1 (
id int(11) not null primary key auto_increment id int(11) not nfull primary key auto_increment
); );
`), `),
}, },
@ -95,9 +92,6 @@ func TestMigrate(t *testing.T) {
t.Error("Expected test case to fail") t.Error("Expected test case to fail")
} }
// TODO remove after debugging
t.Error(errs)
if err := d.Close(); err != nil { if err := d.Close(); err != nil {
t.Fatal(err) t.Fatal(err)
} }