import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.assertj.core.api.Assertions.tuple;
import static org.sonar.db.newcodeperiod.NewCodePeriodType.NUMBER_OF_DAYS;
import static org.sonar.db.newcodeperiod.NewCodePeriodType.PREVIOUS_VERSION;
import static org.sonar.db.newcodeperiod.NewCodePeriodType.REFERENCE_BRANCH;
private final DbSession dbSession = db.getSession();
private final UuidFactory uuidFactory = new SequenceUuidFactory();
private final NewCodePeriodDao underTest = new NewCodePeriodDao(System2.INSTANCE, uuidFactory);
-
+
@Test
public void insert_new_code_period() {
- insert("proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5");
+ insert("1", "proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5");
Optional<NewCodePeriodDto> resultOpt = underTest.selectByUuid(dbSession, "1");
assertNewCodePeriodRowCount(1);
}
+ @Test
+ public void reference_branch_new_code_period_accepts_branches_with_long_names() {
+ String branchWithLongName = "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabc" +
+ "defghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijab" +
+ "cdefghijabcdefghijabcdefghijabcdefghijxxxxx";
+
+ insert("1", "proj-uuid", "branch-uuid", REFERENCE_BRANCH, branchWithLongName);
+
+ assertThat(db.select("select uuid as \"UUID\", value as \"VALUE\" from new_code_periods"))
+ .extracting(r -> r.get("UUID"), r -> r.get("VALUE"))
+ .containsExactly(tuple("1", branchWithLongName));
+ }
+
@Test
public void select_global_with_no_value() {
assertThat(underTest.selectGlobal(dbSession)).isEmpty();
@Test
public void update_new_code_period() {
- insert("proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5");
+ insert("1", "proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5");
underTest.update(dbSession, new NewCodePeriodDto()
.setUuid("1")
@Test
public void insert_with_upsert() {
- insert("proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5");
+ insert("1", "proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5");
Optional<NewCodePeriodDto> resultOpt = underTest.selectByUuid(dbSession, "1");
@Test
public void update_with_upsert() {
- insert("proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5");
+ insert("1", "proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5");
underTest.upsert(dbSession, new NewCodePeriodDto()
.setUuid("1")
@Test
public void select_by_project_and_branch_uuids() {
- insert("proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5");
+ insert("1", "proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5");
Optional<NewCodePeriodDto> resultOpt = underTest.selectByBranch(dbSession, "proj-uuid", "branch-uuid");
assertThat(resultOpt)
BranchDto branch2 = db.components().insertProjectBranch(project);
BranchDto branch3 = db.components().insertProjectBranch(project);
- insert(project.getUuid(), null, REFERENCE_BRANCH, mainBranch.getKey());
- insert(project.getUuid(), branch1.getUuid(), REFERENCE_BRANCH, mainBranch.getKey());
- insert(project.getUuid(), branch2.getUuid(), NUMBER_OF_DAYS, "5");
- insert(project.getUuid(), project.getUuid(), PREVIOUS_VERSION, null);
+ insert("1", project.getUuid(), null, REFERENCE_BRANCH, mainBranch.getKey());
+ insert("2", project.getUuid(), branch1.getUuid(), REFERENCE_BRANCH, mainBranch.getKey());
+ insert("3", project.getUuid(), branch2.getUuid(), NUMBER_OF_DAYS, "5");
+ insert("4", project.getUuid(), project.getUuid(), PREVIOUS_VERSION, null);
db.commit();
assertThat(underTest.selectBranchesReferencing(dbSession, project.getUuid(), mainBranch.getKey())).containsOnly(branch1.getUuid(), branch3.getUuid());
}
@Test
public void select_by_project_uuid() {
- insert("proj-uuid", null, NUMBER_OF_DAYS, "5");
+ insert("1", "proj-uuid", null, NUMBER_OF_DAYS, "5");
Optional<NewCodePeriodDto> resultOpt = underTest.selectByProject(dbSession, "proj-uuid");
assertThat(resultOpt)
@Test
public void select_global() {
- insert(null, null, NUMBER_OF_DAYS, "30");
+ insert("1", null, null, NUMBER_OF_DAYS, "30");
Optional<NewCodePeriodDto> newCodePeriodDto = underTest.selectGlobal(dbSession);
assertThat(newCodePeriodDto).isNotEmpty();
@Test
public void exists_by_project_analysis_is_true() {
- insert("proj-uuid", "branch-uuid", SPECIFIC_ANALYSIS, "analysis-uuid");
+ insert("1", "proj-uuid", "branch-uuid", SPECIFIC_ANALYSIS, "analysis-uuid");
boolean exists = underTest.existsByProjectAnalysisUuid(dbSession, "analysis-uuid");
assertThat(exists).isTrue();
@Test
public void delete_by_project_uuid_and_branch_uuid() {
- insert("proj-uuid", "branch-uuid", SPECIFIC_ANALYSIS, "analysis-uuid");
+ insert("1", "proj-uuid", "branch-uuid", SPECIFIC_ANALYSIS, "analysis-uuid");
underTest.delete(dbSession, "proj-uuid", "branch-uuid");
db.commit();
@Test
public void delete_by_project_uuid() {
- insert("proj-uuid", null, SPECIFIC_ANALYSIS, "analysis-uuid");
+ insert("1", "proj-uuid", null, SPECIFIC_ANALYSIS, "analysis-uuid");
underTest.delete(dbSession, "proj-uuid", null);
db.commit();
@Test
public void delete_global() {
- insert(null, null, SPECIFIC_ANALYSIS, "analysis-uuid");
+ insert("1", null, null, SPECIFIC_ANALYSIS, "analysis-uuid");
underTest.delete(dbSession, null, null);
db.commit();
.isEqualTo(expected);
}
- private void insert(@Nullable String projectUuid, @Nullable String branchUuid, NewCodePeriodType type, @Nullable String value) {
+ private void insert(String uuid, @Nullable String projectUuid, @Nullable String branchUuid, NewCodePeriodType type, @Nullable String value) {
underTest.insert(dbSession, new NewCodePeriodDto()
+ .setUuid(uuid)
.setProjectUuid(projectUuid)
.setBranchUuid(branchUuid)
.setType(type)
.setValue(value));
+ db.commit();
}
}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 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.v89;
+
+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.VarcharColumnDef.newVarcharColumnDefBuilder;
+
+public class IncreaseSizeOfValueColumnInNewCodePeriodsTable extends DdlChange {
+ private static final String TABLE_NAME = "new_code_periods";
+
+ public IncreaseSizeOfValueColumnInNewCodePeriodsTable(Database db) {
+ super(db);
+ }
+
+ @Override
+ public void execute(Context context) throws SQLException {
+ context.execute(new AlterColumnsBuilder(getDialect(), TABLE_NAME)
+ .updateColumn(newVarcharColumnDefBuilder()
+ .setColumnName("value")
+ .setLimit(255)
+ .build())
+ .build());
+ }
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 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.v89;
+
+import java.sql.SQLException;
+import javax.annotation.Nullable;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.utils.System2;
+import org.sonar.db.CoreDbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.assertj.core.api.Assertions.tuple;
+
+public class IncreaseSizeOfValueColumnInNewCodePeriodsTableTest {
+ private static final String TABLE_NAME = "new_code_periods";
+ private static final String VERY_LONG_BRANCH_NAME = "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijab" +
+ "cdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcd" +
+ "efghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijxxxxx";
+ private final System2 system = System2.INSTANCE;
+
+ @Rule
+ public CoreDbTester db = CoreDbTester.createForSchema(IncreaseSizeOfValueColumnInNewCodePeriodsTableTest.class, "schema.sql");
+
+ private final IncreaseSizeOfValueColumnInNewCodePeriodsTable underTest = new IncreaseSizeOfValueColumnInNewCodePeriodsTable(db.database());
+
+ @Test
+ public void cannot_insert_long_value_before_migration() {
+ assertThatThrownBy(() -> insertNewCodePeriod("1", VERY_LONG_BRANCH_NAME))
+ .isInstanceOf(IllegalStateException.class);
+ }
+
+ @Test
+ public void can_insert_long_value_after_migration() throws SQLException {
+ underTest.execute();
+ assertThat(db.countRowsOfTable(TABLE_NAME)).isZero();
+
+ insertNewCodePeriod("1", VERY_LONG_BRANCH_NAME);
+
+ assertThat(db.countRowsOfTable(TABLE_NAME)).isEqualTo(1);
+ }
+
+ @Test
+ public void existing_entries_are_not_affected() throws SQLException {
+ insertNewCodePeriod("1", "branch1");
+ insertNewCodePeriod("2", null);
+
+ underTest.execute();
+
+ assertThat(db.select("select uuid as \"UUID\", value as \"VALUE\"from new_code_periods"))
+ .extracting(r -> r.get("UUID"), r -> r.get("VALUE"))
+ .containsExactlyInAnyOrder(
+ tuple("1", "branch1"),
+ tuple("2", null));
+ }
+
+ private void insertNewCodePeriod(String uuid, @Nullable String value) {
+ long now = system.now();
+ db.executeInsert("NEW_CODE_PERIODS",
+ "UUID", uuid,
+ "PROJECT_UUID", "proj-" + uuid,
+ "BRANCH_UUID", "branch-1",
+ "TYPE", "REFERENCE_BRANCH",
+ "VALUE", value,
+ "UPDATED_AT", now,
+ "CREATED_AT", now);
+ }
+}