]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6571 fix use of Boolean for boolean db column in Metrics table
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 2 Jun 2015 16:13:20 +0000 (18:13 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 2 Jun 2015 16:19:33 +0000 (18:19 +0200)
15 files changed:
server/sonar-server/src/main/java/org/sonar/server/batch/GlobalAction.java
server/sonar-server/src/main/java/org/sonar/server/db/migrations/MigrationStepModule.java
server/sonar-server/src/main/java/org/sonar/server/db/migrations/v52/FeedMetricsBooleans.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/metric/ws/CreateAction.java
server/sonar-server/src/test/java/org/sonar/server/db/migrations/MigrationStepModuleTest.java
server/sonar-server/src/test/java/org/sonar/server/db/migrations/v52/FeedMetricsBooleansTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/metric/persistence/MetricDaoTest.java
server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v52/FeedMetricsBooleansTest/migrate-result.xml [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v52/FeedMetricsBooleansTest/migrate.xml [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v52/FeedMetricsBooleansTest/schema.sql [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/metric/persistence/MetricDaoTest/manual_metric.xml
server/sonar-web/src/main/webapp/WEB-INF/db/migrate/916_feed_metrics_booleans.rb [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/metric/db/MetricDto.java
sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java
sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql

index 23956d9bbba3c4b8c27dc09fe2f855720db00a6a..c87da9be3a7cf7c6841307c482986a17e4742eb9 100644 (file)
@@ -80,7 +80,6 @@ public class GlobalAction implements BatchWsAction {
 
   private void addMetrics(GlobalRepositories ref, DbSession session) {
     for (MetricDto metric : dbClient.metricDao().selectEnabled(session)) {
-      Boolean optimizedBestValue = metric.isOptimizedBestValue();
       ref.addMetric(
         new org.sonar.batch.protocol.input.Metric(metric.getId(), metric.getKey(),
           metric.getValueType(),
@@ -91,7 +90,7 @@ public class GlobalAction implements BatchWsAction {
           metric.isUserManaged(),
           metric.getWorstValue(),
           metric.getBestValue(),
-          optimizedBestValue != null ? optimizedBestValue : false));
+          metric.isOptimizedBestValue()));
     }
   }
 
index cde817e8024e0aa0744a120ac817197ec7dcd09f..a63717387268e05b19b7766ff188c61037e720fa 100644 (file)
@@ -70,6 +70,7 @@ import org.sonar.server.db.migrations.v52.DropDependenciesComponentColumns;
 import org.sonar.server.db.migrations.v52.FeedDependenciesComponentUuids;
 import org.sonar.server.db.migrations.v52.FeedEventsComponentUuid;
 import org.sonar.server.db.migrations.v52.FeedFileSourcesDataType;
+import org.sonar.server.db.migrations.v52.FeedMetricsBooleans;
 import org.sonar.server.db.migrations.v52.FeedProjectLinksComponentUuid;
 import org.sonar.server.db.migrations.v52.MoveProjectProfileAssociation;
 
@@ -144,6 +145,7 @@ public class MigrationStepModule extends Module {
       FeedDependenciesComponentUuids.class,
       DropDependenciesComponentColumns.class,
       FeedFileSourcesDataType.class,
+      FeedMetricsBooleans.class,
       AddDependenciesColumns.class);
   }
 }
diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v52/FeedMetricsBooleans.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v52/FeedMetricsBooleans.java
new file mode 100644 (file)
index 0000000..27b7534
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.server.db.migrations.v52;
+
+import java.sql.SQLException;
+import org.sonar.core.persistence.Database;
+import org.sonar.server.db.migrations.BaseDataChange;
+
+public class FeedMetricsBooleans extends BaseDataChange {
+
+  public FeedMetricsBooleans(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    context.prepareUpsert("update metrics set OPTIMIZED_BEST_VALUE=?, HIDDEN=?, DELETE_HISTORICAL_DATA=? where user_managed=? or OPTIMIZED_BEST_VALUE is null or HIDDEN is null or DELETE_HISTORICAL_DATA is null")
+      .setBoolean(1, false)
+      .setBoolean(2, false)
+      .setBoolean(3, false)
+      .setBoolean(4, true)
+      .execute().commit();
+  }
+}
index d1511cea26cc34845ef9df44150f467b4208cc42..59b9054b14e6b851e3857d60b5a34f0881a73874 100644 (file)
@@ -158,6 +158,9 @@ public class CreateAction implements MetricsWsAction {
       .setUserManaged(true)
       .setDirection(0)
       .setQualitative(false)
+      .setHidden(false)
+      .setOptimizedBestValue(false)
+      .setDeleteHistoricalData(false)
       .setOrigin("GUI");
 
     dbClient.metricDao().insert(dbSession, metric);
index b0522568bed1adabbfaa9701b567498c19c6be12..abc50e78e770640c9ff11ac897fe98651a6da0f3 100644 (file)
@@ -29,6 +29,6 @@ public class MigrationStepModuleTest {
   public void verify_count_of_added_MigrationStep_types() throws Exception {
     ComponentContainer container = new ComponentContainer();
     new MigrationStepModule().configure(container);
-    assertThat(container.size()).isEqualTo(54);
+    assertThat(container.size()).isEqualTo(55);
   }
 }
diff --git a/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v52/FeedMetricsBooleansTest.java b/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v52/FeedMetricsBooleansTest.java
new file mode 100644 (file)
index 0000000..aac8b55
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.server.db.migrations.v52;
+
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.sonar.core.persistence.DbTester;
+import org.sonar.server.db.migrations.MigrationStep;
+
+public class FeedMetricsBooleansTest {
+  @ClassRule
+  public static DbTester db = new DbTester().schema(FeedMetricsBooleansTest.class, "schema.sql");
+
+  MigrationStep migration;
+
+  @Before
+  public void setUp() {
+    db.executeUpdateSql("truncate table metrics");
+
+    migration = new FeedMetricsBooleans(db.database());
+  }
+
+  @Test
+  public void migrate_empty_db() throws Exception {
+    migration.execute();
+  }
+
+  @Test
+  public void migrate() throws Exception {
+    db.prepareDbUnit(this.getClass(), "migrate.xml");
+    migration.execute();
+    db.assertDbUnit(this.getClass(), "migrate-result.xml", "metrics");
+  }
+}
index 1f77cf7c86636eeab554f8c4fdc95d2bd3cdd590..d83ea6e949157c54f92cb581cc66002c197c5134 100644 (file)
@@ -99,9 +99,9 @@ public class MetricDaoTest {
     assertThat(result.isUserManaged()).isTrue();
     assertThat(result.getWorstValue()).isNull();
     assertThat(result.getBestValue()).isNull();
-    assertThat(result.isOptimizedBestValue()).isNull();
-    assertThat(result.isDeleteHistoricalData()).isNull();
-    assertThat(result.isHidden()).isNull();
+    assertThat(result.isOptimizedBestValue()).isFalse();
+    assertThat(result.isDeleteHistoricalData()).isFalse();
+    assertThat(result.isHidden()).isFalse();
     assertThat(result.isEnabled()).isTrue();
   }
 
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v52/FeedMetricsBooleansTest/migrate-result.xml b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v52/FeedMetricsBooleansTest/migrate-result.xml
new file mode 100644 (file)
index 0000000..026d2db
--- /dev/null
@@ -0,0 +1,7 @@
+<dataset>
+
+  <metrics id="1" user_managed="[true]" optimized_best_value="[false]" hidden="[false]" delete_historical_data="[false]" />
+  <metrics id="2" user_managed="[false]" optimized_best_value="[true]" hidden="[true]" delete_historical_data="[true]" />
+  <metrics id="3" user_managed="[false]" optimized_best_value="[false]" hidden="[false]" delete_historical_data="[false]"/>
+
+</dataset>
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v52/FeedMetricsBooleansTest/migrate.xml b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v52/FeedMetricsBooleansTest/migrate.xml
new file mode 100644 (file)
index 0000000..7fee0be
--- /dev/null
@@ -0,0 +1,7 @@
+<dataset>
+
+  <metrics id="1" user_managed="[true]" optimized_best_value="[null]" hidden="[null]" delete_historical_data="[null]"/>
+  <metrics id="2" user_managed="[false]" optimized_best_value="[true]" hidden="[true]" delete_historical_data="[true]"/>
+  <metrics id="3" user_managed="[false]" optimized_best_value="[null]" hidden="[null]" delete_historical_data="[null]"/>
+
+</dataset>
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v52/FeedMetricsBooleansTest/schema.sql b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v52/FeedMetricsBooleansTest/schema.sql
new file mode 100644 (file)
index 0000000..d65487d
--- /dev/null
@@ -0,0 +1,7 @@
+CREATE TABLE "METRICS" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "USER_MANAGED" BOOLEAN DEFAULT FALSE,
+  "OPTIMIZED_BEST_VALUE" BOOLEAN,
+  "HIDDEN" BOOLEAN,
+  "DELETE_HISTORICAL_DATA" BOOLEAN
+);
index 630722c4d75624a35ea32163276070eff2e65081..8dc260a5faeeb4dd2e57c5b638c71128884542d1 100644 (file)
@@ -1,7 +1,7 @@
 <dataset>
 
   <metrics id="1" name="manual" val_type="INT" description="Manual metric" domain="" short_name="Manual metric"
-           qualitative="[false]" enabled="[true]" worst_value="[null]" optimized_best_value="[null]" best_value="[null]" direction="0" hidden="[null]"
-           origin="GUI" delete_historical_data="[null]" user_managed="[true]"/>
+           qualitative="[false]" enabled="[true]" worst_value="[null]" optimized_best_value="[false]" best_value="[null]" direction="0" hidden="[false]"
+           origin="GUI" delete_historical_data="[false]" user_managed="[true]"/>
 
 </dataset>
diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/916_feed_metrics_booleans.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/916_feed_metrics_booleans.rb
new file mode 100644 (file)
index 0000000..347164f
--- /dev/null
@@ -0,0 +1,31 @@
+#
+# SonarQube, open source software quality management tool.
+# Copyright (C) 2008-2014 SonarSource
+# mailto:contact AT sonarsource DOT com
+#
+# SonarQube 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.
+#
+# SonarQube 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 this program; if not, write to the Free Software Foundation,
+# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+#
+
+#
+# SonarQube 5.2
+# SONAR-6571
+#
+class FeedMetricsBooleans < ActiveRecord::Migration
+
+  def self.up
+    execute_java_migration('org.sonar.server.db.migrations.v52.FeedMetricsBooleans')
+  end
+
+end
index 233e3616075e9a58fcdc29ea4f9e9741bac27f4c..9e725c435bb622aa51c52260b1881d8060217fd7 100644 (file)
@@ -47,13 +47,13 @@ public class MetricDto {
 
   private Double bestValue;
 
-  private Boolean optimizedBestValue;
+  private boolean optimizedBestValue;
 
   private String origin;
 
-  private Boolean hidden;
+  private boolean hidden;
 
-  private Boolean deleteHistoricalData;
+  private boolean deleteHistoricalData;
 
   private boolean enabled;
 
@@ -162,15 +162,11 @@ public class MetricDto {
     return this;
   }
 
-  /**
-   * @return null for manual metrics
-   */
-  @CheckForNull
-  public Boolean isOptimizedBestValue() {
+  public boolean isOptimizedBestValue() {
     return optimizedBestValue;
   }
 
-  public MetricDto setOptimizedBestValue(@Nullable Boolean optimizedBestValue) {
+  public MetricDto setOptimizedBestValue(boolean optimizedBestValue) {
     this.optimizedBestValue = optimizedBestValue;
     return this;
   }
@@ -184,28 +180,20 @@ public class MetricDto {
     return this;
   }
 
-  /**
-   * @return null for manual metrics
-   */
-  @CheckForNull
-  public Boolean isHidden() {
+  public boolean isHidden() {
     return hidden;
   }
 
-  public MetricDto setHidden(@Nullable Boolean hidden) {
+  public MetricDto setHidden(boolean hidden) {
     this.hidden = hidden;
     return this;
   }
 
-  /**
-   * @return null for manual metrics
-   */
-  @CheckForNull
-  public Boolean isDeleteHistoricalData() {
+  public boolean isDeleteHistoricalData() {
     return deleteHistoricalData;
   }
 
-  public MetricDto setDeleteHistoricalData(@Nullable Boolean deleteHistoricalData) {
+  public MetricDto setDeleteHistoricalData(boolean deleteHistoricalData) {
     this.deleteHistoricalData = deleteHistoricalData;
     return this;
   }
index 9cf108aa1ae3eb0fdfa381ff1792b1093f9b5d1f..a07de30f9068154728b531ba0dcadfc8259993b7 100644 (file)
@@ -35,7 +35,7 @@ import java.util.List;
 @ServerSide
 public class DatabaseVersion {
 
-  public static final int LAST_VERSION = 915;
+  public static final int LAST_VERSION = 916;
 
   /**
    * List of all the tables.n
index a985006dde761c9f3b74a9040d4daec96253b9dc..714266f33daa3707a1080d4e844be29da9129570 100644 (file)
@@ -339,6 +339,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('912');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('913');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('914');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('915');
+INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('916');
 
 INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '1418215735482', '1418215735482', null, null);
 ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2;