diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2016-12-05 10:46:56 +0100 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2016-12-05 16:54:09 +0100 |
commit | 74c5ca3839bc61535a68d8eb7f89015c6f653b43 (patch) | |
tree | f243a7a2e476ab21b6bea58e52ab86742dcc1f34 /sonar-db | |
parent | 37494f41d3d07042fedb9bab93fb44587866db05 (diff) | |
download | sonarqube-74c5ca3839bc61535a68d8eb7f89015c6f653b43.tar.gz sonarqube-74c5ca3839bc61535a68d8eb7f89015c6f653b43.zip |
SONAR-8464 Make column UUID of Events unique and non null
Diffstat (limited to 'sonar-db')
11 files changed, 162 insertions, 4 deletions
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 523f9292809..5ece857917e 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_501; + public static final int LAST_VERSION = 1_502; /** * 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 29f5cf31177..6267b24e2e2 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 @@ -183,6 +183,7 @@ import org.sonar.db.version.v62.PopulateOrganizationUuidOfPermissionTemplates; import org.sonar.db.version.v62.PopulateOrganizationUuidOfUserRoles; import org.sonar.db.version.v62.UpdateQualityGateConditionsOnCoverage; import org.sonar.db.version.v63.AddUuidToEvents; +import org.sonar.db.version.v63.MakeUuidNotNullOnEvents; import org.sonar.db.version.v63.PopulateUuidColumnOfEvents; public class MigrationStepModule extends Module { @@ -390,6 +391,7 @@ public class MigrationStepModule extends Module { // 6.3 AddUuidToEvents.class, - PopulateUuidColumnOfEvents.class); + PopulateUuidColumnOfEvents.class, + MakeUuidNotNullOnEvents.class); } } diff --git a/sonar-db/src/main/java/org/sonar/db/version/v63/MakeUuidNotNullOnEvents.java b/sonar-db/src/main/java/org/sonar/db/version/v63/MakeUuidNotNullOnEvents.java new file mode 100644 index 00000000000..43b89391ec6 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v63/MakeUuidNotNullOnEvents.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.v63; + +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.UUID_SIZE; +import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class MakeUuidNotNullOnEvents extends DdlChange { + + private static final String TABLE = "events"; + + public MakeUuidNotNullOnEvents(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AlterColumnsBuilder(getDatabase().getDialect(), TABLE) + .updateColumn(newVarcharColumnDefBuilder().setColumnName("uuid").setLimit(UUID_SIZE).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 4025f115fcd..1c84288d0e4 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 @@ -515,6 +515,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1423'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1500'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1501'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1502'); INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, IS_ROOT, CREATED_AT, UPDATED_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', true, 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', true, '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 b7fd1a0331b..cde2dcdaf08 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 @@ -157,7 +157,7 @@ CREATE UNIQUE INDEX "RULES_REPO_KEY" ON "RULES" ("PLUGIN_NAME", "PLUGIN_RULE_KEY CREATE TABLE "EVENTS" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), - "UUID" VARCHAR(40), + "UUID" VARCHAR(40) NOT NULL, "NAME" VARCHAR(400), "ANALYSIS_UUID" VARCHAR(50) NOT NULL, "COMPONENT_UUID" VARCHAR(50), @@ -169,6 +169,7 @@ CREATE TABLE "EVENTS" ( ); CREATE INDEX "EVENTS_ANALYSIS" ON "EVENTS" ("ANALYSIS_UUID"); CREATE INDEX "EVENTS_COMPONENT_UUID" ON "EVENTS" ("COMPONENT_UUID"); +CREATE UNIQUE INDEX "EVENTS_UUID" ON "EVENTS" ("UUID"); CREATE TABLE "QUALITY_GATES" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), 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 2812e2550f7..02056648d4b 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(166); + assertThat(container.size()).isEqualTo(167); } } diff --git a/sonar-db/src/test/java/org/sonar/db/version/v63/MakeUuidNotNullOnEventsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v63/MakeUuidNotNullOnEventsTest.java new file mode 100644 index 00000000000..2813b7b5488 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v63/MakeUuidNotNullOnEventsTest.java @@ -0,0 +1,87 @@ +/* + * 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.v63; + +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; + +import static java.lang.String.valueOf; + +public class MakeUuidNotNullOnEventsTest { + + private static final String TABLE_EVENTS = "events"; + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, MakeUuidNotNullOnEventsTest.class, "in_progress_events.sql"); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private MakeUuidNotNullOnEvents underTest = new MakeUuidNotNullOnEvents(db.database()); + + @Test + public void migration_sets_uuid_column_not_nullable_on_empty_table() throws SQLException { + underTest.execute(); + + verifyColumnDefinition(); + } + + @Test + public void migration_sets_uuid_column_not_nullable_on_populated_table() throws SQLException { + insertEvent(1, true); + insertEvent(2, true); + + underTest.execute(); + + verifyColumnDefinition(); + } + + @Test + public void migration_fails_if_some_row_has_a_null_uuid() throws SQLException { + insertEvent(1, false); + + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("Fail to execute"); + + underTest.execute(); + } + + private void verifyColumnDefinition() { + db.assertColumnDefinition(TABLE_EVENTS, "uuid", Types.VARCHAR, 40, false); + } + + private String insertEvent(long id, boolean hasUuid) { + String uuid = "uuid_" + id; + db.executeInsert( + TABLE_EVENTS, + "ID", valueOf(id), + "ANALYSIS_UUID", valueOf(id + 10), + "COMPONENT_UUID", valueOf(id + 100), + "CREATED_AT", id, + "EVENT_DATE", id, + "UUID", hasUuid ? "uuid_" + id : null); + return uuid; + } +} diff --git a/sonar-db/src/test/resources/org/sonar/db/component/SnapshotDaoTest/select_previous_version_snapshots.xml b/sonar-db/src/test/resources/org/sonar/db/component/SnapshotDaoTest/select_previous_version_snapshots.xml index cc1d280ae32..0bab858a782 100644 --- a/sonar-db/src/test/resources/org/sonar/db/component/SnapshotDaoTest/select_previous_version_snapshots.xml +++ b/sonar-db/src/test/resources/org/sonar/db/component/SnapshotDaoTest/select_previous_version_snapshots.xml @@ -124,6 +124,7 @@ /> <events id="1" + uuid="P1" analysis_uuid="u1000" component_uuid="ABCD" name="1.0" @@ -133,6 +134,7 @@ description="" event_data="[null]"/> <events id="2" + uuid="P2" analysis_uuid="u1000" component_uuid="ABCD" name="Foo" @@ -142,6 +144,7 @@ description="" event_data="[null]"/> <events id="3" + uuid="P3" analysis_uuid="u1001" component_uuid="ABCD" name="1.1" @@ -151,6 +154,7 @@ description="" event_data="[null]"/> <events id="4" + uuid="P4" analysis_uuid="u1001" component_uuid="ABCD" name="Bar" @@ -160,6 +164,7 @@ description="" event_data="[null]"/> <events id="5" + uuid="P5" analysis_uuid="u1002" component_uuid="ABCD" name="Uhh" @@ -169,6 +174,7 @@ description="" event_data="[null]"/> <events id="6" + uuid="P6" analysis_uuid="u1003" component_uuid="ABCD" name="1.2-SNAPSHOT" diff --git a/sonar-db/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldDeleteResource.xml b/sonar-db/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldDeleteResource.xml index 3ade75966f4..9881dbd7124 100644 --- a/sonar-db/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldDeleteResource.xml +++ b/sonar-db/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldDeleteResource.xml @@ -42,6 +42,7 @@ /> <events id="1" + uuid="P1" analysis_uuid="u1" component_uuid="uuid_1" name="Version 1.0" @@ -52,6 +53,7 @@ event_data="[null]"/> <events id="2" + uuid="P2" analysis_uuid="u1" component_uuid="[null]" name="Version 1.0" @@ -62,6 +64,7 @@ event_data="[null]"/> <events id="3" + uuid="P3" analysis_uuid="u1" component_uuid="[null]" name="Version 1.0" diff --git a/sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/shouldSelectPurgeableAnalysis.xml b/sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/shouldSelectPurgeableAnalysis.xml index fda2def6553..19482e742cb 100644 --- a/sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/shouldSelectPurgeableAnalysis.xml +++ b/sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/shouldSelectPurgeableAnalysis.xml @@ -136,6 +136,7 @@ /> <events id="2" + uuid="P2" analysis_uuid="u5" component_uuid="1" category="Version" diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v63/MakeUuidNotNullOnEventsTest/in_progress_events.sql b/sonar-db/src/test/resources/org/sonar/db/version/v63/MakeUuidNotNullOnEventsTest/in_progress_events.sql new file mode 100644 index 00000000000..d83b16dd817 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v63/MakeUuidNotNullOnEventsTest/in_progress_events.sql @@ -0,0 +1,12 @@ +CREATE TABLE "EVENTS" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "UUID" VARCHAR(40), + "NAME" VARCHAR(400), + "ANALYSIS_UUID" VARCHAR(50) NOT NULL, + "COMPONENT_UUID" VARCHAR(50), + "CATEGORY" VARCHAR(50), + "EVENT_DATE" BIGINT NOT NULL, + "CREATED_AT" BIGINT NOT NULL, + "DESCRIPTION" VARCHAR(4000), + "EVENT_DATA" VARCHAR(4000) +); |