From e45efa4d7b456420d4de92b0232e5b9338b3c3bc Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 2 Feb 2016 16:17:48 +0100 Subject: [PATCH] SONAR-7242 Add a migration to remove the preview permission --- .../migrate/1015_remove_preview_permission.rb | 31 +++++++++ .../org/sonar/db/version/DatabaseVersion.java | 2 +- .../sonar/db/version/MigrationStepModule.java | 4 +- .../version/v54/RemovePreviewPermission.java | 60 ++++++++++++++++ .../org/sonar/db/version/rows-h2.sql | 1 + .../db/version/MigrationStepModuleTest.java | 2 +- .../v54/RemovePreviewPermissionTest.java | 69 +++++++++++++++++++ .../migrate-result.xml | 14 ++++ .../RemovePreviewPermissionTest/migrate.xml | 15 ++++ .../RemovePreviewPermissionTest/schema.sql | 13 ++++ 10 files changed, 208 insertions(+), 3 deletions(-) create mode 100644 server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1015_remove_preview_permission.rb create mode 100644 sonar-db/src/main/java/org/sonar/db/version/v54/RemovePreviewPermission.java create mode 100644 sonar-db/src/test/java/org/sonar/db/version/v54/RemovePreviewPermissionTest.java create mode 100644 sonar-db/src/test/resources/org/sonar/db/version/v54/RemovePreviewPermissionTest/migrate-result.xml create mode 100644 sonar-db/src/test/resources/org/sonar/db/version/v54/RemovePreviewPermissionTest/migrate.xml create mode 100644 sonar-db/src/test/resources/org/sonar/db/version/v54/RemovePreviewPermissionTest/schema.sql 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 index 00000000000..a5de591571d --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1015_remove_preview_permission.rb @@ -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 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 @@ + + + + + + + + + + + + + + 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 @@ + + + + + + + + + + + + + + + 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 +); -- 2.39.5