-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Set up SEARCH_PATH only once when running an update command #5444
base: master
Are you sure you want to change the base?
Conversation
…stgres DB to be called when running an update command family. - Remove DatabaseUtils.initializeDatabase() call from Postgres.rollback() implementation.
liquibase-standard/src/main/java/liquibase/database/core/DatabaseUtils.java
Fixed
Show fixed
Hide fixed
This is still a work in progress since I need to add some tests to this PR, but I would appreciate it if you guys could start reviewing these changes in the meantime. From a testing point of view what I have done so far is to execute
as one of them. Which will cause the rollback method to be triggered.
Also
NOTE: just in case I would like to mention part of the provided SQL outputs have been deleted to not make comments too long because of the length of them. |
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.
Looks good from here
try { | ||
searchPath = executor.queryForObject(new RawSqlStatement("SHOW SEARCH_PATH"), String.class); | ||
} catch (Throwable e) { | ||
Scope.getCurrentScope().getLog(DatabaseUtils.class).info("Cannot get search_path", e); |
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.
Maybe this should be logged at a different level than info?
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.
Right, thinking of updating this as a warning and the other one you comment below as an error. Would that be the correct logging level for both of these messages?
Thanks,
Daniel.
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.
Sounds right to me
try { | ||
executor.execute(new RawSqlStatement(String.format("ALTER DATABASE %s SET SEARCH_PATH TO %s", database.getLiquibaseCatalogName(), finalSearchPath))); | ||
} catch (Throwable e) { | ||
Scope.getCurrentScope().getLog(DatabaseUtils.class).info("Cannot set search_path at database level", e); |
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.
Log level?
- Refactor SearchPath initialization for postgres. - Test updated.
|
||
then: | ||
countSetSearchPathOccurrences(generatedSql, String.format("SET SEARCH_PATH TO %s, %s;", catalogName, searchPath)) == 0 | ||
countSetSearchPathOccurrences(generatedSql, String.format("ALTER DATABASE %s SET SEARCH_PATH TO %s;", catalogName, searchPath)) == 1 |
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.
@MalloD12 did you try to execute it with an user that is not database owner? Cause I tried to create an user, give him access to a database as below and then the alter failed:
--as postgres user:
create user tst with password 'tst';
create database tst;
grant connect, create on database tst to tst;
-- reconnect as tst, and then try it:
ALTER DATABASE tst SET SEARCH_PATH TO public;
[42501] ERROR: must be owner of database tst
I need to grant all privileges on database tst to tst
so it works.
I can think of some options:
- try the alter table; if it fails fall back to the set search repetition ;
- do not rollback connections for postgresql (is it really required) then we can have only one set search_path
- Create (another) flag so users can decide what to do.
I'm suggesting this because requiring all privileges would be a breaking change and could create problems with companies devops pipelines.
Impact
Description
Fixes #5316.
This PR provides a new
DatabaseUtils
method to set upSEARCH_PATH
variable permanently when running any update family command. Previously when running aupdate-sql
command on a Postgres DB we could see multipleSET SEARCH_PATH...
statements in the generated SQL output. It seems that was happening because of theDatabaseUtils.initializeDatabase()
call performed in therollback()
method fromPostgresDatabase
implementation. With this change that's not happening anymore, but SQL output will be a bit different now, since what we will see is:only once.
Things to be aware of
Things to worry about
update-one-changeset
) if we are happy with these changes.Additional Context