diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v74/DbVersion74.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v74/DbVersion74.java index bc09170bbcde..285adb6a2f47 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v74/DbVersion74.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v74/DbVersion74.java @@ -46,6 +46,9 @@ public void addSteps(MigrationStepRegistry registry) { .add(2323, "Clean orphans from deleted branches and PRs in CE_* tables", CleanOrphanRowsInCeTables.class) .add(2324, "Create new creator permissions for applications and portfolios", CreateApplicationsAndPortfoliosCreatorPermissions.class) .add(2325, "Add default templates for applications and portfolios", AddDefaultPermTemplateColumnsToOrganizations.class) + .add(2326, "Create new creator permissions for applications and portfolios", CreateApplicationsAndPortfoliosCreatorPermissions.class) + .add(2327, "Add default templates for applications and portfolios", AddDefaultPermTemplateColumnsToOrganizations.class) + .add(2328, "Populate default template permissions on organizations", PopulateDefaultPermTemplateOnOrganizations.class) ; } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v74/PopulateDefaultPermTemplateOnOrganizations.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v74/PopulateDefaultPermTemplateOnOrganizations.java new file mode 100644 index 000000000000..1efe3d706213 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v74/PopulateDefaultPermTemplateOnOrganizations.java @@ -0,0 +1,57 @@ +/* + * 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.v74; + +import java.sql.SQLException; +import org.sonar.api.utils.System2; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.SupportsBlueGreen; +import org.sonar.server.platform.db.migration.step.DataChange; +import org.sonar.server.platform.db.migration.step.MassUpdate; + +@SupportsBlueGreen +public class PopulateDefaultPermTemplateOnOrganizations extends DataChange { + + private final System2 system2; + + public PopulateDefaultPermTemplateOnOrganizations(Database db, System2 system2) { + super(db); + this.system2 = system2; + } + + @Override + protected void execute(Context context) throws SQLException { + long now = system2.now(); + MassUpdate massUpdate = context.prepareMassUpdate().rowPluralName("organizations"); + massUpdate.select("SELECT uuid, default_perm_template_view FROM organizations WHERE default_perm_template_view IS NOT NULL"); + massUpdate.update("UPDATE organizations " + + "SET default_perm_template_app=?, default_perm_template_port=?, updated_at=? " + + "WHERE uuid=?"); + massUpdate.execute((row, update) -> { + String uuid = row.getString(1); + String defaultPermTemplateView = row.getString(2); + update.setString(1, defaultPermTemplateView); + update.setString(2, defaultPermTemplateView); + update.setLong(3, now); + update.setString(4, uuid); + return true; + }); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v74/DbVersion74Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v74/DbVersion74Test.java index 5d511904774c..c0bc4ecb92d0 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v74/DbVersion74Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v74/DbVersion74Test.java @@ -35,6 +35,6 @@ public void migrationNumber_starts_at_2300() { @Test public void verify_migration_count() { - verifyMigrationCount(underTest, 19); + verifyMigrationCount(underTest, 22); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v74/PopulateDefaultPermTemplateOnOrganizationsTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v74/PopulateDefaultPermTemplateOnOrganizationsTest.java new file mode 100644 index 000000000000..7e856f92bc2f --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v74/PopulateDefaultPermTemplateOnOrganizationsTest.java @@ -0,0 +1,109 @@ +/* + * 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.v74; + +import java.sql.SQLException; +import java.util.stream.Collectors; +import javax.annotation.Nullable; +import org.assertj.core.groups.Tuple; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.utils.System2; +import org.sonar.api.utils.internal.TestSystem2; +import org.sonar.db.CoreDbTester; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; + +public class PopulateDefaultPermTemplateOnOrganizationsTest { + + private final static long PAST = 10_000_000_000L; + private final static long NOW = 50_000_000_000L; + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(PopulateDefaultPermTemplateOnOrganizationsTest.class, "organizations.sql"); + private System2 system2 = new TestSystem2().setNow(NOW); + + private PopulateDefaultPermTemplateOnOrganizations underTest = new PopulateDefaultPermTemplateOnOrganizations(db.database(), system2); + + @Test + public void test_is_reentrant() throws SQLException { + insertOrganization("aa", "aa-1"); + insertOrganization("bb", null); + underTest.execute(); + underTest.execute(); + + assertOrganizations( + tuple("aa", "aa-1", "aa-1", "aa-1", NOW), + tuple("bb", null, null, null, PAST) + ); + } + + @Test + public void test_with_organizations() throws SQLException { + insertOrganization("aa", "aa-1"); + insertOrganization("bb", "bb-1"); + insertOrganization("cc", null); + + underTest.execute(); + + assertOrganizations( + tuple("aa", "aa-1", "aa-1", "aa-1", NOW), + tuple("bb", "bb-1", "bb-1", "bb-1", NOW), + tuple("cc", null, null, null, PAST) + ); + } + + @Test + public void without_governance_no_modifications() throws SQLException { + insertOrganization("default-organization", null); + + underTest.execute(); + + assertOrganizations( + tuple("default-organization", null, null, null, PAST) + ); + } + + private void assertOrganizations(Tuple... expectedTuples) { + assertThat(db.select("SELECT uuid, default_perm_template_view, default_perm_template_app, default_perm_template_port, updated_at FROM organizations") + .stream() + .map(row -> new Tuple(row.get("UUID"), row.get("DEFAULT_PERM_TEMPLATE_VIEW"), row.get("DEFAULT_PERM_TEMPLATE_APP"), row.get("DEFAULT_PERM_TEMPLATE_PORT"), row.get("UPDATED_AT"))) + .collect(Collectors.toList())) + .containsExactlyInAnyOrder(expectedTuples); + } + + private void insertOrganization(String uuid, @Nullable String defaultPermTemplateView) { + db.executeInsert("ORGANIZATIONS", + "UUID", uuid, + "KEE", uuid, + "NAME", uuid, + "GUARDED", false, + "DEFAULT_PERM_TEMPLATE_VIEW", defaultPermTemplateView, + "DEFAULT_QUALITY_GATE_UUID", "111", + "NEW_PROJECT_PRIVATE", false, + "SUBSCRIPTION", "sonarqube", + "CREATED_AT", PAST, + "UPDATED_AT", PAST); + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v74/PopulateDefaultPermTemplateOnOrganizationsTest/organizations.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v74/PopulateDefaultPermTemplateOnOrganizationsTest/organizations.sql new file mode 100644 index 000000000000..d5bcfd25874c --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v74/PopulateDefaultPermTemplateOnOrganizationsTest/organizations.sql @@ -0,0 +1,22 @@ +CREATE TABLE "ORGANIZATIONS" ( + "UUID" VARCHAR(40) NOT NULL, + "KEE" VARCHAR(32) NOT NULL, + "NAME" VARCHAR(64) NOT NULL, + "DESCRIPTION" VARCHAR(256), + "URL" VARCHAR(256), + "AVATAR_URL" VARCHAR(256), + "GUARDED" BOOLEAN NOT NULL, + "DEFAULT_PERM_TEMPLATE_PROJECT" VARCHAR(40), + "DEFAULT_PERM_TEMPLATE_VIEW" VARCHAR(40), + "DEFAULT_PERM_TEMPLATE_APP" VARCHAR(40), + "DEFAULT_PERM_TEMPLATE_PORT" VARCHAR(40), + "DEFAULT_GROUP_ID" INTEGER, + "DEFAULT_QUALITY_GATE_UUID" VARCHAR(40) NOT NULL, + "NEW_PROJECT_PRIVATE" BOOLEAN NOT NULL, + "SUBSCRIPTION" VARCHAR(40) NOT NULL, + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL, + + CONSTRAINT "PK_ORGANIZATIONS" PRIMARY KEY ("UUID") +); +CREATE UNIQUE INDEX "ORGANIZATION_KEY" ON "ORGANIZATIONS" ("KEE");