]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7824 Make DB column ACTIVITIES.PROFILE_KEY not null
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 24 Jun 2016 15:47:01 +0000 (17:47 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 29 Jun 2016 09:06:00 +0000 (11:06 +0200)
server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1261_make_profile_key_not_null_on_activities.rb [new file with mode: 0644]
sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java
sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java
sonar-db/src/main/java/org/sonar/db/version/v60/MakeProfileKeyNotNullOnActivities.java [new file with mode: 0644]
sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql
sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl
sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java
sonar-db/src/test/java/org/sonar/db/version/v52/RemoveAnalysisReportsFromActivitiesTest.java
sonar-db/src/test/java/org/sonar/db/version/v60/MakeProfileKeyNotNullOnActivitiesTest.java [new file with mode: 0644]
sonar-db/src/test/resources/org/sonar/db/version/v52/RemoveAnalysisReportsFromActivitiesTest/schema.sql [new file with mode: 0644]
sonar-db/src/test/resources/org/sonar/db/version/v60/MakeProfileKeyNotNullOnActivitiesTest/activities.sql [new file with mode: 0644]

diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1261_make_profile_key_not_null_on_activities.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1261_make_profile_key_not_null_on_activities.rb
new file mode 100644 (file)
index 0000000..cb3b366
--- /dev/null
@@ -0,0 +1,28 @@
+#
+# 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 6.0
+# SONAR-7824
+#
+class MakeProfileKeyNotNullOnActivities < ActiveRecord::Migration
+  def self.up
+    execute_java_migration('org.sonar.db.version.v60.MakeProfileKeyNotNullOnActivities')
+  end
+end
index 7279a6207bea0073964a4cdc8304a33bcabc883c..1ab5f63e5370d7e0dfa71929690b4406466d9abf 100644 (file)
@@ -30,7 +30,7 @@ import org.sonar.db.MyBatis;
 
 public class DatabaseVersion {
 
-  public static final int LAST_VERSION = 1_260;
+  public static final int LAST_VERSION = 1_261;
 
   /**
    * The minimum supported version which can be upgraded. Lower
index fb1ec165eb1e4d391d2ec4807bc72ac00c80519c..15292be30f8e68a1858a130aadfc4da12a57be9a 100644 (file)
@@ -117,6 +117,7 @@ import org.sonar.db.version.v60.MakeAnalysisUuidNotNullOnEvents;
 import org.sonar.db.version.v60.MakeComponentUuidColumnsNotNullOnSnapshots;
 import org.sonar.db.version.v60.MakeComponentUuidNotNullOnDuplicationsIndex;
 import org.sonar.db.version.v60.MakeComponentUuidNotNullOnMeasures;
+import org.sonar.db.version.v60.MakeProfileKeyNotNullOnActivities;
 import org.sonar.db.version.v60.MakeUuidColumnNotNullOnSnapshots;
 import org.sonar.db.version.v60.MakeUuidColumnsNotNullOnProjects;
 import org.sonar.db.version.v60.MakeUuidColumnsNotNullOnResourceIndex;
@@ -245,6 +246,7 @@ public class MigrationStepModule extends Module {
       PopulateLastUsedColumnOfRulesProfiles.class,
       AddProfileKeyToActivities.class,
       PopulateProfileKeyOfActivities.class,
+      MakeProfileKeyNotNullOnActivities.class,
 
       // SNAPSHOTS.UUID
       AddUuidColumnToSnapshots.class,
diff --git a/sonar-db/src/main/java/org/sonar/db/version/v60/MakeProfileKeyNotNullOnActivities.java b/sonar-db/src/main/java/org/sonar/db/version/v60/MakeProfileKeyNotNullOnActivities.java
new file mode 100644 (file)
index 0000000..7a473ae
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.db.version.v60;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.db.version.AlterColumnsBuilder;
+import org.sonar.db.version.DdlChange;
+
+import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder;
+
+public class MakeProfileKeyNotNullOnActivities extends DdlChange {
+
+  private static final String TABLE_ACTIVITIES = "activities";
+
+  public MakeProfileKeyNotNullOnActivities(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    context.execute(new AlterColumnsBuilder(getDatabase().getDialect(), TABLE_ACTIVITIES)
+      .updateColumn(newVarcharColumnDefBuilder().setColumnName("profile_key").setLimit(255).setIsNullable(false).build())
+      .build());
+  }
+
+}
index de83b31f714c124d259944d3e066ef641ddac711..0f997903b43826b7d7665840229bf230d1a34a33 100644 (file)
@@ -466,6 +466,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1256');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1257');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1258');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1259');
+INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1260');
 
 INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', true, 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '1418215735482', '1418215735482');
 ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2;
index b9cafa5b94a633f2208515dd6d42ba9044fc1e46..b89ca216e4e309920c3efc3808df5aa3c5da64e3 100644 (file)
@@ -476,7 +476,7 @@ CREATE TABLE "PERM_TEMPLATES_GROUPS" (
 CREATE TABLE "ACTIVITIES" (
   "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
   "LOG_KEY" VARCHAR(250),
-  "PROFILE_KEY" VARCHAR(255),
+  "PROFILE_KEY" VARCHAR(255) NOT NULL,
   "CREATED_AT" TIMESTAMP,
   "USER_LOGIN" VARCHAR(255),
   "LOG_TYPE" VARCHAR(250),
index d5522ed08da2317cdfc7b078e561222998149313..b72caa7bfbad180dfa1710fd1e4bf6da8839e42c 100644 (file)
@@ -29,6 +29,6 @@ public class MigrationStepModuleTest {
   public void verify_count_of_added_MigrationStep_types() {
     ComponentContainer container = new ComponentContainer();
     new MigrationStepModule().configure(container);
-    assertThat(container.size()).isEqualTo(116);
+    assertThat(container.size()).isEqualTo(117);
   }
 }
index b1502e86b5f7b7230c09f792dbf86f094b0fdfd7..798aa26ec90c9555bef46c59a5e1ad2f20f3c977 100644 (file)
@@ -22,27 +22,24 @@ package org.sonar.db.version.v52;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.utils.System2;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
-import org.sonar.db.activity.ActivityDto;
 import org.sonar.db.version.MigrationStep;
 
 import static org.assertj.core.api.Assertions.assertThat;
 
 public class RemoveAnalysisReportsFromActivitiesTest {
+  static final String TABLE_ACTIVITIES = "activities";
+
   @Rule
-  public DbTester db = DbTester.create(System2.INSTANCE);
-  DbClient dbClient = db.getDbClient();
-  DbSession dbSession = db.getSession();
+  public DbTester db = DbTester.createForSchema(System2.INSTANCE, RemoveAnalysisReportsFromActivitiesTest.class, "schema.sql");
 
   MigrationStep underTest = new RemoveAnalysisReportsFromActivities(db.database());
 
   @Test
   public void test() throws Exception {
-    dbClient.activityDao().insert(dbSession, new ActivityDto().setType("ANALYSIS_REPORT").setKey("1"));
-    dbClient.activityDao().insert(dbSession, new ActivityDto().setType("ANALYSIS_REPORT").setKey("2"));
-    dbClient.activityDao().insert(dbSession, new ActivityDto().setType("PROFILE_CHANGE").setKey("3"));
+    db.executeInsert(TABLE_ACTIVITIES, "log_type", "ANALYSIS_REPORT", "log_key", "1");
+    db.executeInsert(TABLE_ACTIVITIES, "log_type", "ANALYSIS_REPORT", "log_key", "2");
+    db.executeInsert(TABLE_ACTIVITIES, "log_type", "PROFILE_CHANGE", "log_key", "3");
 
     underTest.execute();
 
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v60/MakeProfileKeyNotNullOnActivitiesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v60/MakeProfileKeyNotNullOnActivitiesTest.java
new file mode 100644 (file)
index 0000000..8e2039c
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.db.version.v60;
+
+import java.sql.SQLException;
+import java.sql.Types;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+
+
+public class MakeProfileKeyNotNullOnActivitiesTest {
+
+  private static final String TABLE_ACTIVITIES = "activities";
+
+  @Rule
+  public DbTester db = DbTester.createForSchema(System2.INSTANCE, MakeProfileKeyNotNullOnActivitiesTest.class, "activities.sql");
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  private MakeProfileKeyNotNullOnActivities underTest = new MakeProfileKeyNotNullOnActivities(db.database());
+
+  @Test
+  public void migration_sets_uuid_column_not_nullable_on_empty_table() throws SQLException {
+    underTest.execute();
+
+    verifyColumnDefinitions();
+  }
+
+  @Test
+  public void migration_sets_uuid_column_not_nullable_on_populated_table() throws SQLException {
+    insertActivity(true);
+    insertActivity(true);
+
+    underTest.execute();
+
+    verifyColumnDefinitions();
+  }
+
+  @Test
+  public void migration_fails_if_some_row_has_a_null_profile_key() throws SQLException {
+    insertActivity(false);
+
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("Fail to execute");
+
+    underTest.execute();
+  }
+
+  private void verifyColumnDefinitions() {
+    db.assertColumnDefinition(TABLE_ACTIVITIES, "profile_key", Types.VARCHAR, 255, false);
+  }
+
+  private void insertActivity(boolean hasProfileKey) {
+    db.executeInsert(
+      TABLE_ACTIVITIES,
+      "user_login", "login",
+      "profile_key", hasProfileKey ? "my_profile_key" : null);
+  }
+
+}
diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v52/RemoveAnalysisReportsFromActivitiesTest/schema.sql b/sonar-db/src/test/resources/org/sonar/db/version/v52/RemoveAnalysisReportsFromActivitiesTest/schema.sql
new file mode 100644 (file)
index 0000000..338d203
--- /dev/null
@@ -0,0 +1,10 @@
+CREATE TABLE "ACTIVITIES" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "LOG_KEY" VARCHAR(250),
+  "CREATED_AT" TIMESTAMP,
+  "USER_LOGIN" VARCHAR(255),
+  "LOG_TYPE" VARCHAR(250),
+  "LOG_ACTION" VARCHAR(250),
+  "LOG_MESSAGE" VARCHAR(250),
+  "DATA_FIELD" CLOB(2147483647)
+);
diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v60/MakeProfileKeyNotNullOnActivitiesTest/activities.sql b/sonar-db/src/test/resources/org/sonar/db/version/v60/MakeProfileKeyNotNullOnActivitiesTest/activities.sql
new file mode 100644 (file)
index 0000000..fead19a
--- /dev/null
@@ -0,0 +1,11 @@
+CREATE TABLE "ACTIVITIES" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "LOG_KEY" VARCHAR(250),
+  "PROFILE_KEY" VARCHAR(255),
+  "CREATED_AT" TIMESTAMP,
+  "USER_LOGIN" VARCHAR(255),
+  "LOG_TYPE" VARCHAR(250),
+  "LOG_ACTION" VARCHAR(250),
+  "LOG_MESSAGE" VARCHAR(250),
+  "DATA_FIELD" CLOB(2147483647)
+);