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
PostgreSQL search path not restored properly after migrations when auto-comit == false. #1959
Comments
We completely reimplemented search_path support on the master branch as part of the changes for #1926. I am unable to reproduce the scenario you described here with that code. Please build the latest sources, try again and confirm it is now working for you. |
Same problem with latest sources (cloned few hours ago from master). |
Thanks for checking. Then we must be missing something in our reproduction test case, as in our tests the search_path is always properly restored. How exactly is your datasource configured? Or even better, could you share a small repo that reproduces the problem? |
Datasource is configured via Spring Boot, using HikariCP (version 2.7.8).
From my understanding, the problem (with latest code) is in following:
Since there is no transaction around entire database/connection closing, nor around The problem does not appear with following config change:
If this doesn't help, I'll try to produce minimal test project. |
@jezovuk Thanks for the details. Our tests actually already covered by autocommit and non-autocommit modes and didn't uncover the behavior you describe, so if you can, sharing a minimal test project would be awesome. |
Will share tomorrow. |
Thank you so much for the sample project. That was immensely useful. Somehow we could only ever reproduce this with HikariCP and not with any of the other datasources we normally use. I still haven't fully figured out why, but at least we now have a reliable way to verify this. Fixed. |
You're welcome. Thanks for fixing. |
… migrations when auto-comit == false
@jezovuk does this issue re-occur for you with the current version of flyway? It seems to have regressed for me. |
Just tried the demo project linked above (https://github.com/jezovuk/DemoForFlywayIssue1959), with Flyway 6.3.2 & 6.3.3. Both seem to work fine. |
Which version and edition of Flyway are you using?
Flyway 5.0.7.
If this is not the latest version, can you reproduce the issue with the latest one as well?
(Many bugs are fixed in newer releases and upgrading will often resolve the issue)
Initially noticed with 5.0.5, repeated with 5.0.7. Seems problematic with current code in master (only from code inspection, I haven't actually tried it).
Which client are you using? (Command-line, Java API, Maven plugin, Gradle plugin)
Java API.
Which database are you using (type & version)?
PostgreSQL 9.6
Which operating system are you using?
Windows 10.
What did you do?
(Please include the content causing the issue, any relevant configuration settings, the SQL statement that failed (if relevant) and the command you ran.)
Migrating multiple db schemas via multiple
Flyway
instances (using sameDataSource
instance - same database user, same database config etc.).Relevant part of code:
Code above is common for several modules/classes, all using same (Spring-injected)
DataSource
instance, and each its own db schema (dbSchema() returns jOOQSchema
instance).We're running database migrations for all system modules upon system startup, sequentially (no parallel execution). Several db schemas contain views with same name (
v_output_data
). Typical db migration creatingv_output_data
view (repeatable migration) is of following form:We're using
drop
+create
instead ofcreate or replace
since PostgreSQL doesn't allow reducing number/changing type of view columns viacreate or replace
.What did you expect to see?
The expectation was that DB migrations for each schema were completely isolated from each other (and hence that
drop view
was perfectly safe even for newly created schemas).What did you see instead?
In this specific scenario, we already had one existing schema (let's call it
A
) in the DB with existing viewv_output_data
(and no further migrations to be applied) and one new schema (B
) with repeatable migration for view creation (formed as above -drop view
is redundant for first run, but was there for consistency and future use).What happened was following:
b.v_output_data
was created as expecteda.v_output_data
was dropped - this was not expected!After some debugging we found out that the problem is that restoring of current schema in
DbMigrate.migrate()
(connectionUserObjects.restoreCurrentSchema()
infinally
block at the end of the method) is executed non-transactionally. PostgreSQL'sset search_path
is transaction-aware.The other important piece of the puzzle is the fact that for PG current schema is actually set by prepending desired schema name to existing search path (
PostgreSQLConnection.changeCurrentSchemaTo()
).Finally, we have configured our data source not to use auto-commit, and all non-transactional changes are lost once connection is closed (which happens in
finally
block near the end ofFlyway.execute()
, whenDatabase
object is closed).In our case, migrations for schema
A
were checked first (no migration needed, but search path transactionally set to start withA
and not restored properly). After that, migrations for schemaB
were appied, with search path set toB,A,<default-search-path>
. At this point,drop view
in migration script intended forB
dropped the view fromA
(since the view has not yet been created inB
).I've checked current state of related code here on GitHub, and although it is refactored quite a bit, the problem seems to still be there (restoring is done in
Connection.close()
, still no transaction).The text was updated successfully, but these errors were encountered: