]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8183 fix medium test DB cleaning too violent
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 28 Sep 2016 08:49:39 +0000 (10:49 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 28 Sep 2016 13:26:30 +0000 (15:26 +0200)
medium test DB cleaning was deleting data inserted by default in DB: schema_migrations, user admin, default organization

server/sonar-server/src/main/java/org/sonar/server/platform/BackendCleanup.java
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java

index 7850e8e0ee96aca3f18b2262403c8193f3f5ecca..3d7dc3ca34ac0830e24d4a7419924fd456305cc1 100644 (file)
  */
 package org.sonar.server.platform;
 
+import com.google.common.collect.ImmutableMap;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.SQLException;
 import java.sql.Statement;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Optional;
 import org.apache.commons.dbutils.DbUtils;
 import org.sonar.api.server.ServerSide;
 import org.sonar.api.utils.log.Loggers;
@@ -32,6 +36,7 @@ import org.sonar.db.version.DatabaseVersion;
 import org.sonar.server.es.BulkIndexer;
 import org.sonar.server.es.EsClient;
 import org.sonar.server.issue.index.IssueIndexDefinition;
+import org.sonar.server.property.InternalProperties;
 import org.sonar.server.view.index.ViewIndexDefinition;
 
 import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
@@ -47,6 +52,12 @@ public class BackendCleanup {
   private static final String[] RESOURCE_RELATED_TABLES = {
     "group_roles", "user_roles", "properties"
   };
+  private static final Map<String, TableCleaner> TABLE_CLEANERS = ImmutableMap.of(
+    "organizations", BackendCleanup::truncateOrganizations,
+    "users", BackendCleanup::truncateUsers,
+    "internal_properties", BackendCleanup::truncateInternalProperties,
+      "schema_migrations", BackendCleanup::truncateSchemaMigrations);
+
   private final EsClient esClient;
   private final MyBatis myBatis;
 
@@ -61,22 +72,16 @@ public class BackendCleanup {
   }
 
   public void clearDb() {
-    DbSession dbSession = myBatis.openSession(false);
-    Connection connection = dbSession.getConnection();
-    Statement statement = null;
-    try {
-      statement = connection.createStatement();
-      for (String table : DatabaseVersion.TABLES) {
-        statement.execute("TRUNCATE TABLE " + table.toLowerCase());
-        // commit is useless on some databases
-        connection.commit();
+    try (DbSession dbSession = myBatis.openSession(false);
+      Connection connection = dbSession.getConnection();
+      Statement ddlStatement = connection.createStatement()) {
+      for (String tableName : DatabaseVersion.TABLES) {
+        Optional.ofNullable(TABLE_CLEANERS.get(tableName))
+          .orElse(BackendCleanup::truncateDefault)
+          .clean(tableName, ddlStatement, connection);
       }
     } catch (Exception e) {
       throw new IllegalStateException("Fail to clear db", e);
-    } finally {
-      DbUtils.closeQuietly(statement);
-      DbUtils.closeQuietly(connection);
-      MyBatis.closeQuietly(dbSession);
     }
   }
 
@@ -165,4 +170,57 @@ public class BackendCleanup {
     BulkIndexer.delete(esClient, indexName, esClient.prepareSearch(indexName).setQuery(matchAllQuery()));
   }
 
+  private interface TableCleaner {
+    void clean(String tableName, Statement ddlStatement, Connection connection) throws SQLException;
+  }
+
+  private static void truncateDefault(String tableName, Statement ddlStatement, Connection connection) throws SQLException {
+    ddlStatement.execute("TRUNCATE TABLE " + tableName.toLowerCase(Locale.ENGLISH));
+    // commit is useless on some databases
+    connection.commit();
+  }
+
+  /**
+   * Default organization must never be deleted
+   */
+  private static void truncateOrganizations(String tableName, Statement ddlStatement, Connection connection) throws SQLException {
+    try (PreparedStatement preparedStatement = connection.prepareStatement("delete from organizations where kee <> ?")) {
+      preparedStatement.setString(1, "default-organization");
+      preparedStatement.execute();
+      // commit is useless on some databases
+      connection.commit();
+    }
+  }
+
+  /**
+   * User admin must never be deleted.
+   */
+  private static void truncateUsers(String tableName, Statement ddlStatement, Connection connection) throws SQLException {
+    try (PreparedStatement preparedStatement = connection.prepareStatement("delete from users where login <> ?")) {
+      preparedStatement.setString(1, "admin");
+      preparedStatement.execute();
+      // commit is useless on some databases
+      connection.commit();
+    }
+  }
+
+  /**
+   * Internal property {@link InternalProperties#DEFAULT_ORGANIZATION} must never be deleted.
+   */
+  private static void truncateInternalProperties(String tableName, Statement ddlStatement, Connection connection) throws SQLException {
+    try (PreparedStatement preparedStatement = connection.prepareStatement("delete from internal_properties where kee <> ?")) {
+      preparedStatement.setString(1, InternalProperties.DEFAULT_ORGANIZATION);
+      preparedStatement.execute();
+      // commit is useless on some databases
+      connection.commit();
+    }
+  }
+
+  /**
+   * Data in SCHEMA_MIGRATIONS table is inserted when DB is created and should not be altered afterwards.
+   */
+  private static void truncateSchemaMigrations(String tableName, Statement ddlStatement, Connection connection) throws SQLException {
+    // do nothing
+  }
+
 }
index 09b1365e828c042ee4d81311f96607aab87db5dc..f9de5a50c4cd4631fc5ba7455a006fe4589c9897 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.server.platform.platformlevel;
 import org.sonar.server.app.ProcessCommandWrapper;
 import org.sonar.server.es.IndexerStartupTask;
 import org.sonar.server.issue.filter.RegisterIssueFilters;
+import org.sonar.server.organization.DefaultOrganizationEnforcer;
 import org.sonar.server.platform.ServerLifecycleNotifier;
 import org.sonar.server.platform.web.RegisterServletFilters;
 import org.sonar.server.qualitygate.RegisterQualityGates;
@@ -49,7 +50,8 @@ public class PlatformLevelStartup extends PlatformLevel {
   protected void configureLevel() {
     add(GeneratePluginIndex.class,
       RegisterServletFilters.class,
-      ServerLifecycleNotifier.class);
+      ServerLifecycleNotifier.class,
+      DefaultOrganizationEnforcer.class);
 
     addIfStartupLeader(
       IndexerStartupTask.class,