Merge pull request #281 from ashoda/support_multiple_statements

(Spanner) Add support for multiple statements in one migration step
This commit is contained in:
Matthias Kadenbach 2017-10-09 11:03:59 -07:00 committed by GitHub
commit 5b98c13eff
1 changed files with 12 additions and 2 deletions

View File

@ -137,12 +137,12 @@ func (s *Spanner) Run(migration io.Reader) error {
} }
// run migration // run migration
stmt := string(migr[:]) stmts := migrationStatements(migr)
ctx := context.Background() ctx := context.Background()
op, err := s.db.admin.UpdateDatabaseDdl(ctx, &adminpb.UpdateDatabaseDdlRequest{ op, err := s.db.admin.UpdateDatabaseDdl(ctx, &adminpb.UpdateDatabaseDdlRequest{
Database: s.config.DatabaseName, Database: s.config.DatabaseName,
Statements: []string{stmt}, Statements: stmts,
}) })
if err != nil { if err != nil {
@ -282,3 +282,13 @@ func (s *Spanner) ensureVersionTable() error {
return nil 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
}