]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7242 Add a migration to remove the preview permission 754/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 2 Feb 2016 15:17:48 +0000 (16:17 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 3 Feb 2016 13:52:41 +0000 (14:52 +0100)
server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1015_remove_preview_permission.rb [new file with mode: 0644]
sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java
sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java
sonar-db/src/main/java/org/sonar/db/version/v54/RemovePreviewPermission.java [new file with mode: 0644]
sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql
sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java
sonar-db/src/test/java/org/sonar/db/version/v54/RemovePreviewPermissionTest.java [new file with mode: 0644]
sonar-db/src/test/resources/org/sonar/db/version/v54/RemovePreviewPermissionTest/migrate-result.xml [new file with mode: 0644]
sonar-db/src/test/resources/org/sonar/db/version/v54/RemovePreviewPermissionTest/migrate.xml [new file with mode: 0644]
sonar-db/src/test/resources/org/sonar/db/version/v54/RemovePreviewPermissionTest/schema.sql [new file with mode: 0644]

diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1015_remove_preview_permission.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1015_remove_preview_permission.rb
new file mode 100644 (file)
index 0000000..a5de591
--- /dev/null
@@ -0,0 +1,31 @@
+#
+# SonarQube, open source software quality management tool.
+# Copyright (C) 2008-2014 SonarSource
+# mailto:contact AT sonarsource DOT com
+#
+# SonarQube 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.
+#
+# SonarQube 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.
+#
+
+#
+# SonarQube 5.4
+# SONAR-7242
+#
+class RemovePreviewPermission < ActiveRecord::Migration
+
+  def self.up
+    execute_java_migration('org.sonar.db.version.v54.RemovePreviewPermission')
+  end
+
+end
index 04efac1b84fc1507bcc6e722a1f2240360d7b6e0..96f236dcc491485a414c6642a70a700e909d2440 100644 (file)
@@ -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
index b9fa69f7d13b202697c98e0e8d3c95971fc02b19..fc49e280351d348829e89398e136e47f5c62853d 100644 (file)
@@ -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 (file)
index 0000000..3b0c600
--- /dev/null
@@ -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;
+    }
+  }
+}
index caa404a40bafa2a02edbc84b1393efb69464eb75..27cc3e73c397d461830dcc8b38829952e871d475 100644 (file)
@@ -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;
index aceb2ae19e389d522730e595cb788b0957684429..0378848ad8538bcb9157ce2d0ff2938b7be63f6d 100644 (file)
@@ -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 (file)
index 0000000..2297f26
--- /dev/null
@@ -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 (file)
index 0000000..1f9d8f1
--- /dev/null
@@ -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 (file)
index 0000000..d9e62c3
--- /dev/null
@@ -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 (file)
index 0000000..eca905e
--- /dev/null
@@ -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
+);