diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-12-17 23:53:03 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-12-17 23:53:03 +0100 |
commit | 362a1e6070184c58cbb8d0f6a252c26260367065 (patch) | |
tree | 124d05f041926665aa0c73a4b2e07798dcb687fc | |
parent | 97ecdc3d58f85ddc2914a5730dbb06dafcd16b8c (diff) | |
download | sonarqube-362a1e6070184c58cbb8d0f6a252c26260367065.tar.gz sonarqube-362a1e6070184c58cbb8d0f6a252c26260367065.zip |
SONAR-4950 Create an unique constraint on the "group_roles" table to prevent having two rows with same group_id, resource_id and role
4 files changed, 48 insertions, 1 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java index 79b894f1677..f9785136f17 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java @@ -33,7 +33,7 @@ import java.util.List; */ public class DatabaseVersion implements BatchComponent, ServerComponent { - public static final int LAST_VERSION = 481; + public static final int LAST_VERSION = 482; public static enum Status { UP_TO_DATE, REQUIRES_UPGRADE, REQUIRES_DOWNGRADE, FRESH_INSTALL diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql index 6903625df31..3c128f020a1 100644 --- a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql @@ -192,6 +192,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('465'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('466'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('480'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('481'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('482'); INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '2011-09-26 22:27:48.0', '2011-09-26 22:27:48.0', null, null); ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2; diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl b/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl index f0ebea6fca1..670337719f1 100644 --- a/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl @@ -690,6 +690,8 @@ CREATE INDEX "SNAPSHOTS_ROOT_PROJECT_ID" ON "SNAPSHOTS" ("ROOT_PROJECT_ID"); CREATE INDEX "GROUP_ROLES_ROLE" ON "GROUP_ROLES" ("ROLE"); +CREATE UNIQUE INDEX "GROUP_ROLES_UNIQUE" ON "GROUP_ROLES" ("GROUP_ID", "RESOURCE_ID", "ROLE"); + CREATE UNIQUE INDEX "RULES_PLUGIN_KEY_AND_NAME" ON "RULES" ("PLUGIN_RULE_KEY", "PLUGIN_NAME"); CREATE INDEX "CHARACTERISTICS_ENABLED" ON "CHARACTERISTICS" ("ENABLED"); diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/482_add_unique_constraint_to_group_roles.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/482_add_unique_constraint_to_group_roles.rb new file mode 100644 index 00000000000..62752d2ad27 --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/482_add_unique_constraint_to_group_roles.rb @@ -0,0 +1,44 @@ +# +# SonarQube, open source software quality management tool. +# Copyright (C) 2008-2013 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 4.2 +# See SONAR-4950 +# +class AddUniqueConstraintToGroupRoles < ActiveRecord::Migration + + class GroupRole < ActiveRecord::Base + end + + def self.up + GroupRole.reset_column_information + + duplicated_ids = ActiveRecord::Base.connection.select_rows('select group_id,resource_id,role from group_roles group by group_id,resource_id,role having count(*) > 1') + say_with_time "Remove #{duplicated_ids.size} duplicated group roles" do + duplicated_ids.each do |fields| + rows = GroupRole.find(:all, :conditions => {:group_id => fields[0], :resource_id => fields[1], :role => fields[2]}) + # delete all rows except the last one + rows[0...-1].each do |row| + GroupRole.delete(row.id) + end + end + end + end +end |