"TAGS" CHARACTER VARYING(500),
"CREATED_AT" BIGINT,
"UPDATED_AT" BIGINT NOT NULL,
- "NCLOC" BIGINT
+ "NCLOC" BIGINT,
+ "CREATION_METHOD" CHARACTER VARYING(50) NOT NULL
);
ALTER TABLE "PROJECTS" ADD CONSTRAINT "PK_NEW_PROJECTS" PRIMARY KEY("UUID");
CREATE UNIQUE NULLS DISTINCT INDEX "UNIQ_PROJECTS_KEE" ON "PROJECTS"("KEE" NULLS FIRST);
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.v103;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.db.DatabaseUtils;
+import org.sonar.server.platform.db.migration.def.ColumnDef;
+import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
+import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+public class AddCreationMethodColumnInProjectsTable extends DdlChange {
+ static final String PROJECTS_TABLE_NAME = "projects";
+ static final String PROJECTS_CREATION_METHOD_COLUMN_NAME = "creation_method";
+ static final int PROJECTS_CREATION_METHOD_COLUMN_SIZE = 50;
+
+ public AddCreationMethodColumnInProjectsTable(Database db) {
+ super(db);
+ }
+
+ @Override
+ public void execute(Context context) throws SQLException {
+ try (Connection connection = getDatabase().getDataSource().getConnection()) {
+ if (!DatabaseUtils.tableColumnExists(connection, PROJECTS_TABLE_NAME, PROJECTS_CREATION_METHOD_COLUMN_NAME)) {
+ ColumnDef columnDef = VarcharColumnDef.newVarcharColumnDefBuilder()
+ .setColumnName(PROJECTS_CREATION_METHOD_COLUMN_NAME)
+ .setLimit(PROJECTS_CREATION_METHOD_COLUMN_SIZE)
+ .setIsNullable(true)
+ .build();
+ context.execute(new AddColumnsBuilder(getDialect(), PROJECTS_TABLE_NAME).addColumn(columnDef).build());
+ }
+ }
+ }
+}
.add(10_3_001, "Add table 'github_perms_mapping'", CreateGithubPermissionsMappingTable.class)
.add(10_3_002, "Create unique index on 'github_perms_mapping'", CreateUniqueIndexForGithubPermissionsMappingTable.class)
.add(10_3_003, "Add default mappings to 'github_perms_mapping'", PopulateGithubPermissionsMapping.class)
- .add(10_3_004, "Add 'clean_code_attribute' column in 'issues' table", AddCleanCodeAttributeColumnInIssuesTable.class);
+ .add(10_3_004, "Add 'clean_code_attribute' column in 'issues' table", AddCleanCodeAttributeColumnInIssuesTable.class)
+
+ .add(10_3_005, "Add 'creation_method' column in 'projects' table", AddCreationMethodColumnInProjectsTable.class)
+ .add(10_3_006, "Populate 'creation_method' column in 'projects' table", PopulateCreationMethodColumnInProjectsTable.class)
+ .add(10_3_007, "Make 'creation_method' column in 'projects' table non-nullable", MakeCreationMethodColumnInProjectsNotNullable.class)
+ ;
}
}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.v103;
+
+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.version.v103.AddCreationMethodColumnInProjectsTable.PROJECTS_CREATION_METHOD_COLUMN_NAME;
+import static org.sonar.server.platform.db.migration.version.v103.AddCreationMethodColumnInProjectsTable.PROJECTS_CREATION_METHOD_COLUMN_SIZE;
+import static org.sonar.server.platform.db.migration.version.v103.AddCreationMethodColumnInProjectsTable.PROJECTS_TABLE_NAME;
+
+public class MakeCreationMethodColumnInProjectsNotNullable extends DdlChange {
+
+ public MakeCreationMethodColumnInProjectsNotNullable(Database db) {
+ super(db);
+ }
+
+ @Override
+ public void execute(Context context) throws SQLException {
+ VarcharColumnDef columnDef = VarcharColumnDef.newVarcharColumnDefBuilder(PROJECTS_CREATION_METHOD_COLUMN_NAME)
+ .setIsNullable(false)
+ .setLimit(PROJECTS_CREATION_METHOD_COLUMN_SIZE)
+ .build();
+ context.execute(new AlterColumnsBuilder(getDialect(), PROJECTS_TABLE_NAME).updateColumn(columnDef).build());
+ }
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.v103;
+
+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.Upsert;
+
+public class PopulateCreationMethodColumnInProjectsTable extends DataChange {
+
+ private static final String UPDATE_QUERY = """
+ update projects set creation_method='unknown'
+ """;
+
+ public PopulateCreationMethodColumnInProjectsTable(Database db) {
+ super(db);
+ }
+ @Override
+ protected void execute(Context context) throws SQLException {
+ Upsert upsert = context.prepareUpsert(UPDATE_QUERY);
+ upsert.execute().commit();
+ }
+
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.v103;
+
+import java.sql.SQLException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+
+import static java.sql.Types.VARCHAR;
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.sonar.server.platform.db.migration.version.v103.AddCreationMethodColumnInProjectsTable.PROJECTS_CREATION_METHOD_COLUMN_NAME;
+import static org.sonar.server.platform.db.migration.version.v103.AddCreationMethodColumnInProjectsTable.PROJECTS_CREATION_METHOD_COLUMN_SIZE;
+import static org.sonar.server.platform.db.migration.version.v103.AddCreationMethodColumnInProjectsTable.PROJECTS_TABLE_NAME;
+
+public class AddCreationMethodColumnInProjectsTableTest {
+ @Rule
+ public final CoreDbTester db = CoreDbTester.createForSchema(AddCreationMethodColumnInProjectsTableTest.class, "schema.sql");
+
+ private final AddCreationMethodColumnInProjectsTable underTest = new AddCreationMethodColumnInProjectsTable(db.database());
+
+ @Test
+ public void execute_whenColumnDoesNotExist_shouldCreateColumn() throws SQLException {
+ db.assertColumnDoesNotExist(PROJECTS_TABLE_NAME, PROJECTS_CREATION_METHOD_COLUMN_NAME);
+ underTest.execute();
+ db.assertColumnDefinition(PROJECTS_TABLE_NAME, PROJECTS_CREATION_METHOD_COLUMN_NAME, VARCHAR, PROJECTS_CREATION_METHOD_COLUMN_SIZE, true);
+ }
+
+ @Test
+ public void execute_whenColumnsAlreadyExists_shouldNotFail() throws SQLException {
+ underTest.execute();
+ assertThatCode(underTest::execute).doesNotThrowAnyException();
+ }
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.v103;
+
+import java.sql.SQLException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+
+import static java.sql.Types.VARCHAR;
+import static org.sonar.server.platform.db.migration.version.v103.AddCreationMethodColumnInProjectsTable.PROJECTS_CREATION_METHOD_COLUMN_NAME;
+import static org.sonar.server.platform.db.migration.version.v103.AddCreationMethodColumnInProjectsTable.PROJECTS_TABLE_NAME;
+
+public class MakeCreationMethodColumnInProjectsNotNullableTest {
+ @Rule
+ public final CoreDbTester db = CoreDbTester.createForSchema(MakeCreationMethodColumnInProjectsNotNullableTest.class, "schema.sql");
+
+ private final MakeCreationMethodColumnInProjectsNotNullable underTest = new MakeCreationMethodColumnInProjectsNotNullable(db.database());
+
+ @Test
+ public void user_local_column_is_not_null() throws SQLException {
+ db.assertColumnDefinition(PROJECTS_TABLE_NAME, PROJECTS_CREATION_METHOD_COLUMN_NAME, VARCHAR, null, true);
+ underTest.execute();
+ db.assertColumnDefinition(PROJECTS_TABLE_NAME, PROJECTS_CREATION_METHOD_COLUMN_NAME, VARCHAR, null, false);
+ }
+
+ @Test
+ public void migration_is_reentrant() throws SQLException {
+ db.assertColumnDefinition(PROJECTS_TABLE_NAME, PROJECTS_CREATION_METHOD_COLUMN_NAME, VARCHAR, null, true);
+ underTest.execute();
+ underTest.execute();
+ db.assertColumnDefinition(PROJECTS_TABLE_NAME, PROJECTS_CREATION_METHOD_COLUMN_NAME, VARCHAR, null, false);
+ }
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.v103;
+
+import java.sql.SQLException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.server.platform.db.migration.version.v103.AddCreationMethodColumnInProjectsTable.PROJECTS_TABLE_NAME;
+
+public class PopulateCreationMethodColumnInProjectsTableTest {
+
+ @Rule
+ public final CoreDbTester db = CoreDbTester.createForSchema(PopulateCreationMethodColumnInProjectsTableTest.class, "schema.sql");
+
+ private final PopulateCreationMethodColumnInProjectsTable underTest = new PopulateCreationMethodColumnInProjectsTable(db.database());
+
+ @Test
+ public void execute_whenProjectsTableIsEmpty_shouldDoNothing() throws SQLException {
+ underTest.execute();
+
+ assertThat(db.select("select creation_method from projects")).isEmpty();
+ }
+
+ @Test
+ public void execute_whenProjectsExist_shouldPopulateCreationMethodColumn() throws SQLException {
+ insertProject("uuid-1");
+ insertProject("uuid-2");
+
+ underTest.execute();
+
+ assertThat(db.select("select creation_method from projects"))
+ .extracting(stringObjectMap -> stringObjectMap.get("CREATION_METHOD"))
+ .containsExactlyInAnyOrder("unknown", "unknown");
+ }
+
+ @Test
+ public void execute_isReentrant() throws SQLException {
+ insertProject("uuid-1");
+
+ underTest.execute();
+ underTest.execute();
+
+ assertThat(db.select("select creation_method from projects"))
+ .extracting(stringObjectMap -> stringObjectMap.get("CREATION_METHOD"))
+ .containsExactlyInAnyOrder("unknown");
+ }
+
+ private void insertProject(String uuid) {
+ db.executeInsert(PROJECTS_TABLE_NAME,
+ "UUID", uuid,
+ "KEE", uuid,
+ "QUALIFIER", "TRK",
+ "PRIVATE", true,
+ "UPDATED_AT", 1);
+ }
+}
--- /dev/null
+CREATE TABLE "PROJECTS"(
+ "UUID" CHARACTER VARYING(40) NOT NULL,
+ "KEE" CHARACTER VARYING(400) NOT NULL,
+ "QUALIFIER" CHARACTER VARYING(10) NOT NULL,
+ "NAME" CHARACTER VARYING(2000),
+ "DESCRIPTION" CHARACTER VARYING(2000),
+ "PRIVATE" BOOLEAN NOT NULL,
+ "TAGS" CHARACTER VARYING(500),
+ "CREATED_AT" BIGINT,
+ "UPDATED_AT" BIGINT NOT NULL,
+ "NCLOC" BIGINT
+);
+ALTER TABLE "PROJECTS" ADD CONSTRAINT "PK_NEW_PROJECTS" PRIMARY KEY("UUID");
+CREATE UNIQUE NULLS DISTINCT INDEX "UNIQ_PROJECTS_KEE" ON "PROJECTS"("KEE" NULLS FIRST);
+CREATE INDEX "IDX_QUALIFIER" ON "PROJECTS"("QUALIFIER" NULLS FIRST);
--- /dev/null
+create TABLE "PROJECTS"(
+ "UUID" CHARACTER VARYING(40) NOT NULL,
+ "KEE" CHARACTER VARYING(400) NOT NULL,
+ "QUALIFIER" CHARACTER VARYING(10) NOT NULL,
+ "NAME" CHARACTER VARYING(2000),
+ "DESCRIPTION" CHARACTER VARYING(2000),
+ "PRIVATE" BOOLEAN NOT NULL,
+ "TAGS" CHARACTER VARYING(500),
+ "CREATED_AT" BIGINT,
+ "UPDATED_AT" BIGINT NOT NULL,
+ "NCLOC" BIGINT,
+ "CREATION_METHOD" CHARACTER VARYING(50)
+);
+alter table "PROJECTS" add CONSTRAINT "PK_NEW_PROJECTS" PRIMARY KEY("UUID");
+create unique NULLS DISTINCT INDEX "UNIQ_PROJECTS_KEE" on "PROJECTS"("KEE" NULLS FIRST);
+create index "IDX_QUALIFIER" on "PROJECTS"("QUALIFIER" NULLS FIRST);
--- /dev/null
+create TABLE "PROJECTS"(
+ "UUID" CHARACTER VARYING(40) NOT NULL,
+ "KEE" CHARACTER VARYING(400) NOT NULL,
+ "QUALIFIER" CHARACTER VARYING(10) NOT NULL,
+ "NAME" CHARACTER VARYING(2000),
+ "DESCRIPTION" CHARACTER VARYING(2000),
+ "PRIVATE" BOOLEAN NOT NULL,
+ "TAGS" CHARACTER VARYING(500),
+ "CREATED_AT" BIGINT,
+ "UPDATED_AT" BIGINT NOT NULL,
+ "NCLOC" BIGINT,
+ "CREATION_METHOD" CHARACTER VARYING(50)
+);
+alter table "PROJECTS" add CONSTRAINT "PK_NEW_PROJECTS" PRIMARY KEY("UUID");
+create unique NULLS DISTINCT INDEX "UNIQ_PROJECTS_KEE" on "PROJECTS"("KEE" NULLS FIRST);
+create index "IDX_QUALIFIER" on "PROJECTS"("QUALIFIER" NULLS FIRST);