From 213f48bec60e1a23b3eddaa997e75e888c14af30 Mon Sep 17 00:00:00 2001 From: Vojtech Vitek Date: Tue, 5 Mar 2019 14:57:27 -0500 Subject: [PATCH] Add test case for MySQL change delimiter #127 --- migration_sql.go | 19 ++++++++++++------- sql_parser.go | 23 ++++++++++++----------- sql_parser_test.go | 32 +++++++++++++++++++++++++++----- 3 files changed, 51 insertions(+), 23 deletions(-) diff --git a/migration_sql.go b/migration_sql.go index c57313d..8703ec8 100644 --- a/migration_sql.go +++ b/migration_sql.go @@ -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...) } } diff --git a/sql_parser.go b/sql_parser.go index cbc3b28..27d1466 100644 --- a/sql_parser.go +++ b/sql_parser.go @@ -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 { diff --git a/sql_parser_test.go b/sql_parser_test.go index 7dd5c5f..319b04c 100644 --- a/sql_parser_test.go +++ b/sql_parser_test.go @@ -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 +`