-
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
Support multi-statement execution for PostgreSQL #495
Support multi-statement execution for PostgreSQL #495
Conversation
0bd3bd9
to
3a5169a
Compare
Hey @dhui! Can you please take a look? This is mostly a carbon copy from clickhouse multi-statement implementation. The init code, and constants duplicate each other across the drivers, so probably not a bad idea to extract this into a common place (or even introduce new Also I'm not sure how the multi-statement parser will split something like this: INSERT INTO foo (bar) VALUES (';'); Shall we improve it in a separate issue and PR? |
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!
Also I'm not sure how the multi-statement parser will split something like this:
INSERT INTO foo (bar) VALUES (';');
Shall we improve it in a separate issue and PR?
Agreed that this is an issue and is out of scope for this PR.
How would you work around this issue w/o writing a mini SQL parser? There are other cases with invalid SQL and various escape sequences that will break the simple and naive ;
splitting that multistmt
does.
migrate
intentionally avoids parsing migrations with multistmt
being the exception due to the need by various DB drivers and the simplicity of the approach.
FYI, unfortunately, merges are blocked until CI is fixed. See: #252 (comment)
However, if this PR looks good. I'll approve and will merge it once CI is fixed.
@@ -132,10 +140,23 @@ func (p *Postgres) Open(url string) (database.Driver, error) { | |||
} | |||
} | |||
|
|||
multiStatementMaxSize := DefaultMultiStatementMaxSize |
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.
The init code, and constants duplicate each other across the drivers, so probably not a bad idea to extract this into a common place (or even introduce new
MultiStatementDriver
interface).
Is this what you meant by init code?
What were you thinking in terms of a MultiStatementDriver
interface?
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!
I was hoping that this PR would also address this error I'm getting:
This is the migrations file looks like:
Any suggestions? |
@andream16 did you put |
@AnatolyRugalev thanks for the reply. I'm using I'm using I guess I'll have to try using |
@andream16 if you have var db *sql.DB
... // I assume db gets initialized here
pgDriver := postgres.WithInstance(db, postgres.Config{
MultiStatementEnabled: true
})
migrate.NewWithDatabaseInstance(sourceURL, dbName, pgDriver) |
@AnatolyRugalev that works! Thank you 🎉 For other folks having this problem - you can also achieve the same result using
|
This PR adds support for multi-statement SQL execution for PostgreSQL driver.
In PostgreSQL running multiple SQL statements in one
Exec
executes them inside a transaction. Sometimes this behavior is not desirable because some statements can be only run outside of transaction (e.g.CREATE INDEX CONCURRENTLY
). If you want to useCREATE INDEX CONCURRENTLY
without activating multi-statement mode you have to put such statements in a separate migration files.Resolves #284, Resolves #492