Skip to content
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

Ensure Postgresql search_path entries are quoted correctly #3009

Merged
merged 1 commit into from Jul 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,5 +1,6 @@
package liquibase.database.core;

import liquibase.GlobalConfiguration;
import liquibase.Scope;
import liquibase.database.Database;
import liquibase.database.OfflineConnection;
Expand All @@ -8,6 +9,7 @@
import liquibase.executor.ExecutorService;
import liquibase.statement.core.RawSqlStatement;
import liquibase.structure.core.Schema;
import liquibase.util.StringUtil;

public class DatabaseUtils {
/**
Expand Down Expand Up @@ -39,11 +41,22 @@ public static void initializeDatabase(String defaultCatalogName, String defaultS
String searchPath = executor.queryForObject(new RawSqlStatement("SHOW SEARCH_PATH"), String.class);

if (!searchPath.equals(defaultCatalogName) && !searchPath.startsWith(defaultSchemaName + ",") && !searchPath.startsWith("\"" + defaultSchemaName + "\",")) {
if (database instanceof CockroachDatabase) {
// CockroachDB doesn't support unquoted `$user` type values
searchPath = searchPath.replaceAll("(\\$\\w+)", "'$1'");
String finalSearchPath;
if (GlobalConfiguration.PRESERVE_SCHEMA_CASE.getCurrentValue()) {
finalSearchPath = ((PostgresDatabase) database).quoteObject(defaultSchemaName, Schema.class);
} else {
finalSearchPath = defaultSchemaName;
}
executor.execute(new RawSqlStatement("SET SEARCH_PATH TO " + database.escapeObjectName(defaultSchemaName, Schema.class) + ", " + searchPath));

//If existing search path entries are not quoted, quote them. Some databases do not show them as quoted even though they need to be (like $user or case sensitive schemas)
finalSearchPath += ", " + StringUtil.join(StringUtil.splitAndTrim(searchPath, ","), ",", (StringUtil.StringUtilFormatter<String>) obj -> {
if (obj.startsWith("\"")) {
return obj;
}
return ((PostgresDatabase) database).quoteObject(obj, Schema.class);
});

executor.execute(new RawSqlStatement("SET SEARCH_PATH TO " + finalSearchPath));
}

} else if (database instanceof AbstractDb2Database) {
Expand Down