From: Teryk Bellahsene Date: Fri, 24 Jun 2016 15:47:01 +0000 (+0200) Subject: SONAR-7824 Make DB column ACTIVITIES.PROFILE_KEY not null X-Git-Tag: 6.0-RC1~221 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8823c53764c4304a32b39c4b9a2506a7e8ffc4e2;p=sonarqube.git SONAR-7824 Make DB column ACTIVITIES.PROFILE_KEY not null --- 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 index 00000000000..cb3b366a1de --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1261_make_profile_key_not_null_on_activities.rb @@ -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 diff --git a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java index 7279a6207be..1ab5f63e537 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java +++ b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java @@ -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 diff --git a/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java b/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java index fb1ec165eb1..15292be30f8 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java +++ b/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java @@ -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 index 00000000000..7a473ae2e83 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v60/MakeProfileKeyNotNullOnActivities.java @@ -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()); + } + +} diff --git a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql index de83b31f714..0f997903b43 100644 --- a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql +++ b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql @@ -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; diff --git a/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl b/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl index b9cafa5b94a..b89ca216e4e 100644 --- a/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl +++ b/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl @@ -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), diff --git a/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java index d5522ed08da..b72caa7bfba 100644 --- a/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java +++ b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java @@ -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); } } diff --git a/sonar-db/src/test/java/org/sonar/db/version/v52/RemoveAnalysisReportsFromActivitiesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v52/RemoveAnalysisReportsFromActivitiesTest.java index b1502e86b5f..798aa26ec90 100644 --- a/sonar-db/src/test/java/org/sonar/db/version/v52/RemoveAnalysisReportsFromActivitiesTest.java +++ b/sonar-db/src/test/java/org/sonar/db/version/v52/RemoveAnalysisReportsFromActivitiesTest.java @@ -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 index 00000000000..8e2039ce249 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v60/MakeProfileKeyNotNullOnActivitiesTest.java @@ -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 index 00000000000..338d203db7d --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v52/RemoveAnalysisReportsFromActivitiesTest/schema.sql @@ -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 index 00000000000..fead19a9ef2 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v60/MakeProfileKeyNotNullOnActivitiesTest/activities.sql @@ -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) +);