diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-03-07 22:13:35 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-03-07 23:30:28 +0100 |
commit | 0a608327b220e0d89a380fa4499af5247e1e85d5 (patch) | |
tree | f2e9b8ca5253606ff1a6c03fbd08f2364268d55b /sonar-batch/src | |
parent | 6cc1508a6207bf923da34659722e23e2e5c17599 (diff) | |
download | sonarqube-0a608327b220e0d89a380fa4499af5247e1e85d5.tar.gz sonarqube-0a608327b220e0d89a380fa4499af5247e1e85d5.zip |
SONAR-3292 improve messages when database is not up-to-date
Diffstat (limited to 'sonar-batch/src')
3 files changed, 120 insertions, 2 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java index 9f480c8980f..8ca3fe2a087 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java @@ -27,6 +27,7 @@ import org.sonar.batch.MavenPluginExecutor; import org.sonar.batch.ServerMetadata; import org.sonar.batch.config.BatchSettings; import org.sonar.batch.config.BatchSettingsEnhancer; +import org.sonar.core.persistence.DatabaseVersion; import org.sonar.jpa.session.DatabaseSessionProvider; import org.sonar.jpa.session.DefaultDatabaseConnector; import org.sonar.jpa.session.ThreadLocalDatabaseSessionFactory; @@ -64,14 +65,20 @@ public class BootstrapModule extends Module { // set as the current context classloader for hibernate, else it does not find the JDBC driver. Thread.currentThread().setContextClassLoader(bootstrapClassLoader); + // mybatis addCoreSingleton(BatchDatabase.class); addCoreSingleton(MyBatis.class); - addCoreSingleton(DefaultDatabaseConnector.class); - addCoreSingleton(ThreadLocalDatabaseSessionFactory.class); + addCoreSingleton(DatabaseVersion.class); + addCoreSingleton(DatabaseBatchCompatibility.class); for (Class daoClass : DaoUtils.getDaoClasses()) { addCoreSingleton(daoClass); } + + // hibernate + addCoreSingleton(DefaultDatabaseConnector.class); + addCoreSingleton(ThreadLocalDatabaseSessionFactory.class); addAdapter(new DatabaseSessionProvider()); + for (Object component : boostrapperComponents) { addCoreSingleton(component); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DatabaseBatchCompatibility.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DatabaseBatchCompatibility.java new file mode 100644 index 00000000000..683bcda5d1d --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DatabaseBatchCompatibility.java @@ -0,0 +1,54 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.batch.bootstrap; + +import org.sonar.api.BatchComponent; +import org.sonar.api.platform.Server; +import org.sonar.core.persistence.BadDatabaseVersion; +import org.sonar.core.persistence.DatabaseVersion; + +/** + * Detects if database is not up-to-date with the version required by the batch. + */ +public class DatabaseBatchCompatibility implements BatchComponent { + + private DatabaseVersion version; + private Server server; + + public DatabaseBatchCompatibility(DatabaseVersion version, Server server) { + this.version = version; + this.server = server; + } + + public void start() { + DatabaseVersion.Status status = version.getStatus(); + if (status == DatabaseVersion.Status.REQUIRES_DOWNGRADE) { + throw new BadDatabaseVersion("Database relates to a more recent version of Sonar. Please check your settings (JDBC settings, version of Maven plugin)"); + } + if (status == DatabaseVersion.Status.REQUIRES_UPGRADE) { + throw new BadDatabaseVersion("Database must be upgraded. Please browse " + server.getURL() + "/setup"); + } + if (status != DatabaseVersion.Status.UP_TO_DATE) { + // Support other future values + throw new BadDatabaseVersion("Unknown database status: " + status); + } + } + +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DatabaseBatchCompatibilityTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DatabaseBatchCompatibilityTest.java new file mode 100644 index 00000000000..2059313776a --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DatabaseBatchCompatibilityTest.java @@ -0,0 +1,57 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.batch.bootstrap; + +import org.junit.Test; +import org.sonar.api.config.Settings; +import org.sonar.api.platform.Server; +import org.sonar.batch.ServerMetadata; +import org.sonar.core.persistence.BadDatabaseVersion; +import org.sonar.core.persistence.DatabaseVersion; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class DatabaseBatchCompatibilityTest { + + private Server server = new ServerMetadata(new Settings()); + + @Test(expected = BadDatabaseVersion.class) + public void shouldFailIfRequiresDowngrade() { + DatabaseVersion version = mock(DatabaseVersion.class); + when(version.getStatus()).thenReturn(DatabaseVersion.Status.REQUIRES_DOWNGRADE); + new DatabaseBatchCompatibility(version, server).start(); + } + + @Test(expected = BadDatabaseVersion.class) + public void shouldFailIfRequiresUpgrade() { + DatabaseVersion version = mock(DatabaseVersion.class); + when(version.getStatus()).thenReturn(DatabaseVersion.Status.REQUIRES_UPGRADE); + new DatabaseBatchCompatibility(version, server).start(); + } + + @Test + public void shouldDoNothingIfUpToDate() { + DatabaseVersion version = mock(DatabaseVersion.class); + when(version.getStatus()).thenReturn(DatabaseVersion.Status.UP_TO_DATE); + new DatabaseBatchCompatibility(version, server).start(); + // no error + } +} |