.add(1509, "Populate columns ORGANIZATIONS.DEFAULT_PERM_TEMPLATE_*", PopulateDefaultPermTemplateColumnsOfOrganizations.class)
.add(1510, "Add ORGANIZATIONS.GUARDED", AddColumnGuardedToOrganizations.class)
.add(1511, "Populate ORGANIZATIONS.GUARDED", PopulateColumnGuardedOfOrganizations.class)
- .add(1512, "Make ORGANIZATIONS.GUARDED not nullable", MakeColumnGuardedOfOrganizationsNotNullable.class);
+ .add(1512, "Make ORGANIZATIONS.GUARDED not nullable", MakeColumnGuardedOfOrganizationsNotNullable.class)
+ .add(1513, "Make default organization guarded", MakeDefaultOrganizationGuarded.class)
+ ;
}
}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.v63;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.step.DataChange;
+
+public class MakeDefaultOrganizationGuarded extends DataChange {
+ private final DefaultOrganizationUuid defaultOrganizationUuid;
+
+ public MakeDefaultOrganizationGuarded(Database db, DefaultOrganizationUuid defaultOrganizationUuid) {
+ super(db);
+ this.defaultOrganizationUuid = defaultOrganizationUuid;
+ }
+
+ @Override
+ protected void execute(Context context) throws SQLException {
+ String uuid = this.defaultOrganizationUuid.getAndCheck(context);
+ context.prepareUpsert("update organizations set guarded=? where uuid=?")
+ .setBoolean(1, true)
+ .setString(2, uuid)
+ .execute()
+ .commit();
+ }
+}
@Test
public void verify_migration_count() {
- verifyMigrationCount(underTest, 13);
+ verifyMigrationCount(underTest, 14);
}
}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.v63;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class MakeDefaultOrganizationGuardedTest {
+
+ private static final String TABLE_ORGANIZATIONS = "organizations";
+ private static final String DEFAULT_ORGANIZATION_UUID = "def-org";
+
+ @Rule
+ public DbTester db = DbTester.createForSchema(System2.INSTANCE, MakeDefaultOrganizationGuardedTest.class, "organizations_and_internal_properties.sql");
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ private MakeDefaultOrganizationGuarded underTest = new MakeDefaultOrganizationGuarded(db.database(), new DefaultOrganizationUuidImpl());
+
+ @Test
+ public void fails_with_ISE_when_no_default_organization_is_set() throws SQLException {
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage("Default organization uuid is missing");
+
+ underTest.execute();
+ }
+
+ @Test
+ public void fails_with_ISE_when_default_organization_does_not_exist_in_table_ORGANIZATIONS() throws SQLException {
+ insertDefaultOrganizationUuid("blabla");
+
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage("Default organization with uuid 'blabla' does not exist in table ORGANIZATIONS");
+
+ underTest.execute();
+ }
+
+ @Test
+ public void execute_sets_guarded_of_non_guarded_default_organization_to_true() throws Exception {
+ setupDefaultOrganization();
+ assertThat(isDefaultOrganizationGuarded()).isFalse();
+
+ underTest.execute();
+
+ assertThat(isDefaultOrganizationGuarded()).isTrue();
+ }
+
+ @Test
+ public void execute_is_reentrant() throws Exception {
+ setupDefaultOrganization();
+
+ underTest.execute();
+
+ underTest.execute();
+
+ assertThat(isDefaultOrganizationGuarded()).isTrue();
+ }
+
+ private void setupDefaultOrganization() {
+ insertDefaultOrganizationUuid(DEFAULT_ORGANIZATION_UUID);
+ insertOrganization(DEFAULT_ORGANIZATION_UUID);
+ }
+
+ private boolean isDefaultOrganizationGuarded() throws Exception {
+ try (Connection connection = db.openConnection();
+ PreparedStatement selectGuardedForUuidStatement = createSelectGuardedForUuidStatement(connection, DEFAULT_ORGANIZATION_UUID);
+ ResultSet resultSet = selectGuardedForUuidStatement.executeQuery()) {
+ assertThat(resultSet.next()).isTrue();
+ boolean res = resultSet.getBoolean(1);
+ assertThat(resultSet.next()).isFalse();
+ return res;
+ }
+ }
+
+ private static PreparedStatement createSelectGuardedForUuidStatement(Connection connection, String uuid) throws Exception {
+ PreparedStatement preparedStatement = connection.prepareStatement("select guarded from organizations where uuid=?");
+ preparedStatement.setString(1, uuid);
+ return preparedStatement;
+ }
+
+ private void insertOrganization(String uuid) {
+ db.executeInsert(
+ TABLE_ORGANIZATIONS,
+ "UUID", uuid,
+ "KEE", uuid,
+ "NAME", uuid,
+ "GUARDED", String.valueOf(false),
+ "CREATED_AT", "1000",
+ "UPDATED_AT", "1000");
+ }
+
+ private void insertDefaultOrganizationUuid(String defaultOrganizationUuid) {
+ db.executeInsert(
+ "INTERNAL_PROPERTIES",
+ "KEE", "organization.default",
+ "IS_EMPTY", "false",
+ "TEXT_VALUE", defaultOrganizationUuid);
+ }
+}
--- /dev/null
+CREATE TABLE "ORGANIZATIONS" (
+ "UUID" VARCHAR(40) NOT NULL PRIMARY KEY,
+ "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),
+ "CREATED_AT" BIGINT NOT NULL,
+ "UPDATED_AT" BIGINT NOT NULL
+);
+CREATE UNIQUE INDEX "PK_ORGANIZATIONS" ON "ORGANIZATIONS" ("UUID");
+CREATE UNIQUE INDEX "ORGANIZATION_KEY" ON "ORGANIZATIONS" ("KEE");
+
+CREATE TABLE "INTERNAL_PROPERTIES" (
+ "KEE" VARCHAR(50) NOT NULL PRIMARY KEY,
+ "IS_EMPTY" BOOLEAN NOT NULL,
+ "TEXT_VALUE" VARCHAR(4000),
+ "CLOB_VALUE" CLOB,
+ "CREATED_AT" BIGINT
+);
+CREATE UNIQUE INDEX "UNIQ_INTERNAL_PROPERTIES" ON "INTERNAL_PROPERTIES" ("KEE");
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1510');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1511');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1512');
+INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1513');
INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, IS_ROOT, CREATED_AT, UPDATED_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', true, 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', true, '1418215735482', '1418215735482');
ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2;
-INSERT INTO ORGANIZATIONS (UUID, KEE, NAME, GUARDED, CREATED_AT, UPDATED_AT) VALUES ('AVdqnciQUUs7Zd3KPvFD', 'default-organization', 'Default Organization', false, '1474962596482', '1474962596482');
+INSERT INTO ORGANIZATIONS (UUID, KEE, NAME, GUARDED, CREATED_AT, UPDATED_AT) VALUES ('AVdqnciQUUs7Zd3KPvFD', 'default-organization', 'Default Organization', true, '1474962596482', '1474962596482');
INSERT INTO INTERNAL_PROPERTIES (KEE, IS_EMPTY, TEXT_VALUE, CREATED_AT) VALUES ('organization.default', false, 'AVdqnciQUUs7Zd3KPvFD', '1474962596482');
INSERT INTO GROUPS(ID, ORGANIZATION_UUID, NAME, DESCRIPTION, CREATED_AT, UPDATED_AT) VALUES (1, 'AVdqnciQUUs7Zd3KPvFD', 'sonar-administrators', 'System administrators', '2011-09-26 22:27:51.0', '2011-09-26 22:27:51.0');