From 6eb670a7d276a1a0f046a5f1925f29e1727eec23 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Thu, 26 Mar 2015 14:24:02 +0100 Subject: [PATCH] SONAR-6325 Apply feedback from PR --- .../v52/MoveProjectProfileAssociation.java | 6 +++--- .../qualityprofile/RegisterQualityProfiles.java | 8 +------- .../ws/ProjectAssociationActions.java | 12 +++++++----- .../v52/MoveProjectProfileAssociationTest.java | 6 +++--- .../migrate-result.xml | 2 +- .../MoveProjectProfileAssociationTest/schema.sql | 2 +- .../db/migrate/906_create_project_profiles.rb | 4 ++-- .../sonar/core/persistence/DatabaseVersion.java | 2 +- .../org/sonar/core/persistence/schema-h2.ddl | 4 ++-- .../qualityprofile/db/QualityProfileMapper.xml | 14 +++++++------- .../db/QualityProfileDaoTest/projects.xml | 8 ++++---- 11 files changed, 32 insertions(+), 36 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v52/MoveProjectProfileAssociation.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v52/MoveProjectProfileAssociation.java index 00bf3394e1b..8fa506e48e7 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v52/MoveProjectProfileAssociation.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v52/MoveProjectProfileAssociation.java @@ -54,14 +54,14 @@ public class MoveProjectProfileAssociation extends BaseDataChange { Long id = row.getLong(1); String profileLanguage = extractLanguage(row.getString(2)); String profileName = row.getString(3); - Long projectId = row.getLong(4); + Long projectId = row.getNullableLong(4); String projectUuid = row.getString(5); if (profileKeysByLanguageThenName.contains(profileLanguage, profileName)) { String profileKey = profileKeysByLanguageThenName.get(profileLanguage, profileName); if (projectUuid == null) { - if (projectId == null || projectId == 0L) { + if (projectId == null) { setDefaultProfile.setBoolean(1, true).setString(2, profileKey).execute(); } else { LOGGER.warn(String.format("Profile with language '%s' and name '%s' is associated with unknown project '%d', ignored", profileLanguage, profileName, projectId)); @@ -98,7 +98,7 @@ public class MoveProjectProfileAssociation extends BaseDataChange { massUpdate.update("DELETE FROM properties WHERE id = ?"); final Upsert setDefaultProfile = context.prepareUpsert("UPDATE rules_profiles SET is_default = ? WHERE kee = ?"); - final Upsert associateProjectToProfile = context.prepareUpsert("INSERT INTO project_profiles (project_uuid, profile_key) VALUES (?, ?)"); + final Upsert associateProjectToProfile = context.prepareUpsert("INSERT INTO project_qprofiles (project_uuid, profile_key) VALUES (?, ?)"); try { massUpdate.execute(new ProjectProfileAssociationHandler(setDefaultProfile, associateProjectToProfile, profileKeysByLanguageThenName)); diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java index 1eb5b2b5e5b..b34ff6244de 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java @@ -53,7 +53,6 @@ public class RegisterQualityProfiles implements ServerComponent { private static final Logger LOGGER = Loggers.get(RegisterQualityProfiles.class); private static final String DEFAULT_PROFILE_NAME = "Sonar way"; - private final PersistentSettings settings; private final List definitions; private final BuiltInProfiles builtInProfiles; private final DbClient dbClient; @@ -72,7 +71,6 @@ public class RegisterQualityProfiles implements ServerComponent { public RegisterQualityProfiles(PersistentSettings settings, BuiltInProfiles builtInProfiles, DbClient dbClient, QProfileFactory profileFactory, RuleActivator ruleActivator, List definitions, Languages languages) { - this.settings = settings; this.builtInProfiles = builtInProfiles; this.dbClient = dbClient; this.profileFactory = profileFactory; @@ -151,13 +149,9 @@ public class RegisterQualityProfiles implements ServerComponent { } private void setDefault(String language, List profileDefs, DbSession session) { - boolean upToDate = false; QualityProfileDto currentDefault = dbClient.qualityProfileDao().getDefaultProfile(language, session); - if (currentDefault != null) { - upToDate = true; - } - if (!upToDate) { + if (currentDefault == null) { String defaultProfileName = nameOfDefaultProfile(profileDefs); LOGGER.info("Set default " + language + " profile: " + defaultProfileName); QualityProfileDto newDefaultProfile = dbClient.qualityProfileDao().getByNameAndLanguage(defaultProfileName, language, session); diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ProjectAssociationActions.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ProjectAssociationActions.java index 2760559bd38..8678d5a785c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ProjectAssociationActions.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ProjectAssociationActions.java @@ -19,7 +19,6 @@ */ package org.sonar.server.qualityprofile.ws; -import com.google.common.base.Function; import com.google.common.base.Preconditions; import com.google.common.collect.Collections2; import org.sonar.api.ServerComponent; @@ -27,6 +26,7 @@ import org.sonar.api.resources.Language; import org.sonar.api.resources.Languages; import org.sonar.api.server.ws.*; import org.sonar.api.server.ws.WebService.NewAction; +import org.sonar.core.util.NonNullInputFunction; import org.sonar.server.component.ComponentService; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.qualityprofile.QProfile; @@ -36,6 +36,8 @@ import org.sonar.server.user.UserSession; import java.util.Arrays; +import static org.apache.commons.lang.StringUtils.isEmpty; + public class ProjectAssociationActions implements ServerComponent { private static final String PARAM_LANGUAGE = "language"; @@ -92,9 +94,9 @@ public class ProjectAssociationActions implements ServerComponent { .setDescription("A quality profile name. If this parameter is set, profileKey must not be set and language must be set to disambiguate"); action.createParam(PARAM_LANGUAGE) .setDescription("A quality profile language. If this parameter is set, profileKey must not be set and language must be set to disambiguate") - .setPossibleValues(Collections2.transform(Arrays.asList(languages.all()), new Function() { + .setPossibleValues(Collections2.transform(Arrays.asList(languages.all()), new NonNullInputFunction() { @Override - public String apply(Language input) { + public String doApply(Language input) { return input.getKey(); } })); @@ -121,8 +123,8 @@ public class ProjectAssociationActions implements ServerComponent { String projectUuid = request.param(PARAM_PROJECT_UUID); Preconditions.checkArgument( - (language != null && profileName != null) ^ profileKey != null, "Either profileKey or profileName + language must be set"); - Preconditions.checkArgument(projectKey != null ^ projectUuid != null, "Either projectKey or projectUuid must be set"); + (!isEmpty(language) && !isEmpty(profileName)) ^ !isEmpty(profileKey), "Either profileKey or profileName + language must be set"); + Preconditions.checkArgument(!isEmpty(projectKey) ^ !isEmpty(projectUuid), "Either projectKey or projectUuid must be set"); if(profileKey == null) { profileKey = getProfileKeyFromLanguageAndName(language, profileName); diff --git a/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v52/MoveProjectProfileAssociationTest.java b/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v52/MoveProjectProfileAssociationTest.java index 8857902e2bc..a571bd04123 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v52/MoveProjectProfileAssociationTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v52/MoveProjectProfileAssociationTest.java @@ -36,7 +36,7 @@ public class MoveProjectProfileAssociationTest { @Before public void setUp() throws Exception { db.executeUpdateSql("truncate table projects"); - db.executeUpdateSql("truncate table project_profiles"); + db.executeUpdateSql("truncate table project_qprofiles"); db.executeUpdateSql("truncate table properties"); db.executeUpdateSql("truncate table rules_profiles"); @@ -52,14 +52,14 @@ public class MoveProjectProfileAssociationTest { public void migrate() throws Exception { db.prepareDbUnit(this.getClass(), "migrate.xml"); migration.execute(); - db.assertDbUnit(this.getClass(), "migrate-result.xml", "rules_profiles", "project_profiles"); + db.assertDbUnit(this.getClass(), "migrate-result.xml", "rules_profiles", "project_qprofiles"); } @Test public void not_migrate_already_migrated_data() throws Exception { db.prepareDbUnit(this.getClass(), "migrate-result.xml"); migration.execute(); - db.assertDbUnit(this.getClass(), "migrate-result.xml", "rules_profiles", "project_profiles"); + db.assertDbUnit(this.getClass(), "migrate-result.xml", "rules_profiles", "project_qprofiles"); } } diff --git a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v52/MoveProjectProfileAssociationTest/migrate-result.xml b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v52/MoveProjectProfileAssociationTest/migrate-result.xml index c92078c64de..7c930e3c197 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v52/MoveProjectProfileAssociationTest/migrate-result.xml +++ b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v52/MoveProjectProfileAssociationTest/migrate-result.xml @@ -23,6 +23,6 @@ - + \ No newline at end of file diff --git a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v52/MoveProjectProfileAssociationTest/schema.sql b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v52/MoveProjectProfileAssociationTest/schema.sql index f0e98d0690e..004569aacfe 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v52/MoveProjectProfileAssociationTest/schema.sql +++ b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v52/MoveProjectProfileAssociationTest/schema.sql @@ -41,7 +41,7 @@ CREATE TABLE "PROPERTIES" ( "USER_ID" INTEGER ); -CREATE TABLE "PROJECT_PROFILES" ( +CREATE TABLE "PROJECT_QPROFILES" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "PROJECT_UUID" VARCHAR(50) NOT NULL, "PROFILE_KEY" VARCHAR(255) NOT NULL diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/906_create_project_profiles.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/906_create_project_profiles.rb index ec2766c2307..6dbec9f9a3c 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/906_create_project_profiles.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/906_create_project_profiles.rb @@ -24,12 +24,12 @@ class CreateProjectProfiles < ActiveRecord::Migration def self.up - create_table :project_profiles do |t| + create_table :project_qprofiles do |t| t.column :project_uuid, :string, :limit => 50, :null => false t.column :profile_key, :string, :limit => 255, :null => false end - add_index 'project_profiles', ['project_uuid', 'profile_key'], :unique => true, :name => 'uniq_project_profiles' + add_index 'project_qprofiles', ['project_uuid', 'profile_key'], :unique => true, :name => 'uniq_project_qprofiles' end end 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 2e2c8fb1f4c..78d238fd14a 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 @@ -76,7 +76,7 @@ public class DatabaseVersion implements BatchComponent, ServerComponent { "projects", "project_links", "project_measures", - "project_profiles", + "project_qprofiles", "properties", "resource_index", "rules", 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 12207946a88..5ebe86e0e1b 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 @@ -56,7 +56,7 @@ CREATE TABLE "RULES_PROFILES" ( "UPDATED_AT" TIMESTAMP ); -CREATE TABLE "PROJECT_PROFILES" ( +CREATE TABLE "PROJECT_QPROFILES" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "PROJECT_UUID" VARCHAR(50) NOT NULL, "PROFILE_KEY" VARCHAR(255) NOT NULL @@ -718,4 +718,4 @@ CREATE UNIQUE INDEX "FILE_SOURCES_FILE_UUID_UNIQ" ON "FILE_SOURCES" ("FILE_UUID" CREATE INDEX "FILE_SOURCES_UPDATED_AT" ON "FILE_SOURCES" ("UPDATED_AT"); -CREATE UNIQUE INDEX "PROJECT_PROFILES_UNIQUE" ON "PROJECT_PROFILES" ("PROJECT_UUID", "PROFILE_KEY"); +CREATE UNIQUE INDEX "PROJECT_QPROFILES_UNIQUE" ON "PROJECT_QPROFILES" ("PROJECT_UUID", "PROFILE_KEY"); diff --git a/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/QualityProfileMapper.xml b/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/QualityProfileMapper.xml index 8d76a86144f..8f069d0b366 100644 --- a/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/QualityProfileMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/QualityProfileMapper.xml @@ -95,7 +95,7 @@ SELECT count(projects.id) FROM projects projects - JOIN project_profiles pp ON pp.project_uuid=projects.uuid + JOIN project_qprofiles pp ON pp.project_uuid=projects.uuid JOIN rules_profiles prof ON pp.profile_key=prof.kee AND prof.language=#{language} @@ -117,7 +117,7 @@ SELECT FROM rules_profiles p - JOIN project_profiles pp ON pp.profile_key=p.kee + JOIN project_qprofiles pp ON pp.profile_key=p.kee JOIN projects project ON pp.project_uuid=project.uuid AND project.kee=#{projectKey} WHERE p.language=#{language} - INSERT INTO project_profiles (project_uuid, profile_key) VALUES (#{projectUuid}, #{profileKey}) + INSERT INTO project_qprofiles (project_uuid, profile_key) VALUES (#{projectUuid}, #{profileKey}) - DELETE FROM project_profiles WHERE project_uuid=#{projectUuid} AND profile_key=#{profileKey} + DELETE FROM project_qprofiles WHERE project_uuid=#{projectUuid} AND profile_key=#{profileKey} - DELETE FROM project_profiles WHERE profile_key=#{profileKey} + DELETE FROM project_qprofiles WHERE profile_key=#{profileKey} diff --git a/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/projects.xml b/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/projects.xml index 5cc32bfefad..8e6fb4c83af 100644 --- a/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/projects.xml +++ b/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/projects.xml @@ -8,9 +8,9 @@ - - - - + + + + -- 2.39.5