3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform.db.migration.charset;
22 import com.google.common.annotations.VisibleForTesting;
23 import com.google.common.base.Joiner;
24 import java.sql.Connection;
25 import java.sql.SQLException;
26 import java.util.LinkedHashSet;
27 import java.util.List;
29 import java.util.stream.Collectors;
30 import org.sonar.api.utils.MessageException;
31 import org.slf4j.LoggerFactory;
32 import org.sonar.db.version.SqTables;
34 import static java.lang.String.format;
35 import static java.util.Optional.ofNullable;
36 import static org.apache.commons.lang.StringUtils.containsIgnoreCase;
37 import static org.apache.commons.lang.StringUtils.isBlank;
39 class PostgresCharsetHandler extends CharsetHandler {
41 private final PostgresMetadataReader metadata;
43 PostgresCharsetHandler(SqlExecutor selectExecutor, PostgresMetadataReader metadata) {
44 super(selectExecutor);
45 this.metadata = metadata;
49 void handle(Connection connection, DatabaseCharsetChecker.State state) throws SQLException {
50 // PostgreSQL does not have concept of case-sensitive collation. Only charset ("encoding" in postgresql terminology)
52 expectUtf8AsDefault(connection);
54 if (state == DatabaseCharsetChecker.State.UPGRADE || state == DatabaseCharsetChecker.State.STARTUP) {
55 // no need to check columns on fresh installs... as they are not supposed to exist!
56 expectUtf8Columns(connection);
60 private void expectUtf8AsDefault(Connection connection) throws SQLException {
61 LoggerFactory.getLogger(getClass()).info("Verify that database charset supports UTF8");
62 String collation = metadata.getDefaultCharset(connection);
63 if (!containsIgnoreCase(collation, UTF8)) {
64 throw MessageException.of(format("Database charset is %s. It must support UTF8.", collation));
68 private void expectUtf8Columns(Connection connection) throws SQLException {
69 // Charset is defined globally and can be overridden on each column.
70 // This request returns all VARCHAR columns. Charset may be empty.
73 // projects | name | utf8
74 var sqTables = getSqTables();
75 var schema = getSchema(connection);
76 List<String[]> rows = getSqlExecutor().select(connection, String.format("select table_name, column_name, collation_name " +
77 "from information_schema.columns " +
78 "where table_schema='%s' " +
79 "and table_name in (%s) " +
80 "and udt_name='varchar' " +
81 "order by table_name, column_name", schema, sqTables), new SqlExecutor.StringsConverter(3 /* columns returned by SELECT */));
82 Set<String> errors = new LinkedHashSet<>();
83 for (String[] row : rows) {
84 if (!isBlank(row[2]) && !containsIgnoreCase(row[2], UTF8)) {
85 errors.add(format("%s.%s", row[0], row[1]));
89 if (!errors.isEmpty()) {
90 throw MessageException.of(format("Database columns [%s] must have UTF8 charset.", Joiner.on(", ").join(errors)));
94 private static String getSchema(Connection connection) throws SQLException {
95 return ofNullable(connection.getSchema()).orElse("public");
98 private static String getSqTables() {
99 return SqTables.TABLES.stream().map(s -> "'" + s + "'").collect(Collectors.joining(","));
103 PostgresMetadataReader getMetadata() {