From 28cb030ffef04f092ecf92abd713ad6b169ad8b5 Mon Sep 17 00:00:00 2001 From: Ashod Ayanyan Date: Mon, 18 Sep 2017 12:47:29 -0700 Subject: [PATCH] Add support for multiple DDL/SQL statements in one migration file --- database/spanner/spanner.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/database/spanner/spanner.go b/database/spanner/spanner.go index 44ac74f..6c65bab 100644 --- a/database/spanner/spanner.go +++ b/database/spanner/spanner.go @@ -137,12 +137,12 @@ func (s *Spanner) Run(migration io.Reader) error { } // run migration - stmt := string(migr[:]) + stmts := migrationStatements(migr) ctx := context.Background() op, err := s.db.admin.UpdateDatabaseDdl(ctx, &adminpb.UpdateDatabaseDdlRequest{ Database: s.config.DatabaseName, - Statements: []string{stmt}, + Statements: stmts, }) if err != nil { @@ -282,3 +282,13 @@ func (s *Spanner) ensureVersionTable() error { return nil } + +func migrationStatements(migration []byte) []string { + regex := regexp.MustCompile(";$") + migrationString := string(migration[:]) + migrationString = strings.TrimSpace(migrationString) + migrationString = regex.ReplaceAllString(migrationString, "") + + statements := strings.Split(migrationString, ";") + return statements +}