-
Notifications
You must be signed in to change notification settings - Fork 277
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
Session variables are leaking across migration scripts #216
Comments
Wouldn't closing and reopening the connection affect the transactional execution though? |
Sure, but a single DDL statement in your migration scripts will have the same effect, no? If you start a transaction, insert some records, and follow that up with a |
I think the fresh connection for each migration solution sounds ok. Are there any other downsides to this approach? We already open a fresh transaction for each migration, so I don’t think that is a blocker. FYI - last time I checked, Postgres supports transactional DDL for most statements, but mysql does not. |
Ah yes, it looks like you're correct regarding Postgres DDL transactions: The only other negative impact I can think of is the performance implications for users with a large number of migration scripts. I'm assuming that creating a new connection will take a bit more time than simply reusing an existing one. |
Perhaps the behavior of closing and reopening the connection can be controlled by a new cli flag? This way the default behavior is the same as before, meaning that the feature is backwards compatible. This way postgres users are not affected, and folks that want the new behavior can set the new flag. Say something like |
Just adding a note here so I don't forget: At least for the Postgres driver, consider issuing a For MySQL, issuing a This is a particularly gnarly issue for SQLite, because someone may want to use an in-memory database to test their migrations, and if we close and reopen the database in between migrations, each migration will be applied to a completely new in-memory database, which is not desirable. However, when testing against file-backed databases, it's important to reset session state between migrations to ensure that they are atomic. |
Problem
When a user defines a session variable, the expectation is that the variable will be scoped to the current migration script. However, the migrations seem to reuse the same session, resulting in scope leaks from one migration script to the next. You can recreate this problem using the following migration scripts:
These migrations will succeed if you run them in sequence, resulting in the following table records, which is not expected behavior.
The expected behavior is for migration_2 to result in an error. However, if we perform a single rollback and run the migration again, then the correct behavior is encountered (see below). This is because now migration_2 is running in isolation without the benefit of the variable leak from migration_1.
Problem Location
Here's location of the problematic function:
dbmate/pkg/dbmate/db.go
Line 317 in cdbbdd6
Solutions
A few ways to fix this include:
step
which will run only a single migration script and exitThe second option might be the most backwards compatible approach, since some folks may be dependent (perhaps unwittingly?) on this behavior.
The text was updated successfully, but these errors were encountered: