From 32b97ccbf42ad73666b78c68b878732d28085cad Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Mon, 23 Apr 2018 16:25:45 +0200 Subject: [PATCH] SONAR-10430 add FILE_SOURCES.LINE_COUNT --- .../org/sonar/db/version/schema-h2.ddl | 1 + ..._files_sources_of_specified_components.xml | 4 + .../PurgeDaoTest/shouldDeleteProject.xml | 1 + .../no_line_hashes_when_only_test_data.xml | 1 + .../db/source/FileSourceDaoTest/shared.xml | 1 + .../FileSourceDaoTest/update-result.xml | 1 + .../version/v72/AddFileSourceLineCount.java | 42 +++++++++ .../db/migration/version/v72/DbVersion72.java | 3 + .../MakeFileSourceLineCountNotNullable.java | 43 +++++++++ .../v72/PopulateFileSourceLineCount.java | 62 +++++++++++++ .../v72/AddFileSourceLineCountTest.java | 55 ++++++++++++ .../version/v72/DbVersion72Test.java | 2 +- ...akeFileSourceLineCountNotNullableTest.java | 61 +++++++++++++ .../v72/PopulateFileSourceLineCountTest.java | 89 +++++++++++++++++++ .../file_sources.sql | 16 ++++ .../file_sources.sql | 17 ++++ .../file_sources.sql | 17 ++++ 17 files changed, 415 insertions(+), 1 deletion(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/AddFileSourceLineCount.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/MakeFileSourceLineCountNotNullable.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/PopulateFileSourceLineCount.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/AddFileSourceLineCountTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/MakeFileSourceLineCountNotNullableTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/PopulateFileSourceLineCountTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v72/AddFileSourceLineCountTest/file_sources.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v72/MakeFileSourceLineCountNotNullableTest/file_sources.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v72/PopulateFileSourceLineCountTest/file_sources.sql diff --git a/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl b/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl index 83a3091a455..d480b16e24a 100644 --- a/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl +++ b/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl @@ -615,6 +615,7 @@ CREATE TABLE "FILE_SOURCES" ( "FILE_UUID" VARCHAR(50) NOT NULL, "LINE_HASHES" CLOB, "LINE_HASHES_VERSION" INTEGER, + "LINE_COUNT" INTEGER NOT NULL, "BINARY_DATA" BLOB, "DATA_TYPE" VARCHAR(20), "DATA_HASH" VARCHAR(50), diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/purge/PurgeDaoTest/close_issues_clean_index_and_files_sources_of_specified_components.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/purge/PurgeDaoTest/close_issues_clean_index_and_files_sources_of_specified_components.xml index 40c2ae8f4f7..d371b7cd955 100644 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/purge/PurgeDaoTest/close_issues_clean_index_and_files_sources_of_specified_components.xml +++ b/server/sonar-db-dao/src/test/resources/org/sonar/db/purge/PurgeDaoTest/close_issues_clean_index_and_files_sources_of_specified_components.xml @@ -257,6 +257,7 @@ file_uuid="GHIJ" binary_data="[null]" line_hashes="[null]" + line_count="0" data_hash="321654987" revision="123456789" created_at="123456789" @@ -268,6 +269,7 @@ file_uuid="KLMN" binary_data="[null]" line_hashes="[null]" + line_count="0" data_hash="321654988" revision="123456789" created_at="123456789" @@ -279,6 +281,7 @@ file_uuid="GHIJ" binary_data="[null]" line_hashes="[null]" + line_count="0" data_hash="321654987" revision="123456789" created_at="123456789" @@ -290,6 +293,7 @@ file_uuid="KLMN" binary_data="[null]" line_hashes="[null]" + line_count="0" data_hash="321654988" revision="123456789" created_at="123456789" diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/purge/PurgeDaoTest/shouldDeleteProject.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/purge/PurgeDaoTest/shouldDeleteProject.xml index deb3b1511d6..f0e0ed49218 100644 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/purge/PurgeDaoTest/shouldDeleteProject.xml +++ b/server/sonar-db-dao/src/test/resources/org/sonar/db/purge/PurgeDaoTest/shouldDeleteProject.xml @@ -221,6 +221,7 @@ file_uuid="D" binary_data="[null]" line_hashes="[null]" + line_count="0" data_hash="321654987" created_at="123456789" updated_at="123456789" diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/source/FileSourceDaoTest/no_line_hashes_when_only_test_data.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/source/FileSourceDaoTest/no_line_hashes_when_only_test_data.xml index 481445affdc..01bc65ac204 100644 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/source/FileSourceDaoTest/no_line_hashes_when_only_test_data.xml +++ b/server/sonar-db-dao/src/test/resources/org/sonar/db/source/FileSourceDaoTest/no_line_hashes_when_only_test_data.xml @@ -3,6 +3,7 @@ diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/source/FileSourceDaoTest/shared.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/source/FileSourceDaoTest/shared.xml index a2d372095c0..77d242162c6 100644 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/source/FileSourceDaoTest/shared.xml +++ b/server/sonar-db-dao/src/test/resources/org/sonar/db/source/FileSourceDaoTest/shared.xml @@ -3,6 +3,7 @@ diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/source/FileSourceDaoTest/update-result.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/source/FileSourceDaoTest/update-result.xml index f57da523a84..e51106a4630 100644 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/source/FileSourceDaoTest/update-result.xml +++ b/server/sonar-db-dao/src/test/resources/org/sonar/db/source/FileSourceDaoTest/update-result.xml @@ -4,6 +4,7 @@ binary_data="[ignore]" data_hash="NEW_DATA_HASH" line_hashes="NEW_LINE_HASHES" + line_count="1" src_hash="NEW_FILE_HASH" revision="987654321" created_at="1500000000000" updated_at="1500000000002" data_type="SOURCE" line_hashes_version="1" /> diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/AddFileSourceLineCount.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/AddFileSourceLineCount.java new file mode 100644 index 00000000000..643339a5e2e --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/AddFileSourceLineCount.java @@ -0,0 +1,42 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 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.v72; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.def.IntegerColumnDef; +import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +public class AddFileSourceLineCount extends DdlChange { + public AddFileSourceLineCount(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AddColumnsBuilder(getDialect(), "file_sources") + .addColumn(IntegerColumnDef.newIntegerColumnDefBuilder() + .setColumnName("line_count") + .setIsNullable(true) + .build()) + .build()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/DbVersion72.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/DbVersion72.java index d072d970386..243ec08c19f 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/DbVersion72.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/DbVersion72.java @@ -51,6 +51,9 @@ public class DbVersion72 implements DbVersion { .add(2121, "Rename NOTE_USER_LOGIN TO NOTE_USER_UUID on table RULES_METADATA", RenameNoteUserLoginToNoteUserUuidOnTableRulesMetadata.class) .add(2122, "Rename SUBMITTER_LOGIN TO SUBMITTER_UUID on table CE_QUEUE", RenameSubmitterLoginToSubmitterUuidOnTableCeQueue.class) .add(2123, "Rename SUBMITTER_LOGIN TO SUBMITTER_UUID on table CE_ACTIVITY", RenameSubmitterLoginToSubmitterUuidOnTableCeActivity.class) + .add(2124, "Add FILE_SOURCE.LINE_COUNT", AddFileSourceLineCount.class) + .add(2125, "Populate FILE_SOURCE.LINE_COUNT", PopulateFileSourceLineCount.class) + .add(2126, "Make FILE_SOURCE.LINE_COUNT not nullable", MakeFileSourceLineCountNotNullable.class) ; } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/MakeFileSourceLineCountNotNullable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/MakeFileSourceLineCountNotNullable.java new file mode 100644 index 00000000000..3b196d754f4 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/MakeFileSourceLineCountNotNullable.java @@ -0,0 +1,43 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 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.v72; + +import java.sql.SQLException; +import org.sonar.db.Database; +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.IntegerColumnDef.newIntegerColumnDefBuilder; + +public class MakeFileSourceLineCountNotNullable extends DdlChange { + public MakeFileSourceLineCountNotNullable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AlterColumnsBuilder(getDialect(), "file_sources") + .updateColumn(newIntegerColumnDefBuilder() + .setColumnName("line_count") + .setIsNullable(false) + .build()) + .build()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/PopulateFileSourceLineCount.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/PopulateFileSourceLineCount.java new file mode 100644 index 00000000000..756f4d9c666 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/PopulateFileSourceLineCount.java @@ -0,0 +1,62 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 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.v72; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.DataChange; +import org.sonar.server.platform.db.migration.step.MassUpdate; +import org.sonar.server.platform.db.migration.step.Select; +import org.sonar.server.platform.db.migration.step.SqlStatement; + +/** + * New column is populated with value {@code -1} rather than with the number of lines computed from the value of column + * {@code line_hashes} (which would be the correct value for this column). + *

+ * Column will be populated with the correct value by the new "DB migration step" of the analysis report processing the + * first time a project is analyzed after SonarQube's upgrade. + *

+ * This innovative approach to DB migration is used because populating the column from {@code line_hashes} will take + * a very long time on large DBs. + */ +public class PopulateFileSourceLineCount extends DataChange { + private static final int LINE_COUNT_NOT_POPULATED = -1; + + public PopulateFileSourceLineCount(Database db) { + super(db); + } + + @Override + protected void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("select distinct project_uuid from file_sources where line_count is null"); + massUpdate.update("update file_sources set line_count = ? where project_uuid = ?"); + massUpdate.rowPluralName("file source line counts"); + massUpdate.execute(PopulateFileSourceLineCount::handle); + } + + private static boolean handle(Select.Row row, SqlStatement update) throws SQLException { + String projectUuid = row.getString(1); + + update.setInt(1, LINE_COUNT_NOT_POPULATED); + update.setString(2, projectUuid); + return true; + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/AddFileSourceLineCountTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/AddFileSourceLineCountTest.java new file mode 100644 index 00000000000..a65dafd76a0 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/AddFileSourceLineCountTest.java @@ -0,0 +1,55 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 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.v72; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.db.CoreDbTester; + +import static java.sql.Types.INTEGER; + +public class AddFileSourceLineCountTest { + private static final String TABLE_NAME = "file_sources"; + + @Rule + public CoreDbTester dbTester = CoreDbTester.createForSchema(AddFileSourceLineCountTest.class, "file_sources.sql"); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private AddFileSourceLineCount underTest = new AddFileSourceLineCount(dbTester.database()); + + @Test + public void column_is_added_to_table() throws SQLException { + underTest.execute(); + + dbTester.assertColumnDefinition(TABLE_NAME, "line_count", INTEGER, null, true); + } + + @Test + public void migration_is_not_reentrant() throws SQLException { + underTest.execute(); + + expectedException.expect(IllegalStateException.class); + + underTest.execute(); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/DbVersion72Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/DbVersion72Test.java index 3109b8f8735..bd24b8f02ec 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/DbVersion72Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/DbVersion72Test.java @@ -34,7 +34,7 @@ public class DbVersion72Test { @Test public void verify_migration_count() { - verifyMigrationCount(underTest, 24); + verifyMigrationCount(underTest, 27); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/MakeFileSourceLineCountNotNullableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/MakeFileSourceLineCountNotNullableTest.java new file mode 100644 index 00000000000..1564b8d6377 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/MakeFileSourceLineCountNotNullableTest.java @@ -0,0 +1,61 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 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.v72; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.db.CoreDbTester; + +import static java.sql.Types.INTEGER; + +public class MakeFileSourceLineCountNotNullableTest { + private static final String TABLE_NAME = "file_sources"; + + @Rule + public CoreDbTester dbTester = CoreDbTester.createForSchema(MakeFileSourceLineCountNotNullableTest.class, "file_sources.sql"); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private MakeFileSourceLineCountNotNullable underTest = new MakeFileSourceLineCountNotNullable(dbTester.database()); + + @Test + public void column_is_made_not_nullable() throws SQLException { + underTest.execute(); + + dbTester.assertColumnDefinition(TABLE_NAME, "line_count", INTEGER, null, false); + } + + @Test + public void migration_does_not_fix_null_values_in_line_count() throws SQLException { + dbTester.executeInsert( + TABLE_NAME, + "PROJECT_UUID", "foo_prj", + "FILE_UUID", "foo_file", + "CREATED_AT", 123456, + "UPDATED_AT", 987654 + ); + + expectedException.expect(IllegalStateException.class); + + underTest.execute(); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/PopulateFileSourceLineCountTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/PopulateFileSourceLineCountTest.java new file mode 100644 index 00000000000..e27061387eb --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/PopulateFileSourceLineCountTest.java @@ -0,0 +1,89 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 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.v72; + +import java.sql.SQLException; +import java.util.Random; +import java.util.stream.IntStream; +import javax.annotation.Nullable; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.db.CoreDbTester; + +import static org.assertj.core.api.Assertions.assertThat; + +public class PopulateFileSourceLineCountTest { + private static final String TABLE_NAME = "file_sources"; + + @Rule + public CoreDbTester dbTester = CoreDbTester.createForSchema(PopulateFileSourceLineCountTest.class, "file_sources.sql"); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private PopulateFileSourceLineCount underTest = new PopulateFileSourceLineCount(dbTester.database()); + + @Test + public void execute_does_not_fail_on_empty_table() throws SQLException { + underTest.execute(); + } + + @Test + public void execute_set_value_to_minus_1_when_line_count_is_null() throws SQLException { + IntStream.range(0, 5 + new Random().nextInt(10)).forEach(i -> insert("prj_" + i, "file_" + i)); + + underTest.execute(); + + assertThat(dbTester.select("select distinct line_count as \"count\" from " + TABLE_NAME)) + .extracting(t -> t.get("count")) + .containsOnly(-1L); + } + + @Test + public void execute_keeps_value_when_line_count_is_not_null() throws SQLException { + insert("prj_A", "file_1", 12); + insert("prj_A", "file_2", 0); + insert("prj_B", "file_3", -5); + insert("prj_B", "file_4", -5); + insert("prj_C", "file_5", null); + insert("prj_D", "file_6", null); + insert("prj_D", "file_7", 12); + + underTest.execute(); + + assertThat(dbTester.select("select line_count as \"count\" from " + TABLE_NAME)) + .extracting(t -> t.get("count")) + .containsOnly(-1L, 12L, 12L, 0L, -5L, -5L); + } + + public void insert(String projectUuid, String fileUuid) { + insert(projectUuid, fileUuid, null); + } + + public void insert(String projectUuid, String fileUuid, @Nullable Integer lineCount) { + dbTester.executeInsert( + TABLE_NAME, + "PROJECT_UUID", projectUuid, + "FILE_UUID", fileUuid, + "LINE_COUNT", lineCount, + "CREATED_AT", 123456, + "UPDATED_AT", 987654); + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v72/AddFileSourceLineCountTest/file_sources.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v72/AddFileSourceLineCountTest/file_sources.sql new file mode 100644 index 00000000000..4df5eff4862 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v72/AddFileSourceLineCountTest/file_sources.sql @@ -0,0 +1,16 @@ +CREATE TABLE "FILE_SOURCES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "PROJECT_UUID" VARCHAR(50) NOT NULL, + "FILE_UUID" VARCHAR(50) NOT NULL, + "LINE_HASHES" CLOB, + "BINARY_DATA" BLOB, + "DATA_TYPE" VARCHAR(20), + "DATA_HASH" VARCHAR(50), + "SRC_HASH" VARCHAR(50), + "REVISION" VARCHAR(100), + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL +); +CREATE INDEX "FILE_SOURCES_PROJECT_UUID" ON "FILE_SOURCES" ("PROJECT_UUID"); +CREATE UNIQUE INDEX "FILE_SOURCES_UUID_TYPE" ON "FILE_SOURCES" ("FILE_UUID", "DATA_TYPE"); +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/v72/MakeFileSourceLineCountNotNullableTest/file_sources.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v72/MakeFileSourceLineCountNotNullableTest/file_sources.sql new file mode 100644 index 00000000000..ac8ae8eba78 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v72/MakeFileSourceLineCountNotNullableTest/file_sources.sql @@ -0,0 +1,17 @@ +CREATE TABLE "FILE_SOURCES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "PROJECT_UUID" VARCHAR(50) NOT NULL, + "FILE_UUID" VARCHAR(50) NOT NULL, + "LINE_HASHES" CLOB, + "LINE_COUNT" INTEGER, + "BINARY_DATA" BLOB, + "DATA_TYPE" VARCHAR(20), + "DATA_HASH" VARCHAR(50), + "SRC_HASH" VARCHAR(50), + "REVISION" VARCHAR(100), + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL +); +CREATE INDEX "FILE_SOURCES_PROJECT_UUID" ON "FILE_SOURCES" ("PROJECT_UUID"); +CREATE UNIQUE INDEX "FILE_SOURCES_UUID_TYPE" ON "FILE_SOURCES" ("FILE_UUID", "DATA_TYPE"); +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/v72/PopulateFileSourceLineCountTest/file_sources.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v72/PopulateFileSourceLineCountTest/file_sources.sql new file mode 100644 index 00000000000..ac8ae8eba78 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v72/PopulateFileSourceLineCountTest/file_sources.sql @@ -0,0 +1,17 @@ +CREATE TABLE "FILE_SOURCES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "PROJECT_UUID" VARCHAR(50) NOT NULL, + "FILE_UUID" VARCHAR(50) NOT NULL, + "LINE_HASHES" CLOB, + "LINE_COUNT" INTEGER, + "BINARY_DATA" BLOB, + "DATA_TYPE" VARCHAR(20), + "DATA_HASH" VARCHAR(50), + "SRC_HASH" VARCHAR(50), + "REVISION" VARCHAR(100), + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL +); +CREATE INDEX "FILE_SOURCES_PROJECT_UUID" ON "FILE_SOURCES" ("PROJECT_UUID"); +CREATE UNIQUE INDEX "FILE_SOURCES_UUID_TYPE" ON "FILE_SOURCES" ("FILE_UUID", "DATA_TYPE"); +CREATE INDEX "FILE_SOURCES_UPDATED_AT" ON "FILE_SOURCES" ("UPDATED_AT"); -- 2.39.5