split sql statements by ;

This commit is contained in:
mattes 2014-10-11 00:49:40 +02:00
parent 2faaebc76a
commit cc23e29eef
1 changed files with 48 additions and 46 deletions

View File

@ -7,15 +7,11 @@ import (
"database/sql" "database/sql"
"errors" "errors"
"fmt" "fmt"
"github.com/go-sql-driver/mysql"
"github.com/mattes/migrate/file" "github.com/mattes/migrate/file"
"github.com/mattes/migrate/migrate/direction" "github.com/mattes/migrate/migrate/direction"
"regexp" "regexp"
"strconv" "strconv"
// Use fork instead of github.com/go-sql-driver/mysql, because it
// has clientMultiStatements enabled.
// see https://github.com/go-sql-driver/mysql/issues/66
"github.com/mattes/mysql"
) )
type Driver struct { type Driver struct {
@ -93,7 +89,12 @@ func (driver *Driver) Migrate(f file.File, pipe chan interface{}) {
return return
} }
if _, err := tx.Exec(string(f.Content)); err != nil { // TODO this is not good! unfortunately there is no mysql driver that
// supports multiple statements per query.
sqlStmts := bytes.Split(f.Content, ";")
for _, sqlStmt := range sqlStmts {
if _, err := tx.Exec(string(sqlStmt)); err != nil {
mysqlErr := err.(*mysql.MySQLError) mysqlErr := err.(*mysql.MySQLError)
re, err := regexp.Compile(`at line ([0-9]+)$`) re, err := regexp.Compile(`at line ([0-9]+)$`)
@ -140,6 +141,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