From c7f043b589959b365c88f7b9e404083d3075491d Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Tue, 20 Aug 2019 15:50:33 -0700 Subject: [PATCH] Added support for migrations with comments (mysql only) --- gomigrate.go | 13 +++++++++++++ test_migrations/test1_mysql/1_test_up.sql | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/gomigrate.go b/gomigrate.go index 09ecd52..73a4059 100644 --- a/gomigrate.go +++ b/gomigrate.go @@ -10,6 +10,7 @@ import ( "os" "path/filepath" "sort" + "strings" ) type migrationType string @@ -238,6 +239,18 @@ func (m *Migrator) ApplyMigration(migration *Migration, mType migrationType) err // Perform the migration. for _, cmd := range commands { + cmd = strings.ReplaceAll(cmd, "\n", "") // strip off leading whitespace + if strings.HasPrefix(cmd, "SELECT") && strings.HasSuffix(cmd, "AS comment") { + row := transaction.QueryRow(cmd) + var comment string + err = row.Scan(&comment) + if err != nil { + m.logger.Printf("Error executing query comment: %v", err) + return err + } + m.logger.Printf("Comment: %v", comment) + continue + } result, err := transaction.Exec(cmd) if err != nil { m.logger.Printf("Error executing migration: %v", err) diff --git a/test_migrations/test1_mysql/1_test_up.sql b/test_migrations/test1_mysql/1_test_up.sql index 5e504d5..f56d3b0 100644 --- a/test_migrations/test1_mysql/1_test_up.sql +++ b/test_migrations/test1_mysql/1_test_up.sql @@ -3,11 +3,15 @@ CREATE TABLE test ( PRIMARY KEY (id) ); +SELECT 'my comment' AS comment; + CREATE TABLE test2 ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (id) ); +SELECT 'my other comment' AS comment; + CREATE TABLE tt (c text NOT NULL); INSERT INTO tt VALUES('a');