-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for comments in Spanner migrations #409
Conversation
Pull Request Test Coverage Report for Build 800
💛 - Coveralls |
What's the procedure here given the tiny coverage decrease due to a error check (which is already being tested on the library that returns the actual error anyway)? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
Don't worry about the drop in test coverage since spanner doesn't have integration tests...
database/spanner/spanner.go
Outdated
@@ -142,9 +143,12 @@ func (s *Spanner) Run(migration io.Reader) error { | |||
} | |||
|
|||
// run migration | |||
stmts := migrationStatements(migr) | |||
ctx := context.Background() | |||
stmts, err := migrationStatements(migr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make statement parsing optional and disabled by default.
migrate
should not be parsing migrations since we treat the migration contents as an opaque blob
Not sure why this isn't already behind an optional. Let's name it something like x-clean-statements
and put it in Config.CleanStatements
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright. I assumed the way it was working before was not considered parsing so I restored it and put behind a flag. Felt that x-strip-comments
is a little more representative of the feature. What do you think?
Or did you mean to take the incoming byte slice and pass it straight as one statement?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe ParseDDL() also supports multiple statements, so x-strip-comments
isn't an accurate description of the behavior.
CleanStatements
seems like be best name so far since it re-formats the statements. e.g. does more than just parsing and validating structure.
I'm ok with making this a backwards incompatible change since it's limited to the spanner db driver and the trade-off of cleaner code is worth it. So anyone using this updated driver will need to specify x-clean-statements
to continue multi-statement support. We'll document this in the release notes as a breaking change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright. Another push. Added a little more documentation on the README as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like it didn't end up getting listed as breaking, FWIW.
Running into some issues with this after updating, mentioned in #426
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alethenorio if you have bandwidth, we can bump up the test coverage for spanner in a separate PR: #410 |
85f3982
to
e298c77
Compare
No promises but I'll try to get to it |
e298c77
to
8c522e9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the update!
Just a few minor things left.
database/spanner/spanner.go
Outdated
} | ||
} else { | ||
// run migration | ||
stmts = migrationStatements(migr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove migrationStatements()
since it's now replaced by cleanStatements()
?
e.g. something along the lines of:
stmts := []string{string(migr)}
if s.config.CleanStatements {
stmts, err = cleanStatements(migr)
if err != nil {
return err
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
database/spanner/README.md
Outdated
@@ -12,6 +12,7 @@ See [Google Spanner Documentation](https://cloud.google.com/spanner/docs) for de | |||
| Param | WithInstance Config | Description | | |||
| ----- | ------------------- | ----------- | | |||
| `x-migrations-table` | `MigrationsTable` | Name of the migrations table | | |||
| `x-clean-statements` | `CleanStatements` | Whether to parse DDL statements before running migration towards Spanner | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change this to "Whether to parse and clean". Also add something in the description about this flag and multi statement support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
database/spanner/spanner.go
Outdated
if err != nil { | ||
return nil, err | ||
} | ||
stmts := []string{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: pre-allocate the slice e.g. make([]string, 0, len(ddl.List))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
8c522e9
to
f7da555
Compare
All very good comments. Fixed them all 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for addressing my feedback!
2 final things and we're good to merge.
database/spanner/spanner_test.go
Outdated
if stmts := migrationStatements([]byte(tc.multiStatement)); !assert.Equal(t, stmts, tc.expected) { | ||
stmts, err := cleanStatements([]byte(tc.multiStatement)) | ||
if err != nil { | ||
t.Error() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make this t.Fatal("Error cleaning statements:", err)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. Since we are already using testify I switched the whole if statement to use require.NoError
which does the same but avoids the extra if
. Let me know if this is an issue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, didn't know about require.NoError()
!
database/spanner/spanner_test.go
Outdated
if err != nil { | ||
t.Error() | ||
} | ||
if !assert.Equal(t, tc.expected, stmts) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This if
check isn't necessary: https://godoc.org/github.com/stretchr/testify/assert#hdr-Example_Usage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doh, I've done this a million times before and probably missed this on copy paste
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
At the moment GCP Spanner backend does not seem to support comments (issue being tracked at https://issuetracker.google.com/issues/159730604). By adding support for parsing the migration DDL with spansql we are able to support comments and remove them before applying on the database
f7da555
to
e6faa29
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again for the PR!
At the moment GCP Spanner backend does not seem to support comments (issue being tracked at https://issuetracker.google.com/issues/159730604).
By parsing the migration DDL with spansql we are able to support comments and remove them before applying on the database.
Includes tests for comments as well