diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2016-12-02 18:12:56 +0100 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2016-12-05 16:52:54 +0100 |
commit | 37494f41d3d07042fedb9bab93fb44587866db05 (patch) | |
tree | 88a381ec0813aa9fd8343fc4bc21b7d20d11aeb2 /sonar-db | |
parent | 19882feb2c947ed14cc90190a000d6fa0d3b39d0 (diff) | |
download | sonarqube-37494f41d3d07042fedb9bab93fb44587866db05.tar.gz sonarqube-37494f41d3d07042fedb9bab93fb44587866db05.zip |
SONAR-8464 Populate UUID column of table EVENTS
Diffstat (limited to 'sonar-db')
-rw-r--r-- | sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java | 2 | ||||
-rw-r--r-- | sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java | 4 | ||||
-rw-r--r-- | sonar-db/src/main/java/org/sonar/db/version/v63/PopulateUuidColumnOfEvents.java (renamed from sonar-db/src/main/java/org/sonar/db/version/v63/PopulateUuidColumnOnEvents.java) | 4 | ||||
-rw-r--r-- | sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql | 1 | ||||
-rw-r--r-- | sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java | 2 | ||||
-rw-r--r-- | sonar-db/src/test/java/org/sonar/db/version/v63/PopulateUuidColumnOfEventsTest.java | 95 | ||||
-rw-r--r-- | sonar-db/src/test/resources/org/sonar/db/version/v63/PopulateUuidColumnOfEventsTest/in_progress_events.sql | 12 |
7 files changed, 115 insertions, 5 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 317c3ad2e47..523f9292809 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_500; + public static final int LAST_VERSION = 1_501; /** * 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 b0a4467227b..29f5cf31177 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.PopulateUuidColumnOfEvents; public class MigrationStepModule extends Module { @Override @@ -388,6 +389,7 @@ public class MigrationStepModule extends Module { CreateTableWebhookDeliveries.class, // 6.3 - AddUuidToEvents.class); + AddUuidToEvents.class, + PopulateUuidColumnOfEvents.class); } } diff --git a/sonar-db/src/main/java/org/sonar/db/version/v63/PopulateUuidColumnOnEvents.java b/sonar-db/src/main/java/org/sonar/db/version/v63/PopulateUuidColumnOfEvents.java index 380cac0068c..698d29f188e 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/v63/PopulateUuidColumnOnEvents.java +++ b/sonar-db/src/main/java/org/sonar/db/version/v63/PopulateUuidColumnOfEvents.java @@ -28,11 +28,11 @@ import org.sonar.db.version.MassUpdate; import org.sonar.db.version.Select; import org.sonar.db.version.SqlStatement; -public class PopulateUuidColumnOnEvents extends BaseDataChange { +public class PopulateUuidColumnOfEvents extends BaseDataChange { private final UuidFactory uuidFactory; - public PopulateUuidColumnOnEvents(Database db, UuidFactory uuidFactory) { + public PopulateUuidColumnOfEvents(Database db, UuidFactory uuidFactory) { super(db); this.uuidFactory = uuidFactory; } 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 59149e8346a..4025f115fcd 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 @@ -514,6 +514,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1422'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1423'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1500'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1501'); 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/test/java/org/sonar/db/version/MigrationStepModuleTest.java b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java index b292574a3d2..2812e2550f7 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(165); + assertThat(container.size()).isEqualTo(166); } } diff --git a/sonar-db/src/test/java/org/sonar/db/version/v63/PopulateUuidColumnOfEventsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v63/PopulateUuidColumnOfEventsTest.java new file mode 100644 index 00000000000..1c67cc9fd20 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v63/PopulateUuidColumnOfEventsTest.java @@ -0,0 +1,95 @@ +/* + * 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.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.commons.lang.StringUtils; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.core.util.UuidFactoryImpl; +import org.sonar.db.DbTester; + +import static java.lang.String.valueOf; +import static org.assertj.core.api.Assertions.assertThat; + +public class PopulateUuidColumnOfEventsTest { + + private static final String TABLE_EVENTS = "events"; + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, PopulateUuidColumnOfEventsTest.class, "in_progress_events.sql"); + + private PopulateUuidColumnOfEvents underTest = new PopulateUuidColumnOfEvents(db.database(), UuidFactoryImpl.INSTANCE); + + @Test + public void migration_has_no_effect_on_empty_tables() throws SQLException { + underTest.execute(); + + assertThat(db.countRowsOfTable(TABLE_EVENTS)).isEqualTo(0); + } + + @Test + public void migration_generates_uuids() throws SQLException { + insertEvents(1); + insertEvents(2); + insertEvents(3); + db.commit(); + + underTest.execute(); + + verifyUuids(3); + } + + @Test + public void migration_is_reentrant() throws SQLException { + insertEvents(1); + + underTest.execute(); + verifyUuids(1); + + underTest.execute(); + verifyUuids(1); + } + + private void verifyUuids(int expectedCount) { + List<Map<String, Object>> rows = db.select("select uuid from events where uuid is not null"); + Set<Object> uuids = rows.stream().map(cols -> cols.get("UUID")).filter(uuid -> StringUtils.isNotBlank((String) uuid)).collect(Collectors.toSet()); + assertThat(uuids).hasSize(expectedCount); + } + + private String insertEvents(long id) { + String uuid = "uuid_" + id; + db.executeInsert( + TABLE_EVENTS, + "ID", valueOf(id), + "COMPONENT_UUID", valueOf(id + 10), + "ANALYSIS_UUID", valueOf(id + 100), + "EVENT_DATE", 123456, + "CREATED_AT", 456789); + return uuid; + } + +} diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v63/PopulateUuidColumnOfEventsTest/in_progress_events.sql b/sonar-db/src/test/resources/org/sonar/db/version/v63/PopulateUuidColumnOfEventsTest/in_progress_events.sql new file mode 100644 index 00000000000..d83b16dd817 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v63/PopulateUuidColumnOfEventsTest/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) +); |