aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-migration
diff options
context:
space:
mode:
authorMichal Duda <michal.duda@sonarsource.com>2021-04-21 11:27:25 +0200
committersonartech <sonartech@sonarsource.com>2021-04-21 20:03:47 +0000
commitc36b12d2160dd7c57bb7374345a02ebb91ae31e9 (patch)
tree7e83037eeb598644485d5d624aaba8ab8199d442 /server/sonar-db-migration
parent58e250e51c4b8197d2f5bf7f2562f1a0c542b8f9 (diff)
downloadsonarqube-c36b12d2160dd7c57bb7374345a02ebb91ae31e9.tar.gz
sonarqube-c36b12d2160dd7c57bb7374345a02ebb91ae31e9.zip
SONAR-14693 fix error when setting a new code period reference branch
Diffstat (limited to 'server/sonar-db-migration')
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v89/DbVersion89.java3
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v89/IncreaseSizeOfValueColumnInNewCodePeriodsTable.java45
-rw-r--r--server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v89/IncreaseSizeOfValueColumnInNewCodePeriodsTableTest.java86
-rw-r--r--server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v89/IncreaseSizeOfValueColumnInNewCodePeriodsTableTest/schema.sql13
4 files changed, 146 insertions, 1 deletions
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v89/DbVersion89.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v89/DbVersion89.java
index c9685b683b2..54ced6ea0d9 100644
--- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v89/DbVersion89.java
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v89/DbVersion89.java
@@ -30,6 +30,7 @@ public class DbVersion89 implements DbVersion {
.add(4400, "Add indices on columns 'type' and 'value' to 'new_code_periods' table", AddIndicesToNewCodePeriodTable.class)
.add(4401, "Drop local webhooks", DropLocalWebhooks.class)
.add(4402, "Add Index on column 'main_branch_project_uuid' to 'components' table", AddMainBranchProjectUuidIndexToComponentTable.class)
- .add(4403, "Drop Github endpoint on project level setting", DropGithubEndpointOnProjectLevelSetting.class);
+ .add(4403, "Drop Github endpoint on project level setting", DropGithubEndpointOnProjectLevelSetting.class)
+ .add(4404, "Increase size of 'value' column in 'new_code_periods' table ", IncreaseSizeOfValueColumnInNewCodePeriodsTable.class);
}
}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v89/IncreaseSizeOfValueColumnInNewCodePeriodsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v89/IncreaseSizeOfValueColumnInNewCodePeriodsTable.java
new file mode 100644
index 00000000000..59814056f31
--- /dev/null
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v89/IncreaseSizeOfValueColumnInNewCodePeriodsTable.java
@@ -0,0 +1,45 @@
+/*
+ * 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());
+ }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v89/IncreaseSizeOfValueColumnInNewCodePeriodsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v89/IncreaseSizeOfValueColumnInNewCodePeriodsTableTest.java
new file mode 100644
index 00000000000..c6c47e402a1
--- /dev/null
+++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v89/IncreaseSizeOfValueColumnInNewCodePeriodsTableTest.java
@@ -0,0 +1,86 @@
+/*
+ * 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);
+ }
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v89/IncreaseSizeOfValueColumnInNewCodePeriodsTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v89/IncreaseSizeOfValueColumnInNewCodePeriodsTableTest/schema.sql
new file mode 100644
index 00000000000..b560d20c68a
--- /dev/null
+++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v89/IncreaseSizeOfValueColumnInNewCodePeriodsTableTest/schema.sql
@@ -0,0 +1,13 @@
+CREATE TABLE "NEW_CODE_PERIODS"(
+ "UUID" VARCHAR(40) NOT NULL,
+ "PROJECT_UUID" VARCHAR(40),
+ "BRANCH_UUID" VARCHAR(40),
+ "TYPE" VARCHAR(30) NOT NULL,
+ "VALUE" VARCHAR(40),
+ "UPDATED_AT" BIGINT NOT NULL,
+ "CREATED_AT" BIGINT NOT NULL
+);
+ALTER TABLE "NEW_CODE_PERIODS" ADD CONSTRAINT "PK_NEW_CODE_PERIODS" PRIMARY KEY("UUID");
+CREATE UNIQUE INDEX "UNIQ_NEW_CODE_PERIODS" ON "NEW_CODE_PERIODS"("PROJECT_UUID", "BRANCH_UUID");
+CREATE INDEX "IDX_NCP_TYPE" ON "NEW_CODE_PERIODS"("TYPE");
+CREATE INDEX "IDX_NCP_VALUE" ON "NEW_CODE_PERIODS"("VALUE");