diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2021-01-07 15:28:26 -0600 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2021-02-08 20:07:45 +0000 |
commit | ea6e4464c8d6bf09bf748d1219d98a736689c9cb (patch) | |
tree | aa4370c1defd8723305e3c7fec9773449dfd9f3f /server/sonar-db-core | |
parent | 92762b12fd1662c75cab6cce036a2320956f233c (diff) | |
download | sonarqube-ea6e4464c8d6bf09bf748d1219d98a736689c9cb.tar.gz sonarqube-ea6e4464c8d6bf09bf748d1219d98a736689c9cb.zip |
SONAR-14323 Optimize DB migrations from 7.9 LTS to 8.X LTS
Diffstat (limited to 'server/sonar-db-core')
3 files changed, 68 insertions, 0 deletions
diff --git a/server/sonar-db-core/src/main/java/org/sonar/db/DatabaseUtils.java b/server/sonar-db-core/src/main/java/org/sonar/db/DatabaseUtils.java index 4f579f486f2..2d566ee3ede 100644 --- a/server/sonar-db-core/src/main/java/org/sonar/db/DatabaseUtils.java +++ b/server/sonar-db-core/src/main/java/org/sonar/db/DatabaseUtils.java @@ -351,6 +351,30 @@ public class DatabaseUtils { } } + public static boolean tableColumnExists(Connection connection, String tableName, String columnName) { + try { + return columnExists(connection, tableName.toLowerCase(Locale.US), columnName) + || columnExists(connection, tableName.toUpperCase(Locale.US), columnName); + } catch (SQLException e) { + throw wrapSqlException(e, "Can not check that column %s exists", columnName); + } + } + + private static boolean columnExists(Connection connection, String tableName, String columnName) throws SQLException { + String schema = getSchema(connection); + try (ResultSet rs = connection.getMetaData().getColumns(connection.getCatalog(), schema, tableName, null)) { + while (rs.next()) { + for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) { + String name = rs.getString(i); + if (columnName.equalsIgnoreCase(name)) { + return true; + } + } + } + return false; + } + } + @CheckForNull private static String getSchema(Connection connection) { String schema = null; diff --git a/server/sonar-db-core/src/test/java/org/sonar/db/DatabaseUtilsTest.java b/server/sonar-db-core/src/test/java/org/sonar/db/DatabaseUtilsTest.java index 92b20692e32..21ae0cbba43 100644 --- a/server/sonar-db-core/src/test/java/org/sonar/db/DatabaseUtilsTest.java +++ b/server/sonar-db-core/src/test/java/org/sonar/db/DatabaseUtilsTest.java @@ -86,6 +86,46 @@ public class DatabaseUtilsTest { } @Test + public void find_column_with_lower_case_table_name_and_upper_case_column_name() throws SQLException { + String tableName = "tablea"; + String columnName = "COLUMNA"; + try (Connection connection = dbTester.openConnection()) { + assertThat(DatabaseUtils.tableColumnExists(connection, tableName, columnName)).isTrue(); + assertThat(DatabaseUtils.tableColumnExists(connection, tableName.toLowerCase(Locale.US), columnName)).isTrue(); + } + } + @Test + public void find_column_with_upper_case_table_name_and_upper_case_column_name() throws SQLException { + String tableName = "TABLEA"; + String columnName = "COLUMNA"; + try (Connection connection = dbTester.openConnection()) { + assertThat(DatabaseUtils.tableColumnExists(connection, tableName, columnName)).isTrue(); + assertThat(DatabaseUtils.tableColumnExists(connection, tableName.toLowerCase(Locale.US), columnName)).isTrue(); + } + } + + @Test + public void find_column_with_lower_case_table_name_and_lower_case_column_name() throws SQLException { + String tableName = "tablea"; + String columnName = "columna"; + try (Connection connection = dbTester.openConnection()) { + assertThat(DatabaseUtils.tableColumnExists(connection, tableName, columnName)).isTrue(); + assertThat(DatabaseUtils.tableColumnExists(connection, tableName.toLowerCase(Locale.US), columnName)).isTrue(); + } + } + + @Test + public void find_column_with_upper_case_table_name_and_lower_case_column_name() throws SQLException { + String tableName = "TABLEA"; + String columnName = "columna"; + try (Connection connection = dbTester.openConnection()) { + assertThat(DatabaseUtils.tableColumnExists(connection, tableName, columnName)).isTrue(); + assertThat(DatabaseUtils.tableColumnExists(connection, tableName, columnName.toLowerCase(Locale.US))).isTrue(); + assertThat(DatabaseUtils.tableColumnExists(connection, tableName.toLowerCase(Locale.US), columnName.toLowerCase(Locale.US))).isTrue(); + } + } + + @Test public void should_close_connection() throws Exception { try (Connection connection = dbTester.openConnection()) { assertThat(isClosed(connection)).isFalse(); diff --git a/server/sonar-db-core/src/test/resources/org/sonar/db/DatabaseUtilsTest/sql.sql b/server/sonar-db-core/src/test/resources/org/sonar/db/DatabaseUtilsTest/sql.sql index ec4b8782e31..e0fbf26800c 100644 --- a/server/sonar-db-core/src/test/resources/org/sonar/db/DatabaseUtilsTest/sql.sql +++ b/server/sonar-db-core/src/test/resources/org/sonar/db/DatabaseUtilsTest/sql.sql @@ -2,6 +2,10 @@ CREATE TABLE "schema_migrations" ( "version" VARCHAR(256) NOT NULL ); +CREATE TABLE "TABLEA" ( + "COLUMNA" VARCHAR(256) NOT NULL +); + CREATE INDEX UPPER_CASE_NAME ON schema_migrations (version); CREATE INDEX lower_case_name ON schema_migrations (version); |