From 98d70ead6633d2614890c83fe36709485d149ecd Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 28 Jun 2019 09:21:02 +0200 Subject: SONAR-11964 Fail at startup when detecting MySQL --- .../main/java/org/sonar/db/DefaultDatabase.java | 21 ++++++++++++++++--- .../src/main/java/org/sonar/db/dialect/MySql.java | 11 ---------- .../java/org/sonar/db/DefaultDatabaseTest.java | 24 ++++++++++++++++------ .../test/java/org/sonar/db/dialect/MySqlTest.java | 14 ++----------- 4 files changed, 38 insertions(+), 32 deletions(-) (limited to 'server/sonar-db-core') diff --git a/server/sonar-db-core/src/main/java/org/sonar/db/DefaultDatabase.java b/server/sonar-db-core/src/main/java/org/sonar/db/DefaultDatabase.java index b2863282fe4..0349abf3a40 100644 --- a/server/sonar-db-core/src/main/java/org/sonar/db/DefaultDatabase.java +++ b/server/sonar-db-core/src/main/java/org/sonar/db/DefaultDatabase.java @@ -33,10 +33,12 @@ import org.apache.commons.dbcp2.BasicDataSourceFactory; import org.apache.commons.dbutils.DbUtils; import org.apache.commons.lang.StringUtils; import org.sonar.api.config.Settings; +import org.sonar.api.utils.MessageException; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.db.dialect.Dialect; import org.sonar.db.dialect.DialectUtils; +import org.sonar.db.dialect.MySql; import org.sonar.db.profiling.NullConnectionInterceptor; import org.sonar.db.profiling.ProfiledConnectionInterceptor; import org.sonar.db.profiling.ProfiledDataSource; @@ -63,8 +65,7 @@ public class DefaultDatabase implements Database { private static final String DBCP_JDBC_MAX_WAIT = "maxWaitMillis"; private static final Map SONAR_JDBC_TO_DBCP_PROPERTY_MAPPINGS = ImmutableMap.of( SONAR_JDBC_MAX_ACTIVE, DBCP_JDBC_MAX_ACTIVE, - SONAR_JDBC_MAX_WAIT, DBCP_JDBC_MAX_WAIT - ); + SONAR_JDBC_MAX_WAIT, DBCP_JDBC_MAX_WAIT); private final LogbackHelper logbackHelper; private final Settings settings; @@ -79,8 +80,9 @@ public class DefaultDatabase implements Database { @Override public void start() { + initSettings(); + failIfMySql(); try { - initSettings(); initDataSource(); checkConnection(); @@ -100,6 +102,19 @@ public class DefaultDatabase implements Database { properties.setProperty(SONAR_JDBC_DRIVER, dialect.getDefaultDriverClassName()); } + private void failIfMySql() { + if (!MySql.ID.equals(dialect.getId())) { + return; + } + throw MessageException.of("\n" + + "#############################################################################################################\n" + + "# End of Life of MySQL Support : SonarQube 7.9 and future versions do not support MySQL. #\n" + + "# Please migrate to a supported database. Get more details at #\n" + + "# https://community.sonarsource.com/t/end-of-life-of-mysql-support #\n" + + "# and https://github.com/SonarSource/mysql-migrator #\n" + + "#############################################################################################################\n"); + } + private void initDataSource() throws Exception { // but it's correctly caught by start() LOG.info("Create JDBC data source for {}", properties.getProperty(JDBC_URL.getKey()), DEFAULT_URL); diff --git a/server/sonar-db-core/src/main/java/org/sonar/db/dialect/MySql.java b/server/sonar-db-core/src/main/java/org/sonar/db/dialect/MySql.java index bc8dfe81528..08c5d887138 100644 --- a/server/sonar-db-core/src/main/java/org/sonar/db/dialect/MySql.java +++ b/server/sonar-db-core/src/main/java/org/sonar/db/dialect/MySql.java @@ -23,7 +23,6 @@ import java.sql.DatabaseMetaData; import java.sql.SQLException; import org.apache.commons.lang.StringUtils; import org.sonar.api.utils.Version; -import org.sonar.api.utils.log.Loggers; public class MySql extends AbstractDialect { @@ -62,15 +61,5 @@ public class MySql extends AbstractDialect { @Override public void init(DatabaseMetaData metaData) throws SQLException { checkDbVersion(metaData, MIN_SUPPORTED_VERSION); - - String message = "\n" + - "#############################################################################################################\n" + - "# End of Life of MySQL Support : SonarQube 7.8 is the last version that will support MySQL. #\n" + - "# Please consider migrating to a supported database. Get the migration detail on #\n" + - "# https://community.sonarsource.com/t/end-of-life-of-mysql-support #\n" + - "# and https://github.com/SonarSource/mysql-migrator #\n" + - "#############################################################################################################\n"; - - Loggers.get(getClass()).warn(message); } } diff --git a/server/sonar-db-core/src/test/java/org/sonar/db/DefaultDatabaseTest.java b/server/sonar-db-core/src/test/java/org/sonar/db/DefaultDatabaseTest.java index a43143b5861..f282aabcdb0 100644 --- a/server/sonar-db-core/src/test/java/org/sonar/db/DefaultDatabaseTest.java +++ b/server/sonar-db-core/src/test/java/org/sonar/db/DefaultDatabaseTest.java @@ -28,8 +28,8 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; -import org.sonar.api.config.Settings; import org.sonar.api.config.internal.MapSettings; +import org.sonar.api.utils.MessageException; import org.sonar.db.dialect.PostgreSql; import org.sonar.process.logging.LogbackHelper; @@ -40,10 +40,10 @@ import static org.mockito.Mockito.mock; @RunWith(DataProviderRunner.class) public class DefaultDatabaseTest { + private LogbackHelper logbackHelper = mock(LogbackHelper.class); private static final String SONAR_JDBC = "sonar.jdbc."; - @Rule public ExpectedException expectedException = none(); @@ -101,7 +101,7 @@ public class DefaultDatabaseTest { @Test public void shouldCompleteProperties() { - Settings settings = new MapSettings(); + MapSettings settings = new MapSettings(); DefaultDatabase db = new DefaultDatabase(logbackHelper, settings) { @Override @@ -118,7 +118,7 @@ public class DefaultDatabaseTest { @Test public void shouldStart() { - Settings settings = new MapSettings(); + MapSettings settings = new MapSettings(); settings.setProperty("sonar.jdbc.url", "jdbc:h2:mem:sonar"); settings.setProperty("sonar.jdbc.driverClassName", "org.h2.Driver"); settings.setProperty("sonar.jdbc.username", "sonar"); @@ -135,7 +135,7 @@ public class DefaultDatabaseTest { @Test public void shouldGuessDialectFromUrl() { - Settings settings = new MapSettings(); + MapSettings settings = new MapSettings(); settings.setProperty("sonar.jdbc.url", "jdbc:postgresql://localhost/sonar"); DefaultDatabase database = new DefaultDatabase(logbackHelper, settings); @@ -146,7 +146,7 @@ public class DefaultDatabaseTest { @Test public void shouldGuessDefaultDriver() { - Settings settings = new MapSettings(); + MapSettings settings = new MapSettings(); settings.setProperty("sonar.jdbc.url", "jdbc:postgresql://localhost/sonar"); DefaultDatabase database = new DefaultDatabase(logbackHelper, settings); @@ -155,6 +155,18 @@ public class DefaultDatabaseTest { assertThat(database.getProperties().getProperty("sonar.jdbc.driverClassName")).isEqualTo("org.postgresql.Driver"); } + @Test + public void fail_when_mysql_is_used() { + MapSettings settings = new MapSettings(); + settings.setProperty("sonar.jdbc.url", "jdbc:mysql://localhost/sonar"); + + DefaultDatabase database = new DefaultDatabase(logbackHelper, settings); + + expectedException.expect(MessageException.class); + expectedException.expectMessage("End of Life of MySQL Support"); + database.start(); + } + @DataProvider public static Object[][] sonarJdbcAndDbcpProperties() { return new Object[][] { diff --git a/server/sonar-db-core/src/test/java/org/sonar/db/dialect/MySqlTest.java b/server/sonar-db-core/src/test/java/org/sonar/db/dialect/MySqlTest.java index a176c15bdf1..5982642cbfd 100644 --- a/server/sonar-db-core/src/test/java/org/sonar/db/dialect/MySqlTest.java +++ b/server/sonar-db-core/src/test/java/org/sonar/db/dialect/MySqlTest.java @@ -21,14 +21,12 @@ package org.sonar.db.dialect; import java.sql.DatabaseMetaData; import java.sql.SQLException; -import java.util.List; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.mockito.Mockito; import org.sonar.api.utils.MessageException; import org.sonar.api.utils.log.LogTester; -import org.sonar.api.utils.log.LoggerLevel; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -86,24 +84,16 @@ public class MySqlTest { expectedException.expect(MessageException.class); expectedException.expectMessage("Unsupported mysql version: 5.5. Minimal supported version is 5.6."); - DatabaseMetaData metadata = newMetadata( 5, 5); + DatabaseMetaData metadata = newMetadata(5, 5); underTest.init(metadata); } @Test public void init_does_not_fail_if_mysql_5_6() throws Exception { - DatabaseMetaData metadata = newMetadata( 5, 6); + DatabaseMetaData metadata = newMetadata(5, 6); underTest.init(metadata); } - @Test - public void init_logs_warning() throws SQLException { - underTest.init(newMetadata(5, 6)); - - List logs = this.logs.logs(LoggerLevel.WARN); - assertThat(logs.get(0).contains("End of Life of MySQL Support : SonarQube 7.8 is the last version that will support MySQL.")).isTrue(); - } - @Test public void supportsUpsert_returns_false() { assertThat(underTest.supportsUpsert()).isFalse(); -- cgit v1.2.3