From 37494f41d3d07042fedb9bab93fb44587866db05 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Fri, 2 Dec 2016 18:12:56 +0100 Subject: SONAR-8464 Populate UUID column of table EVENTS --- .../java/org/sonar/db/version/DatabaseVersion.java | 2 +- .../org/sonar/db/version/MigrationStepModule.java | 4 +- .../db/version/v63/PopulateUuidColumnOfEvents.java | 56 +++++++++++++ .../db/version/v63/PopulateUuidColumnOnEvents.java | 56 ------------- .../resources/org/sonar/db/version/rows-h2.sql | 1 + .../sonar/db/version/MigrationStepModuleTest.java | 2 +- .../v63/PopulateUuidColumnOfEventsTest.java | 95 ++++++++++++++++++++++ .../in_progress_events.sql | 12 +++ 8 files changed, 169 insertions(+), 59 deletions(-) create mode 100644 sonar-db/src/main/java/org/sonar/db/version/v63/PopulateUuidColumnOfEvents.java delete mode 100644 sonar-db/src/main/java/org/sonar/db/version/v63/PopulateUuidColumnOnEvents.java create mode 100644 sonar-db/src/test/java/org/sonar/db/version/v63/PopulateUuidColumnOfEventsTest.java create mode 100644 sonar-db/src/test/resources/org/sonar/db/version/v63/PopulateUuidColumnOfEventsTest/in_progress_events.sql (limited to 'sonar-db/src') 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/PopulateUuidColumnOfEvents.java b/sonar-db/src/main/java/org/sonar/db/version/v63/PopulateUuidColumnOfEvents.java new file mode 100644 index 00000000000..698d29f188e --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v63/PopulateUuidColumnOfEvents.java @@ -0,0 +1,56 @@ +/* + * 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.core.util.UuidFactory; +import org.sonar.db.Database; +import org.sonar.db.version.BaseDataChange; +import org.sonar.db.version.MassUpdate; +import org.sonar.db.version.Select; +import org.sonar.db.version.SqlStatement; + +public class PopulateUuidColumnOfEvents extends BaseDataChange { + + private final UuidFactory uuidFactory; + + public PopulateUuidColumnOfEvents(Database db, UuidFactory uuidFactory) { + super(db); + this.uuidFactory = uuidFactory; + } + + @Override + public void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("SELECT e.id from events e where e.uuid is null"); + massUpdate.update("UPDATE events SET uuid=? WHERE id=?"); + massUpdate.rowPluralName("events"); + massUpdate.execute(this::handle); + } + + private boolean handle(Select.Row row, SqlStatement update) throws SQLException { + long id = row.getLong(1); + update.setString(1, uuidFactory.create()); + update.setLong(2, id); + return true; + } + +} 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/PopulateUuidColumnOnEvents.java deleted file mode 100644 index 380cac0068c..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/version/v63/PopulateUuidColumnOnEvents.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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.core.util.UuidFactory; -import org.sonar.db.Database; -import org.sonar.db.version.BaseDataChange; -import org.sonar.db.version.MassUpdate; -import org.sonar.db.version.Select; -import org.sonar.db.version.SqlStatement; - -public class PopulateUuidColumnOnEvents extends BaseDataChange { - - private final UuidFactory uuidFactory; - - public PopulateUuidColumnOnEvents(Database db, UuidFactory uuidFactory) { - super(db); - this.uuidFactory = uuidFactory; - } - - @Override - public void execute(Context context) throws SQLException { - MassUpdate massUpdate = context.prepareMassUpdate(); - massUpdate.select("SELECT e.id from events e where e.uuid is null"); - massUpdate.update("UPDATE events SET uuid=? WHERE id=?"); - massUpdate.rowPluralName("events"); - massUpdate.execute(this::handle); - } - - private boolean handle(Select.Row row, SqlStatement update) throws SQLException { - long id = row.getLong(1); - update.setString(1, uuidFactory.create()); - update.setLong(2, id); - return true; - } - -} 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> rows = db.select("select uuid from events where uuid is not null"); + Set 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) +); -- cgit v1.2.3