From d94ddd98de1e2b201d3e6183e925bc9ce2c5ff24 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Mon, 20 Apr 2020 15:24:55 -0500 Subject: [PATCH] SONAR-13221 File sources --- .../source/PersistFileSourcesStep.java | 2 + .../filemove/FileMoveDetectionStepTest.java | 2 + .../scm/ScmInfoDbLoaderTest.java | 2 + .../source/PersistFileSourcesStepTest.java | 3 + .../org/sonar/db/source/FileSourceDto.java | 12 +-- .../org/sonar/db/source/FileSourceMapper.xml | 10 ++- .../sonar/db/source/FileSourceDaoTest.java | 13 ++- .../sonar/db/source/FileSourceDtoTest.java | 6 +- .../org/sonar/db/source/FileSourceTester.java | 5 +- ...maryKeyOnUuidColumnOfFileSourcesTable.java | 38 +++++++++ .../AddUuidColumnToFileSourcesTable.java | 31 +++++++ .../DropIdColumnOfFileSourcesTable.java | 31 +++++++ ...rimaryKeyOnIdColumnOfFileSourcesTable.java | 32 +++++++ .../MakeFileSourcesUuidColumnNotNullable.java | 50 +++++++++++ .../filesources/PopulateFileSourcesUuid.java | 50 +++++++++++ ...KeyOnUuidColumnOfFileSourcesTableTest.java | 50 +++++++++++ .../AddUuidColumnToFileSourcesTableTest.java | 69 +++++++++++++++ .../DropIdColumnOfFileSourcesTableTest.java | 51 +++++++++++ ...ryKeyOnIdColumnOfFileSourcesTableTest.java | 56 +++++++++++++ ...eFileSourcesUuidColumnNotNullableTest.java | 42 ++++++++++ .../PopulateFileSourcesUuidTest.java | 84 +++++++++++++++++++ .../schema.sql | 18 ++++ .../schema.sql | 18 ++++ .../schema.sql | 19 +++++ .../schema.sql | 19 +++++ .../schema.sql | 19 +++++ .../PopulateFileSourcesUuidTest/schema.sql | 19 +++++ .../server/batch/ProjectDataLoaderTest.java | 4 +- .../server/source/SourceServiceTest.java | 3 +- .../server/source/ws/IndexActionTest.java | 2 + .../server/source/ws/LinesActionTest.java | 4 + .../sonar/server/source/ws/ScmActionTest.java | 7 ++ 32 files changed, 752 insertions(+), 19 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddPrimaryKeyOnUuidColumnOfFileSourcesTable.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddUuidColumnToFileSourcesTable.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropIdColumnOfFileSourcesTable.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropPrimaryKeyOnIdColumnOfFileSourcesTable.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/MakeFileSourcesUuidColumnNotNullable.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/PopulateFileSourcesUuid.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddPrimaryKeyOnUuidColumnOfFileSourcesTableTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddUuidColumnToFileSourcesTableTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropIdColumnOfFileSourcesTableTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropPrimaryKeyOnIdColumnOfFileSourcesTableTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/MakeFileSourcesUuidColumnNotNullableTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/PopulateFileSourcesUuidTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/AddPrimaryKeyOnUuidColumnOfFileSourcesTableTest/schema.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/AddUuidColumnToFileSourcesTableTest/schema.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/DropIdColumnOfFileSourcesTableTest/schema.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/DropPrimaryKeyOnIdColumnOfFileSourcesTableTest/schema.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/MakeFileSourcesUuidColumnNotNullableTest/schema.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/PopulateFileSourcesUuidTest/schema.sql diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/PersistFileSourcesStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/PersistFileSourcesStep.java index 6c92a9deae4..37f5d3c52f4 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/PersistFileSourcesStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/PersistFileSourcesStep.java @@ -35,6 +35,7 @@ import org.sonar.ce.task.projectanalysis.component.TreeRootHolder; import org.sonar.ce.task.projectanalysis.component.TypeAwareVisitorAdapter; import org.sonar.ce.task.projectanalysis.scm.Changeset; import org.sonar.ce.task.step.ComputationStep; +import org.sonar.core.util.Uuids; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.protobuf.DbFileSources; @@ -115,6 +116,7 @@ public class PersistFileSourcesStep implements ComputationStep { FileSourceDto previousDto = previousFileSourcesByUuid.get(file.getUuid()); if (previousDto == null) { FileSourceDto dto = new FileSourceDto() + .setUuid(Uuids.create()) .setProjectUuid(projectUuid) .setFileUuid(file.getUuid()) .setBinaryData(binaryData) diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/filemove/FileMoveDetectionStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/filemove/FileMoveDetectionStepTest.java index c50515ce4ce..3b288083b51 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/filemove/FileMoveDetectionStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/filemove/FileMoveDetectionStepTest.java @@ -46,6 +46,7 @@ import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule; import org.sonar.ce.task.projectanalysis.source.SourceLinesHashRepository; import org.sonar.ce.task.step.TestComputationStepContext; import org.sonar.core.hash.SourceLineHashesComputer; +import org.sonar.core.util.Uuids; import org.sonar.db.DbClient; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; @@ -594,6 +595,7 @@ public class FileMoveDetectionStepTest { stream(content).forEach(linesHashesComputer::addLine); } FileSourceDto fileSourceDto = new FileSourceDto() + .setUuid(Uuids.createFast()) .setFileUuid(file.uuid()) .setProjectUuid(file.projectUuid()) .setLineHashes(linesHashesComputer.getLineHashes()); diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/scm/ScmInfoDbLoaderTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/scm/ScmInfoDbLoaderTest.java index 269878f2bc9..677edb370b8 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/scm/ScmInfoDbLoaderTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/scm/ScmInfoDbLoaderTest.java @@ -37,6 +37,7 @@ import org.sonar.ce.task.projectanalysis.component.Component; import org.sonar.ce.task.projectanalysis.component.ReferenceBranchComponentUuids; import org.sonar.ce.task.projectanalysis.filemove.MutableMovedFilesRepositoryRule; import org.sonar.core.hash.SourceHashComputer; +import org.sonar.core.util.Uuids; import org.sonar.db.DbTester; import org.sonar.db.component.BranchType; import org.sonar.db.protobuf.DbFileSources; @@ -182,6 +183,7 @@ public class ScmInfoDbLoaderTest { builder.setScmRevision(revision); } dbTester.getDbClient().fileSourceDao().insert(dbTester.getSession(), new FileSourceDto() + .setUuid(Uuids.createFast()) .setLineHashes(Collections.singletonList("lineHash")) .setFileUuid(fileUuid) .setProjectUuid("PROJECT_UUID") diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/PersistFileSourcesStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/PersistFileSourcesStepTest.java index 8c5d99f11da..af384066289 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/PersistFileSourcesStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/PersistFileSourcesStepTest.java @@ -38,6 +38,7 @@ import org.sonar.ce.task.projectanalysis.scm.Changeset; import org.sonar.ce.task.projectanalysis.step.BaseStepTest; import org.sonar.ce.task.step.ComputationStep; import org.sonar.ce.task.step.TestComputationStepContext; +import org.sonar.core.util.Uuids; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; @@ -328,6 +329,7 @@ public class PersistFileSourcesStepTest extends BaseStepTest { // Existing sources long past = 150000L; dbClient.fileSourceDao().insert(dbTester.getSession(), new FileSourceDto() + .setUuid(Uuids.createFast()) .setProjectUuid(PROJECT_UUID) .setFileUuid(FILE1_UUID) .setSrcHash("5b4bd9815cdb17b8ceae19eb1810c34c") @@ -421,6 +423,7 @@ public class PersistFileSourcesStepTest extends BaseStepTest { String dataHash = DigestUtils.md5Hex(data); FileSourceDto dto = new FileSourceDto() + .setUuid(Uuids.createFast()) .setProjectUuid(PROJECT_UUID) .setFileUuid(FILE1_UUID) .setSrcHash("sourceHash") diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/source/FileSourceDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/source/FileSourceDto.java index 69429b6d9b8..56ce6a9d0ca 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/source/FileSourceDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/source/FileSourceDto.java @@ -46,7 +46,7 @@ public class FileSourceDto { public static final Splitter LINES_HASHES_SPLITTER = on('\n'); public static final int LINE_COUNT_NOT_POPULATED = -1; - private Long id; + private String uuid; private String projectUuid; private String fileUuid; private long createdAt; @@ -80,12 +80,12 @@ public class FileSourceDto { return this; } - public Long getId() { - return id; + public String getUuid() { + return uuid; } - public FileSourceDto setId(Long id) { - this.id = id; + public FileSourceDto setUuid(String uuid) { + this.uuid = uuid; return this; } @@ -125,7 +125,7 @@ public class FileSourceDto { return decodeRegularSourceData(binaryData); } catch (IOException e) { throw new IllegalStateException( - format("Fail to decompress and deserialize source data [id=%s,fileUuid=%s,projectUuid=%s]", id, fileUuid, projectUuid), + format("Fail to decompress and deserialize source data [uuid=%s,fileUuid=%s,projectUuid=%s]", uuid, fileUuid, projectUuid), e); } } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/source/FileSourceMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/source/FileSourceMapper.xml index 3a8658f30aa..ba90ab521a9 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/source/FileSourceMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/source/FileSourceMapper.xml @@ -6,7 +6,7 @@ select - id, + uuid, file_uuid as fileUuid, data_hash as dataHash, src_hash as srcHash, @@ -63,9 +63,10 @@ file_uuid = #{fileUuid,jdbcType=VARCHAR} - + insert into file_sources ( + uuid, project_uuid, file_uuid, created_at, @@ -80,6 +81,7 @@ ) values ( + #{uuid,jdbcType=VARCHAR}, #{projectUuid,jdbcType=VARCHAR}, #{fileUuid,jdbcType=VARCHAR}, #{createdAt,jdbcType=BIGINT}, @@ -107,7 +109,7 @@ src_hash = #{srcHash,jdbcType=VARCHAR}, revision = #{revision,jdbcType=VARCHAR} where - id = #{id,jdbcType=INTEGER} + uuid = #{uuid,jdbcType=VARCHAR} diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/source/FileSourceDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/source/FileSourceDaoTest.java index 171bd95290b..0acd7441556 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/source/FileSourceDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/source/FileSourceDaoTest.java @@ -37,6 +37,7 @@ import org.apache.ibatis.session.ResultHandler; import org.junit.Rule; import org.junit.Test; import org.sonar.api.utils.System2; +import org.sonar.core.util.Uuids; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; @@ -108,6 +109,7 @@ public class FileSourceDaoTest { @Test public void insert() { FileSourceDto expected = new FileSourceDto() + .setUuid(Uuids.createFast()) .setProjectUuid("PRJ_UUID") .setFileUuid("FILE2_UUID") .setBinaryData("FILE2_BINARY_DATA".getBytes()) @@ -139,6 +141,7 @@ public class FileSourceDaoTest { @Test public void insert_does_not_fail_on_FileSourceDto_with_only_non_nullable_data() { FileSourceDto fileSourceDto = new FileSourceDto() + .setUuid(Uuids.createFast()) .setProjectUuid("Foo") .setFileUuid("Bar") .setCreatedAt(1500000000000L) @@ -150,6 +153,7 @@ public class FileSourceDaoTest { @Test public void selectSourceByFileUuid_reads_source_without_line_hashes() { FileSourceDto fileSourceDto = new FileSourceDto() + .setUuid(Uuids.createFast()) .setProjectUuid("Foo") .setFileUuid("Bar") .setCreatedAt(1500000000000L) @@ -166,6 +170,7 @@ public class FileSourceDaoTest { @Test public void selectLineHashes_does_not_fail_when_lineshashes_is_null() { underTest.insert(dbSession, new FileSourceDto() + .setUuid(Uuids.createFast()) .setProjectUuid("PRJ_UUID") .setFileUuid("FILE2_UUID") .setBinaryData("FILE2_BINARY_DATA".getBytes()) @@ -182,6 +187,7 @@ public class FileSourceDaoTest { @Test public void selectLineHashesVersion_returns_without_significant_code_by_default() { underTest.insert(dbSession, new FileSourceDto() + .setUuid(Uuids.createFast()) .setProjectUuid("PRJ_UUID") .setFileUuid("FILE2_UUID") .setBinaryData("FILE2_BINARY_DATA".getBytes()) @@ -199,6 +205,7 @@ public class FileSourceDaoTest { @Test public void selectLineHashesVersion_succeeds() { underTest.insert(dbSession, new FileSourceDto() + .setUuid(Uuids.createFast()) .setProjectUuid("PRJ_UUID") .setFileUuid("FILE2_UUID") .setBinaryData("FILE2_BINARY_DATA".getBytes()) @@ -217,6 +224,7 @@ public class FileSourceDaoTest { @Test public void readLineHashesStream_does_not_fail_when_lineshashes_is_null() { underTest.insert(dbSession, new FileSourceDto() + .setUuid(Uuids.createFast()) .setProjectUuid("PRJ_UUID") .setFileUuid("FILE2_UUID") .setBinaryData("FILE2_BINARY_DATA".getBytes()) @@ -338,7 +346,7 @@ public class FileSourceDaoTest { FileSourceDto expected = dbTester.fileSources().insertFileSource(file); underTest.update(dbSession, new FileSourceDto() - .setId(expected.getId()) + .setUuid(expected.getUuid()) .setProjectUuid("PRJ_UUID") .setFileUuid("FILE1_UUID") .setBinaryData("updated data".getBytes()) @@ -369,6 +377,7 @@ public class FileSourceDaoTest { public void update_to_no_line_hashes() { ImmutableList lineHashes = of("a", "b", "c"); FileSourceDto fileSourceDto = new FileSourceDto() + .setUuid(Uuids.createFast()) .setProjectUuid("Foo") .setFileUuid("Bar") .setLineHashes(lineHashes) @@ -381,7 +390,7 @@ public class FileSourceDaoTest { assertThat(resBefore.getLineCount()).isEqualTo(lineHashes.size()); assertThat(resBefore.getLineHashes()).isEqualTo(lineHashes); - fileSourceDto.setId(resBefore.getId()); + fileSourceDto.setUuid(resBefore.getUuid()); fileSourceDto.setLineHashes(emptyList()); underTest.update(dbSession, fileSourceDto); dbSession.commit(); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/source/FileSourceDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/source/FileSourceDtoTest.java index c62a04c559b..1d04df016b7 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/source/FileSourceDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/source/FileSourceDtoTest.java @@ -44,17 +44,17 @@ public class FileSourceDtoTest { @Test public void getSourceData_throws_ISE_with_id_fileUuid_and_projectUuid_in_message_when_data_cant_be_read() { - long id = 12L; + String uuid = "uuid"; String fileUuid = "file uuid"; String projectUuid = "project uuid"; FileSourceDto underTest = new FileSourceDto() .setBinaryData(new byte[] {1, 2, 3, 4, 5}) - .setId(id) + .setUuid(uuid) .setFileUuid(fileUuid) .setProjectUuid(projectUuid); expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Fail to decompress and deserialize source data [id=" + id + ",fileUuid=" + fileUuid + ",projectUuid=" + projectUuid + "]"); + expectedException.expectMessage("Fail to decompress and deserialize source data [uuid=" + uuid + ",fileUuid=" + fileUuid + ",projectUuid=" + projectUuid + "]"); underTest.getSourceData(); } diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/source/FileSourceTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/source/FileSourceTester.java index 5b3bfba8b0f..09e5e57d683 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/source/FileSourceTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/source/FileSourceTester.java @@ -25,6 +25,7 @@ import java.util.Random; import java.util.function.Consumer; import java.util.stream.IntStream; import org.apache.commons.lang.math.RandomUtils; +import org.sonar.core.util.Uuids; import org.sonar.core.util.stream.MoreCollectors; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; @@ -43,6 +44,7 @@ public class FileSourceTester { @SafeVarargs public final FileSourceDto insertFileSource(ComponentDto file, Consumer... dtoPopulators) { FileSourceDto dto = new FileSourceDto() + .setUuid(Uuids.createFast()) .setProjectUuid(file.projectUuid()) .setFileUuid(file.uuid()) .setSrcHash(randomAlphanumeric(50)) @@ -55,13 +57,14 @@ public class FileSourceTester { Arrays.stream(dtoPopulators).forEach(c -> c.accept(dto)); db.getDbClient().fileSourceDao().insert(db.getSession(), dto); db.commit(); - dto.setId(db.getDbClient().fileSourceDao().selectByFileUuid(db.getSession(), dto.getFileUuid()).getId()); + dto.setUuid(db.getDbClient().fileSourceDao().selectByFileUuid(db.getSession(), dto.getFileUuid()).getUuid()); return dto; } @SafeVarargs public final FileSourceDto insertFileSource(ComponentDto file, int numLines, Consumer... dtoPopulators) { FileSourceDto dto = new FileSourceDto() + .setUuid(Uuids.createFast()) .setProjectUuid(file.projectUuid()) .setFileUuid(file.uuid()) .setSrcHash(randomAlphanumeric(50)) diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddPrimaryKeyOnUuidColumnOfFileSourcesTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddPrimaryKeyOnUuidColumnOfFileSourcesTable.java new file mode 100644 index 00000000000..7f290eccd1b --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddPrimaryKeyOnUuidColumnOfFileSourcesTable.java @@ -0,0 +1,38 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.DdlChange; +import org.sonar.server.platform.db.migration.version.v83.util.AddPrimaryKeyBuilder; + +public class AddPrimaryKeyOnUuidColumnOfFileSourcesTable extends DdlChange { + + public AddPrimaryKeyOnUuidColumnOfFileSourcesTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AddPrimaryKeyBuilder("file_sources", "uuid").build()); + } + +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddUuidColumnToFileSourcesTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddUuidColumnToFileSourcesTable.java new file mode 100644 index 00000000000..79b6bd695fb --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddUuidColumnToFileSourcesTable.java @@ -0,0 +1,31 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.version.v83.common.AddUuidColumnToTable; + +public class AddUuidColumnToFileSourcesTable extends AddUuidColumnToTable { + private static final String TABLE = "file_sources"; + + public AddUuidColumnToFileSourcesTable(Database db) { + super(db, TABLE); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropIdColumnOfFileSourcesTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropIdColumnOfFileSourcesTable.java new file mode 100644 index 00000000000..741bf99a0e3 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropIdColumnOfFileSourcesTable.java @@ -0,0 +1,31 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.version.v83.common.DropIdColumn; + +public class DropIdColumnOfFileSourcesTable extends DropIdColumn { + private static final String TABLE = "file_sources"; + + public DropIdColumnOfFileSourcesTable(Database db) { + super(db, TABLE); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropPrimaryKeyOnIdColumnOfFileSourcesTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropPrimaryKeyOnIdColumnOfFileSourcesTable.java new file mode 100644 index 00000000000..8b03c71be40 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropPrimaryKeyOnIdColumnOfFileSourcesTable.java @@ -0,0 +1,32 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.version.v83.common.DropPrimaryKeyOnIdColumn; +import org.sonar.server.platform.db.migration.version.v83.util.DropPrimaryKeySqlGenerator; + +public class DropPrimaryKeyOnIdColumnOfFileSourcesTable extends DropPrimaryKeyOnIdColumn { + private static final String TABLE_NAME = "file_sources"; + + public DropPrimaryKeyOnIdColumnOfFileSourcesTable(Database db, DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator) { + super(db, dropPrimaryKeySqlGenerator, TABLE_NAME); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/MakeFileSourcesUuidColumnNotNullable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/MakeFileSourcesUuidColumnNotNullable.java new file mode 100644 index 00000000000..efd6b87eeb3 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/MakeFileSourcesUuidColumnNotNullable.java @@ -0,0 +1,50 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.def.VarcharColumnDef; +import org.sonar.server.platform.db.migration.sql.AlterColumnsBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class MakeFileSourcesUuidColumnNotNullable extends DdlChange { + private static final String TABLE = "file_sources"; + + private static final VarcharColumnDef uuidColumnDefinition = newVarcharColumnDefBuilder() + .setColumnName("uuid") + .setIsNullable(false) + .setDefaultValue(null) + .setLimit(VarcharColumnDef.UUID_SIZE) + .build(); + + public MakeFileSourcesUuidColumnNotNullable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AlterColumnsBuilder(getDialect(), TABLE) + .updateColumn(uuidColumnDefinition) + .build()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/PopulateFileSourcesUuid.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/PopulateFileSourcesUuid.java new file mode 100644 index 00000000000..b292f040284 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/filesources/PopulateFileSourcesUuid.java @@ -0,0 +1,50 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import java.sql.SQLException; +import org.sonar.core.util.UuidFactory; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.DataChange; +import org.sonar.server.platform.db.migration.step.MassUpdate; + +public class PopulateFileSourcesUuid extends DataChange { + + private final UuidFactory uuidFactory; + + public PopulateFileSourcesUuid(Database db, UuidFactory uuidFactory) { + super(db); + this.uuidFactory = uuidFactory; + } + + @Override + protected void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + + massUpdate.select("select id from file_sources where uuid is null order by id asc"); + massUpdate.update("update file_sources set uuid = ? where id = ?"); + + massUpdate.execute((row, update) -> { + update.setString(1, uuidFactory.create()); + update.setLong(2, row.getLong(1)); + return true; + }); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddPrimaryKeyOnUuidColumnOfFileSourcesTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddPrimaryKeyOnUuidColumnOfFileSourcesTableTest.java new file mode 100644 index 00000000000..d74453a6c9e --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddPrimaryKeyOnUuidColumnOfFileSourcesTableTest.java @@ -0,0 +1,50 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class AddPrimaryKeyOnUuidColumnOfFileSourcesTableTest { + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(AddPrimaryKeyOnUuidColumnOfFileSourcesTableTest.class, "schema.sql"); + + private DdlChange underTest = new AddPrimaryKeyOnUuidColumnOfFileSourcesTable(db.database()); + + @Test + public void execute() throws SQLException { + underTest.execute(); + + db.assertPrimaryKey("file_sources", "pk_file_sources", "uuid"); + } + + @Test + public void migration_is_not_re_entrant() throws SQLException { + underTest.execute(); + + assertThatThrownBy(() -> underTest.execute()).isInstanceOf(IllegalStateException.class); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddUuidColumnToFileSourcesTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddUuidColumnToFileSourcesTableTest.java new file mode 100644 index 00000000000..d8739496423 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/AddUuidColumnToFileSourcesTableTest.java @@ -0,0 +1,69 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import java.sql.SQLException; +import java.sql.Types; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.core.util.UuidFactoryFast; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.assertj.core.api.Assertions.assertThat; + +public class AddUuidColumnToFileSourcesTableTest { + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(AddUuidColumnToFileSourcesTableTest.class, "schema.sql"); + + private DdlChange underTest = new AddUuidColumnToFileSourcesTable(db.database()); + + private UuidFactoryFast uuidFactory = UuidFactoryFast.getInstance(); + + @Before + public void setup() { + insertFileSources(1L); + insertFileSources(2L); + insertFileSources(3L); + } + + @Test + public void add_uuid_column() throws SQLException { + underTest.execute(); + + db.assertColumnDefinition("file_sources", "uuid", Types.VARCHAR, 40, true); + + assertThat(db.countRowsOfTable("file_sources")) + .isEqualTo(3); + } + + private void insertFileSources(Long id) { + db.executeInsert("file_sources", + "id", id, + "project_uuid", uuidFactory.create(), + "file_uuid", uuidFactory.create(), + "line_count", id + 1, + "created_at", id + 2, + "updated_at", id + 3); + } + +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropIdColumnOfFileSourcesTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropIdColumnOfFileSourcesTableTest.java new file mode 100644 index 00000000000..b0132fb007b --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropIdColumnOfFileSourcesTableTest.java @@ -0,0 +1,51 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class DropIdColumnOfFileSourcesTableTest { + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(DropIdColumnOfFileSourcesTableTest.class, "schema.sql"); + + private DdlChange underTest = new DropIdColumnOfFileSourcesTable(db.database()); + + @Test + public void execute() throws SQLException { + underTest.execute(); + + db.assertColumnDoesNotExist("file_sources", "id"); + } + + @Test + public void migration_is_not_re_entrant() throws SQLException { + underTest.execute(); + + assertThatThrownBy(() -> underTest.execute()).isInstanceOf(IllegalStateException.class); + } + +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropPrimaryKeyOnIdColumnOfFileSourcesTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropPrimaryKeyOnIdColumnOfFileSourcesTableTest.java new file mode 100644 index 00000000000..7fcf0f3c7d3 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/DropPrimaryKeyOnIdColumnOfFileSourcesTableTest.java @@ -0,0 +1,56 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; +import org.sonar.server.platform.db.migration.version.v83.util.DropPrimaryKeySqlGenerator; +import org.sonar.server.platform.db.migration.version.v83.util.SqlHelper; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class DropPrimaryKeyOnIdColumnOfFileSourcesTableTest { + + private static final String TABLE_NAME = "file_sources"; + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(DropPrimaryKeyOnIdColumnOfFileSourcesTableTest.class, "schema.sql"); + + private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), new SqlHelper(db.database())); + + private DdlChange underTest = new DropPrimaryKeyOnIdColumnOfFileSourcesTable(db.database(), dropPrimaryKeySqlGenerator); + + @Test + public void execute() throws SQLException { + underTest.execute(); + + db.assertNoPrimaryKey(TABLE_NAME); + } + + @Test + public void migration_is_not_re_entrant() throws SQLException { + underTest.execute(); + + assertThatThrownBy(() -> underTest.execute()).isInstanceOf(IllegalStateException.class); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/MakeFileSourcesUuidColumnNotNullableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/MakeFileSourcesUuidColumnNotNullableTest.java new file mode 100644 index 00000000000..111984bc7f1 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/MakeFileSourcesUuidColumnNotNullableTest.java @@ -0,0 +1,42 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.MigrationStep; + +import static java.sql.Types.VARCHAR; + +public class MakeFileSourcesUuidColumnNotNullableTest { + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(MakeFileSourcesUuidColumnNotNullableTest.class, "schema.sql"); + + private MigrationStep underTest = new MakeFileSourcesUuidColumnNotNullable(db.database()); + + @Test + public void uuid_column_is_not_nullable() throws SQLException { + underTest.execute(); + + db.assertColumnDefinition("file_sources", "uuid", VARCHAR, null, false); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/PopulateFileSourcesUuidTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/PopulateFileSourcesUuidTest.java new file mode 100644 index 00000000000..dde090da5bc --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/filesources/PopulateFileSourcesUuidTest.java @@ -0,0 +1,84 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.filesources; + +import java.sql.SQLException; +import java.util.Objects; +import java.util.stream.Collectors; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.core.util.UuidFactory; +import org.sonar.core.util.UuidFactoryFast; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DataChange; + +import static org.assertj.core.api.Assertions.assertThat; + +public class PopulateFileSourcesUuidTest { + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(PopulateFileSourcesUuidTest.class, "schema.sql"); + + private UuidFactory uuidFactory = UuidFactoryFast.getInstance(); + private DataChange underTest = new PopulateFileSourcesUuid(db.database(), uuidFactory); + + @Test + public void populate_uuids() throws SQLException { + insertFileSources(1L); + insertFileSources(2L); + insertFileSources(3L); + + underTest.execute(); + + verifyUuidsAreNotNull(); + } + + @Test + public void migration_is_reentrant() throws SQLException { + insertFileSources(1L); + insertFileSources(2L); + insertFileSources(3L); + + underTest.execute(); + // re-entrant + underTest.execute(); + + verifyUuidsAreNotNull(); + } + + private void verifyUuidsAreNotNull() { + assertThat(db.select("select uuid from file_sources") + .stream() + .map(row -> row.get("UUID")) + .filter(Objects::isNull) + .collect(Collectors.toList())).isEmpty(); + } + + private void insertFileSources(Long id) { + db.executeInsert("file_sources", + "id", id, + "uuid", uuidFactory.create(), + "project_uuid", uuidFactory.create(), + "file_uuid", uuidFactory.create(), + "line_count", id + 1, + "created_at", id + 2, + "updated_at", id + 3); + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/AddPrimaryKeyOnUuidColumnOfFileSourcesTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/AddPrimaryKeyOnUuidColumnOfFileSourcesTableTest/schema.sql new file mode 100644 index 00000000000..dfb2d817bc7 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/AddPrimaryKeyOnUuidColumnOfFileSourcesTableTest/schema.sql @@ -0,0 +1,18 @@ +CREATE TABLE "FILE_SOURCES"( + "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1), + "UUID" VARCHAR(40) NOT NULL, + "PROJECT_UUID" VARCHAR(50) NOT NULL, + "FILE_UUID" VARCHAR(50) NOT NULL, + "LINE_HASHES" CLOB(2147483647), + "LINE_HASHES_VERSION" INTEGER, + "DATA_HASH" VARCHAR(50), + "SRC_HASH" VARCHAR(50), + "REVISION" VARCHAR(100), + "LINE_COUNT" INTEGER NOT NULL, + "BINARY_DATA" BLOB, + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL +); +CREATE UNIQUE INDEX "FILE_SOURCES_FILE_UUID" ON "FILE_SOURCES"("FILE_UUID"); +CREATE INDEX "FILE_SOURCES_PROJECT_UUID" ON "FILE_SOURCES"("PROJECT_UUID"); +CREATE INDEX "FILE_SOURCES_UPDATED_AT" ON "FILE_SOURCES"("UPDATED_AT"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/AddUuidColumnToFileSourcesTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/AddUuidColumnToFileSourcesTableTest/schema.sql new file mode 100644 index 00000000000..179847ee2ce --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/AddUuidColumnToFileSourcesTableTest/schema.sql @@ -0,0 +1,18 @@ +CREATE TABLE "FILE_SOURCES"( + "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1), + "PROJECT_UUID" VARCHAR(50) NOT NULL, + "FILE_UUID" VARCHAR(50) NOT NULL, + "LINE_HASHES" CLOB(2147483647), + "LINE_HASHES_VERSION" INTEGER, + "DATA_HASH" VARCHAR(50), + "SRC_HASH" VARCHAR(50), + "REVISION" VARCHAR(100), + "LINE_COUNT" INTEGER NOT NULL, + "BINARY_DATA" BLOB, + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL +); +ALTER TABLE "FILE_SOURCES" ADD CONSTRAINT "PK_FILE_SOURCES" PRIMARY KEY("ID"); +CREATE UNIQUE INDEX "FILE_SOURCES_FILE_UUID" ON "FILE_SOURCES"("FILE_UUID"); +CREATE INDEX "FILE_SOURCES_PROJECT_UUID" ON "FILE_SOURCES"("PROJECT_UUID"); +CREATE INDEX "FILE_SOURCES_UPDATED_AT" ON "FILE_SOURCES"("UPDATED_AT"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/DropIdColumnOfFileSourcesTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/DropIdColumnOfFileSourcesTableTest/schema.sql new file mode 100644 index 00000000000..3dab0132b33 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/DropIdColumnOfFileSourcesTableTest/schema.sql @@ -0,0 +1,19 @@ +CREATE TABLE "FILE_SOURCES"( + "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1), + "UUID" VARCHAR(40) NOT NULL, + "PROJECT_UUID" VARCHAR(50) NOT NULL, + "FILE_UUID" VARCHAR(50) NOT NULL, + "LINE_HASHES" CLOB(2147483647), + "LINE_HASHES_VERSION" INTEGER, + "DATA_HASH" VARCHAR(50), + "SRC_HASH" VARCHAR(50), + "REVISION" VARCHAR(100), + "LINE_COUNT" INTEGER NOT NULL, + "BINARY_DATA" BLOB, + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL +); +ALTER TABLE "FILE_SOURCES" ADD CONSTRAINT "PK_FILE_SOURCES" PRIMARY KEY("UUID"); +CREATE UNIQUE INDEX "FILE_SOURCES_FILE_UUID" ON "FILE_SOURCES"("FILE_UUID"); +CREATE INDEX "FILE_SOURCES_PROJECT_UUID" ON "FILE_SOURCES"("PROJECT_UUID"); +CREATE INDEX "FILE_SOURCES_UPDATED_AT" ON "FILE_SOURCES"("UPDATED_AT"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/DropPrimaryKeyOnIdColumnOfFileSourcesTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/DropPrimaryKeyOnIdColumnOfFileSourcesTableTest/schema.sql new file mode 100644 index 00000000000..a6a2a841f83 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/DropPrimaryKeyOnIdColumnOfFileSourcesTableTest/schema.sql @@ -0,0 +1,19 @@ +CREATE TABLE "FILE_SOURCES"( + "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1), + "UUID" VARCHAR(40) NOT NULL, + "PROJECT_UUID" VARCHAR(50) NOT NULL, + "FILE_UUID" VARCHAR(50) NOT NULL, + "LINE_HASHES" CLOB(2147483647), + "LINE_HASHES_VERSION" INTEGER, + "DATA_HASH" VARCHAR(50), + "SRC_HASH" VARCHAR(50), + "REVISION" VARCHAR(100), + "LINE_COUNT" INTEGER NOT NULL, + "BINARY_DATA" BLOB, + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL +); +ALTER TABLE "FILE_SOURCES" ADD CONSTRAINT "PK_FILE_SOURCES" PRIMARY KEY("ID"); +CREATE UNIQUE INDEX "FILE_SOURCES_FILE_UUID" ON "FILE_SOURCES"("FILE_UUID"); +CREATE INDEX "FILE_SOURCES_PROJECT_UUID" ON "FILE_SOURCES"("PROJECT_UUID"); +CREATE INDEX "FILE_SOURCES_UPDATED_AT" ON "FILE_SOURCES"("UPDATED_AT"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/MakeFileSourcesUuidColumnNotNullableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/MakeFileSourcesUuidColumnNotNullableTest/schema.sql new file mode 100644 index 00000000000..559418594f8 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/MakeFileSourcesUuidColumnNotNullableTest/schema.sql @@ -0,0 +1,19 @@ +CREATE TABLE "FILE_SOURCES"( + "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1), + "UUID" VARCHAR(40), + "PROJECT_UUID" VARCHAR(50) NOT NULL, + "FILE_UUID" VARCHAR(50) NOT NULL, + "LINE_HASHES" CLOB(2147483647), + "LINE_HASHES_VERSION" INTEGER, + "DATA_HASH" VARCHAR(50), + "SRC_HASH" VARCHAR(50), + "REVISION" VARCHAR(100), + "LINE_COUNT" INTEGER NOT NULL, + "BINARY_DATA" BLOB, + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL +); +ALTER TABLE "FILE_SOURCES" ADD CONSTRAINT "PK_FILE_SOURCES" PRIMARY KEY("ID"); +CREATE UNIQUE INDEX "FILE_SOURCES_FILE_UUID" ON "FILE_SOURCES"("FILE_UUID"); +CREATE INDEX "FILE_SOURCES_PROJECT_UUID" ON "FILE_SOURCES"("PROJECT_UUID"); +CREATE INDEX "FILE_SOURCES_UPDATED_AT" ON "FILE_SOURCES"("UPDATED_AT"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/PopulateFileSourcesUuidTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/PopulateFileSourcesUuidTest/schema.sql new file mode 100644 index 00000000000..559418594f8 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/filesources/PopulateFileSourcesUuidTest/schema.sql @@ -0,0 +1,19 @@ +CREATE TABLE "FILE_SOURCES"( + "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1), + "UUID" VARCHAR(40), + "PROJECT_UUID" VARCHAR(50) NOT NULL, + "FILE_UUID" VARCHAR(50) NOT NULL, + "LINE_HASHES" CLOB(2147483647), + "LINE_HASHES_VERSION" INTEGER, + "DATA_HASH" VARCHAR(50), + "SRC_HASH" VARCHAR(50), + "REVISION" VARCHAR(100), + "LINE_COUNT" INTEGER NOT NULL, + "BINARY_DATA" BLOB, + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL +); +ALTER TABLE "FILE_SOURCES" ADD CONSTRAINT "PK_FILE_SOURCES" PRIMARY KEY("ID"); +CREATE UNIQUE INDEX "FILE_SOURCES_FILE_UUID" ON "FILE_SOURCES"("FILE_UUID"); +CREATE INDEX "FILE_SOURCES_PROJECT_UUID" ON "FILE_SOURCES"("PROJECT_UUID"); +CREATE INDEX "FILE_SOURCES_UPDATED_AT" ON "FILE_SOURCES"("UPDATED_AT"); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/batch/ProjectDataLoaderTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/batch/ProjectDataLoaderTest.java index e3437f4bb48..c435c7daac8 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/batch/ProjectDataLoaderTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/batch/ProjectDataLoaderTest.java @@ -25,7 +25,7 @@ import org.junit.rules.ExpectedException; import org.sonar.api.config.internal.MapSettings; import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Scopes; -import org.sonar.api.web.UserRole; +import org.sonar.core.util.Uuids; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; @@ -51,7 +51,6 @@ import static org.junit.Assert.fail; import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION; import static org.sonar.db.component.ComponentTesting.newFileDto; import static org.sonar.db.component.ComponentTesting.newModuleDto; -import static org.sonar.process.ProcessProperties.Property.SONARCLOUD_ENABLED; public class ProjectDataLoaderTest { @Rule @@ -220,6 +219,7 @@ public class ProjectDataLoaderTest { private static FileSourceDto newFileSourceDto(ComponentDto file) { return new FileSourceDto() + .setUuid(Uuids.createFast()) .setFileUuid(file.uuid()) .setProjectUuid(file.projectUuid()) .setDataHash("0263047cd758c68c27683625f072f010") diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/SourceServiceTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/SourceServiceTest.java index 288dfd530d4..a148f91c352 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/SourceServiceTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/SourceServiceTest.java @@ -30,6 +30,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; +import org.sonar.core.util.Uuids; import org.sonar.db.DbTester; import org.sonar.db.protobuf.DbFileSources; import org.sonar.db.source.FileSourceDto; @@ -56,7 +57,7 @@ public class SourceServiceTest { @Before public void injectFakeLines() { FileSourceDto dto = new FileSourceDto(); - dto.setFileUuid(FILE_UUID).setProjectUuid("PROJECT_UUID"); + dto.setFileUuid(FILE_UUID).setUuid(Uuids.createFast()).setProjectUuid("PROJECT_UUID"); dto.setSourceData(FileSourceTesting.newFakeData(10).build()); dbTester.getDbClient().fileSourceDao().insert(dbTester.getSession(), dto); dbTester.commit(); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/IndexActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/IndexActionTest.java index 6ec084421f8..99f9a4daa25 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/IndexActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/IndexActionTest.java @@ -23,6 +23,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; +import org.sonar.core.util.Uuids; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; import org.sonar.db.protobuf.DbFileSources; @@ -143,6 +144,7 @@ public class IndexActionTest { private void insertFileWithData(ComponentDto file, DbFileSources.Data fileData) { db.getDbClient().fileSourceDao().insert(db.getSession(), new FileSourceDto() + .setUuid(Uuids.createFast()) .setProjectUuid(file.projectUuid()) .setFileUuid(file.uuid()) .setSourceData(fileData)); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/LinesActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/LinesActionTest.java index 846b9f6ce99..e842b6a617c 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/LinesActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/LinesActionTest.java @@ -26,6 +26,7 @@ import org.junit.rules.ExpectedException; import org.mockito.stubbing.Answer; import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; +import org.sonar.core.util.Uuids; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDao; import org.sonar.db.component.ComponentDto; @@ -127,6 +128,7 @@ public class LinesActionTest { ComponentDto branch = db.components().insertProjectBranch(project); ComponentDto file = db.components().insertComponent(newFileDto(branch)); db.getDbClient().fileSourceDao().insert(db.getSession(), new FileSourceDto() + .setUuid(Uuids.createFast()) .setProjectUuid(branch.uuid()) .setFileUuid(file.uuid()) .setSourceData(FileSourceTesting.newFakeData(3).build())); @@ -150,6 +152,7 @@ public class LinesActionTest { ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST)); ComponentDto file = db.components().insertComponent(newFileDto(branch)); db.getDbClient().fileSourceDao().insert(db.getSession(), new FileSourceDto() + .setUuid(Uuids.createFast()) .setProjectUuid(branch.uuid()) .setFileUuid(file.uuid()) .setSourceData(FileSourceTesting.newFakeData(3).build())); @@ -398,6 +401,7 @@ public class LinesActionTest { private ComponentDto insertFileWithData(DbFileSources.Data fileData, ComponentDto project) { ComponentDto file = insertFile(project); db.getDbClient().fileSourceDao().insert(db.getSession(), new FileSourceDto() + .setUuid(Uuids.createFast()) .setProjectUuid(project.projectUuid()) .setFileUuid(file.uuid()) .setSourceData(fileData)); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/ScmActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/ScmActionTest.java index 6b45db4afef..030749fc64c 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/ScmActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/ScmActionTest.java @@ -27,6 +27,7 @@ import org.junit.rules.ExpectedException; import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; +import org.sonar.core.util.Uuids; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; @@ -78,6 +79,7 @@ public class ScmActionTest { userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project, file); dbTester.getDbClient().fileSourceDao().insert(dbSession, new FileSourceDto() + .setUuid(Uuids.createFast()) .setProjectUuid(PROJECT_UUID) .setFileUuid(FILE_UUID) .setSourceData(DbFileSources.Data.newBuilder().addLines( @@ -95,6 +97,7 @@ public class ScmActionTest { userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project, file); dbTester.getDbClient().fileSourceDao().insert(dbSession, new FileSourceDto() + .setUuid(Uuids.createFast()) .setProjectUuid(PROJECT_UUID) .setFileUuid(FILE_UUID) .setSourceData(DbFileSources.Data.newBuilder() @@ -119,6 +122,7 @@ public class ScmActionTest { // lines 1 and 2 are the same commit, but not 3 (different date) dbTester.getDbClient().fileSourceDao().insert(dbSession, new FileSourceDto() + .setUuid(Uuids.createFast()) .setProjectUuid(PROJECT_UUID) .setFileUuid(FILE_UUID) .setSourceData(DbFileSources.Data.newBuilder() @@ -142,6 +146,7 @@ public class ScmActionTest { // lines 1 and 2 are the same commit, but not 3 (different date) dbTester.getDbClient().fileSourceDao().insert(dbSession, new FileSourceDto() + .setUuid(Uuids.createFast()) .setProjectUuid(PROJECT_UUID) .setFileUuid(FILE_UUID) .setSourceData(DbFileSources.Data.newBuilder() @@ -164,6 +169,7 @@ public class ScmActionTest { userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project, file); dbTester.getDbClient().fileSourceDao().insert(dbSession, new FileSourceDto() + .setUuid(Uuids.createFast()) .setProjectUuid(PROJECT_UUID) .setFileUuid(FILE_UUID) .setSourceData(DbFileSources.Data.newBuilder() @@ -187,6 +193,7 @@ public class ScmActionTest { userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project, file); dbTester.getDbClient().fileSourceDao().insert(dbSession, new FileSourceDto() + .setUuid(Uuids.createFast()) .setProjectUuid(PROJECT_UUID) .setFileUuid(FILE_UUID) .setSourceData(DbFileSources.Data.newBuilder().build())); -- 2.39.5