Add test case for MySQL change delimiter #127

pull/151/head
Vojtech Vitek 2019-03-05 14:57:27 -05:00
parent 3836c78d69
commit 213f48bec6
3 changed files with 51 additions and 23 deletions

View File

@ -19,7 +19,7 @@ func runSQLMigration(db *sql.DB, statements []string, useTx bool, v int64, direc
if useTx {
// TRANSACTION.
verboseInfo("Begin transaction\n")
verboseInfo("Begin transaction")
tx, err := db.Begin()
if err != nil {
@ -29,7 +29,7 @@ func runSQLMigration(db *sql.DB, statements []string, useTx bool, v int64, direc
for _, query := range statements {
verboseInfo("Executing statement: %s\n", clearStatement(query))
if _, err = tx.Exec(query); err != nil {
verboseInfo("Rollback transaction\n")
verboseInfo("Rollback transaction")
tx.Rollback()
return errors.Wrapf(err, "failed to execute SQL query %q", clearStatement(query))
}
@ -37,19 +37,19 @@ func runSQLMigration(db *sql.DB, statements []string, useTx bool, v int64, direc
if direction {
if _, err := tx.Exec(GetDialect().insertVersionSQL(), v, direction); err != nil {
verboseInfo("Rollback transaction\n")
verboseInfo("Rollback transaction")
tx.Rollback()
return errors.Wrap(err, "failed to insert new goose version")
}
} else {
if _, err := tx.Exec(GetDialect().deleteVersionSQL(), v); err != nil {
verboseInfo("Rollback transaction\n")
verboseInfo("Rollback transaction")
tx.Rollback()
return errors.Wrap(err, "failed to delete goose version")
}
}
verboseInfo("Commit transaction\n")
verboseInfo("Commit transaction")
if err := tx.Commit(); err != nil {
return errors.Wrap(err, "failed to commit transaction")
}
@ -59,7 +59,7 @@ func runSQLMigration(db *sql.DB, statements []string, useTx bool, v int64, direc
// NO TRANSACTION.
for _, query := range statements {
verboseInfo("Executing statement: %s\n", clearStatement(query))
verboseInfo("Executing statement: %s", clearStatement(query))
if _, err := db.Exec(query); err != nil {
return errors.Wrapf(err, "failed to execute SQL query %q", clearStatement(query))
}
@ -71,9 +71,14 @@ func runSQLMigration(db *sql.DB, statements []string, useTx bool, v int64, direc
return nil
}
const (
grayColor = "\033[90m"
resetColor = "\033[00m"
)
func verboseInfo(s string, args ...interface{}) {
if verbose {
log.Printf(s, args...)
log.Printf(grayColor+s+resetColor, args...)
}
}

View File

@ -29,7 +29,7 @@ func (s *stateMachine) Get() parserState {
return parserState(*s)
}
func (s *stateMachine) Set(new parserState) {
verboseInfo("=> stateMachine: %v => %v", *s, new)
verboseInfo("StateMachine: %v => %v", *s, new)
*s = stateMachine(new)
}
@ -66,8 +66,9 @@ func parseSQLMigration(r io.Reader, direction bool) (stmts []string, useTx bool,
for scanner.Scan() {
line := scanner.Text()
verboseInfo(" %v\n", line)
if verbose {
log.Println(line)
}
if strings.HasPrefix(line, "--") {
cmd := strings.TrimSpace(strings.TrimPrefix(line, "--"))
@ -118,14 +119,14 @@ func parseSQLMigration(r io.Reader, direction bool) (stmts []string, useTx bool,
default:
// Ignore comments.
verboseInfo("=> ignore comment")
verboseInfo("StateMachine: ignore comment")
continue
}
}
// Ignore empty lines.
if matchEmptyLines.MatchString(line) {
verboseInfo("=> ignore empty line")
verboseInfo("StateMachine: ignore empty line")
continue
}
@ -143,13 +144,13 @@ func parseSQLMigration(r io.Reader, direction bool) (stmts []string, useTx bool,
case gooseUp, gooseStatementBeginUp, gooseStatementEndUp:
if !direction /*down*/ {
buf.Reset()
verboseInfo("=> ignore down")
verboseInfo("StateMachine: ignore down")
continue
}
case gooseDown, gooseStatementBeginDown, gooseStatementEndDown:
if direction /*up*/ {
buf.Reset()
verboseInfo("=> ignore up")
verboseInfo("StateMachine: ignore up")
continue
}
default:
@ -161,22 +162,22 @@ func parseSQLMigration(r io.Reader, direction bool) (stmts []string, useTx bool,
if endsWithSemicolon(line) {
stmts = append(stmts, buf.String())
buf.Reset()
verboseInfo("=> store simple up query")
verboseInfo("StateMachine: store simple Up query")
}
case gooseDown:
if endsWithSemicolon(line) {
stmts = append(stmts, buf.String())
buf.Reset()
verboseInfo("=> store simple down query")
verboseInfo("StateMachine: store simple Down query")
}
case gooseStatementEndUp:
stmts = append(stmts, buf.String())
buf.Reset()
verboseInfo("=> store up statement")
verboseInfo("StateMachine: store Up statement")
case gooseStatementEndDown:
stmts = append(stmts, buf.String())
buf.Reset()
verboseInfo("=> store down statement")
verboseInfo("StateMachine: store Down statement")
}
}
if err := scanner.Err(); err != nil {

View File

@ -45,6 +45,7 @@ func TestSplitStatements(t *testing.T) {
{sql: emptySQL, up: 0, down: 0},
{sql: emptySQL2, up: 0, down: 0},
{sql: functxt, up: 2, down: 2},
{sql: mysqlChangeDelimiter, up: 4, down: 0},
}
for i, test := range tt {
@ -118,16 +119,16 @@ CREATE TABLE post (
title text,
body text,
PRIMARY KEY(id)
); SELECT 1;
); -- 1st stmt
-- comment
SELECT 2;
SELECT 3; SELECT 3;
SELECT 4;
SELECT 2; -- 2nd stmt
SELECT 3; SELECT 3; -- 3rd stmt
SELECT 4; -- 4th stmt
-- +goose Down
-- comment
DROP TABLE post; SELECT 1; -- comment
DROP TABLE post; -- 1st stmt
`
var functxt = `-- +goose Up
@ -252,3 +253,24 @@ CREATE TABLE post (
PRIMARY KEY(id)
);
`
var mysqlChangeDelimiter = `
-- +goose Up
-- +goose StatementBegin
DELIMITER |
-- +goose StatementEnd
-- +goose StatementBegin
CREATE FUNCTION my_func( str CHAR(255) ) RETURNS CHAR(255) DETERMINISTIC
BEGIN
RETURN "Dummy Body";
END |
-- +goose StatementEnd
-- +goose StatementBegin
DELIMITER ;
-- +goose StatementEnd
select my_func("123") from dual;
-- +goose Down
`