aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2016-02-02 16:17:48 +0100
committerJulien Lancelot <julien.lancelot@sonarsource.com>2016-02-03 14:52:41 +0100
commite45efa4d7b456420d4de92b0232e5b9338b3c3bc (patch)
tree2c7fdae24e3bf5e7b10dcebf7bd6cf3dbaab9aa8 /sonar-db
parent320223012bbfc3fcdfd8f8c5305c1ff2746c363a (diff)
downloadsonarqube-e45efa4d7b456420d4de92b0232e5b9338b3c3bc.tar.gz
sonarqube-e45efa4d7b456420d4de92b0232e5b9338b3c3bc.zip
SONAR-7242 Add a migration to remove the preview permission
Diffstat (limited to 'sonar-db')
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java2
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java4
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/v54/RemovePreviewPermission.java60
-rw-r--r--sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql1
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java2
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/v54/RemovePreviewPermissionTest.java69
-rw-r--r--sonar-db/src/test/resources/org/sonar/db/version/v54/RemovePreviewPermissionTest/migrate-result.xml14
-rw-r--r--sonar-db/src/test/resources/org/sonar/db/version/v54/RemovePreviewPermissionTest/migrate.xml15
-rw-r--r--sonar-db/src/test/resources/org/sonar/db/version/v54/RemovePreviewPermissionTest/schema.sql13
9 files changed, 177 insertions, 3 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java
index 04efac1b84f..96f236dcc49 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java
@@ -29,7 +29,7 @@ import org.sonar.db.MyBatis;
public class DatabaseVersion {
- public static final int LAST_VERSION = 1014;
+ public static final int LAST_VERSION = 1015;
/**
* The minimum supported version which can be upgraded. Lower
diff --git a/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java b/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java
index b9fa69f7d13..fc49e280351 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java
@@ -67,6 +67,7 @@ import org.sonar.db.version.v54.MigrateDisabledUsersToOnlyKeepLoginAndName;
import org.sonar.db.version.v54.MigrateQualityGatesConditions;
import org.sonar.db.version.v54.MigrateUsersIdentity;
import org.sonar.db.version.v54.RemoveComponentPageProperties;
+import org.sonar.db.version.v54.RemovePreviewPermission;
public class MigrationStepModule extends Module {
@Override
@@ -129,7 +130,8 @@ public class MigrationStepModule extends Module {
AddUsersIdentityColumns.class,
MigrateUsersIdentity.class,
MigrateQualityGatesConditions.class,
- MigrateDisabledUsersToOnlyKeepLoginAndName.class
+ MigrateDisabledUsersToOnlyKeepLoginAndName.class,
+ RemovePreviewPermission.class
);
}
}
diff --git a/sonar-db/src/main/java/org/sonar/db/version/v54/RemovePreviewPermission.java b/sonar-db/src/main/java/org/sonar/db/version/v54/RemovePreviewPermission.java
new file mode 100644
index 00000000000..3b0c600867a
--- /dev/null
+++ b/sonar-db/src/main/java/org/sonar/db/version/v54/RemovePreviewPermission.java
@@ -0,0 +1,60 @@
+/*
+ * 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.db.version.v54;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.db.version.BaseDataChange;
+import org.sonar.db.version.MassUpdate;
+import org.sonar.db.version.Select;
+import org.sonar.db.version.SqlStatement;
+
+/**
+ * Remove the "Execute Preview Analysis" (dryRunScan) permission
+ */
+public class RemovePreviewPermission extends BaseDataChange {
+
+ public RemovePreviewPermission(Database db) {
+ super(db);
+ }
+
+ @Override
+ public void execute(Context context) throws SQLException {
+ execute(context, "group_roles", "groups");
+ execute(context, "user_roles", "users");
+ }
+
+ private void execute(Context context, String tableName, String displayName) throws SQLException {
+ MassUpdate update = context.prepareMassUpdate().rowPluralName(displayName);
+ update.select("SELECT r.id FROM " + tableName + " r WHERE r.role=?").setString(1, "dryRunScan");
+ update.update("DELETE FROM " + tableName + " WHERE id=?");
+ update.execute(MigrationHandler.INSTANCE);
+ }
+
+ private enum MigrationHandler implements MassUpdate.Handler {
+ INSTANCE;
+
+ @Override
+ public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
+ update.setLong(1, row.getLong(1));
+ return true;
+ }
+ }
+}
diff --git a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql
index caa404a40ba..27cc3e73c39 100644
--- a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql
+++ b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql
@@ -375,6 +375,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1011');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1012');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1013');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1014');
+INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1015');
INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '1418215735482', '1418215735482', null, null);
ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2;
diff --git a/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java
index aceb2ae19e3..0378848ad85 100644
--- a/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java
@@ -29,6 +29,6 @@ public class MigrationStepModuleTest {
public void verify_count_of_added_MigrationStep_types() {
ComponentContainer container = new ComponentContainer();
new MigrationStepModule().configure(container);
- assertThat(container.size()).isEqualTo(49);
+ assertThat(container.size()).isEqualTo(50);
}
}
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v54/RemovePreviewPermissionTest.java b/sonar-db/src/test/java/org/sonar/db/version/v54/RemovePreviewPermissionTest.java
new file mode 100644
index 00000000000..2297f26df99
--- /dev/null
+++ b/sonar-db/src/test/java/org/sonar/db/version/v54/RemovePreviewPermissionTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.db.version.v54;
+
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+import org.sonar.db.version.MigrationStep;
+
+public class RemovePreviewPermissionTest {
+
+ static final String GROUP_ROLES_TABLE = "group_roles";
+ static final String USER_ROLES_TABLE = "user_roles";
+
+ @ClassRule
+ public static DbTester db = DbTester.createForSchema(System2.INSTANCE, RemovePreviewPermissionTest.class, "schema.sql");
+
+ MigrationStep migration;
+
+ @Before
+ public void setUp() {
+ db.executeUpdateSql("truncate table " + GROUP_ROLES_TABLE);
+ db.executeUpdateSql("truncate table " + USER_ROLES_TABLE);
+ migration = new RemovePreviewPermission(db.database());
+ }
+
+ @Test
+ public void migrate_empty_db() throws Exception {
+ migration.execute();
+ }
+
+ @Test
+ public void migrate() throws Exception {
+ db.prepareDbUnit(this.getClass(), "migrate.xml");
+
+ migration.execute();
+
+ db.assertDbUnit(getClass(), "migrate-result.xml", GROUP_ROLES_TABLE, USER_ROLES_TABLE);
+ }
+
+ @Test
+ public void nothing_to_do_on_already_migrated_data() throws Exception {
+ db.prepareDbUnit(this.getClass(), "migrate-result.xml");
+
+ migration.execute();
+
+ db.assertDbUnit(getClass(), "migrate-result.xml", GROUP_ROLES_TABLE, USER_ROLES_TABLE);
+ }
+
+}
diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v54/RemovePreviewPermissionTest/migrate-result.xml b/sonar-db/src/test/resources/org/sonar/db/version/v54/RemovePreviewPermissionTest/migrate-result.xml
new file mode 100644
index 00000000000..1f9d8f11e47
--- /dev/null
+++ b/sonar-db/src/test/resources/org/sonar/db/version/v54/RemovePreviewPermissionTest/migrate-result.xml
@@ -0,0 +1,14 @@
+<dataset>
+
+ <!-- Groups -->
+ <group_roles id="1" group_id="[null]" role="scan" resource_id="[null]"/>
+ <!--<group_roles id="2" group_id="[null]" role="dryRunScan" resource_id="[null]"/>-->
+
+ <group_roles id="3" group_id="102" role="admin" resource_id="1"/>
+ <!--<group_roles id="4" group_id="102" role="dryRunScan" resource_id="1"/>-->
+
+ <!-- Users -->
+ <user_roles id="1" user_id="200" role="admin" resource_id="[null]"/>
+ <!--<user_roles id="2" user_id="201" role="dryRunScan" resource_id="[null]"/>-->
+ <!--<user_roles id="3" user_id="201" role="dryRunScan" resource_id="1"/>-->
+</dataset>
diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v54/RemovePreviewPermissionTest/migrate.xml b/sonar-db/src/test/resources/org/sonar/db/version/v54/RemovePreviewPermissionTest/migrate.xml
new file mode 100644
index 00000000000..d9e62c305ee
--- /dev/null
+++ b/sonar-db/src/test/resources/org/sonar/db/version/v54/RemovePreviewPermissionTest/migrate.xml
@@ -0,0 +1,15 @@
+<dataset>
+
+ <!-- Groups -->
+ <group_roles id="1" group_id="[null]" role="scan" resource_id="[null]"/>
+ <group_roles id="2" group_id="[null]" role="dryRunScan" resource_id="[null]"/>
+
+ <group_roles id="3" group_id="102" role="admin" resource_id="1"/>
+ <group_roles id="4" group_id="102" role="dryRunScan" resource_id="1"/>
+
+ <!-- Users -->
+ <user_roles id="1" user_id="200" role="admin" resource_id="[null]"/>
+ <user_roles id="2" user_id="201" role="dryRunScan" resource_id="[null]"/>
+ <user_roles id="3" user_id="201" role="dryRunScan" resource_id="1"/>
+
+</dataset>
diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v54/RemovePreviewPermissionTest/schema.sql b/sonar-db/src/test/resources/org/sonar/db/version/v54/RemovePreviewPermissionTest/schema.sql
new file mode 100644
index 00000000000..eca905e2c3d
--- /dev/null
+++ b/sonar-db/src/test/resources/org/sonar/db/version/v54/RemovePreviewPermissionTest/schema.sql
@@ -0,0 +1,13 @@
+CREATE TABLE "GROUP_ROLES" (
+ "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+ "GROUP_ID" INTEGER,
+ "RESOURCE_ID" INTEGER,
+ "ROLE" VARCHAR(64) NOT NULL
+);
+
+CREATE TABLE "USER_ROLES" (
+ "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+ "USER_ID" INTEGER,
+ "RESOURCE_ID" INTEGER,
+ "ROLE" VARCHAR(64) NOT NULL
+);