From: Simon Brandhof Date: Wed, 7 Nov 2012 16:13:03 +0000 (+0100) Subject: SONAR-3940 property relocation X-Git-Tag: 3.4~358 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7264e00ff2f411e6dacd65e467968b1ff1134583;p=sonarqube.git SONAR-3940 property relocation --- diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java index a496d826fc6..32f3b60fa4b 100644 --- a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java +++ b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java @@ -19,6 +19,8 @@ */ package org.sonar.core.properties; +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; import org.apache.commons.lang.StringUtils; import org.apache.ibatis.session.SqlSession; import org.sonar.api.BatchComponent; @@ -131,4 +133,21 @@ public class PropertiesDao implements BatchComponent, ServerComponent { MyBatis.closeQuietly(session); } } + + public void renamePropertyKey(String oldKey, String newKey) { + Preconditions.checkArgument(!Strings.isNullOrEmpty(oldKey), "Old property key must not be empty"); + Preconditions.checkArgument(!Strings.isNullOrEmpty(newKey), "New property key must not be empty"); + + if (!newKey.equals(oldKey)) { + SqlSession session = mybatis.openSession(); + PropertiesMapper mapper = session.getMapper(PropertiesMapper.class); + try { + mapper.renamePropertyKey(oldKey, newKey); + session.commit(); + + } finally { + MyBatis.closeQuietly(session); + } + } + } } diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java index cb63e4bb227..c80a4d32c0f 100644 --- a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java @@ -34,4 +34,5 @@ public interface PropertiesMapper { void insert(PropertyDto property); void deleteGlobalProperty(String key); void deleteGlobalProperties(); + void renamePropertyKey(@Param("oldKey") String oldKey, @Param("newKey") String newKey); } diff --git a/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml b/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml index d3d55c0008f..756892b24f0 100644 --- a/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml @@ -72,4 +72,8 @@ delete from properties where resource_id is null and user_id is null + + update properties set prop_key = #{newKey} where prop_key=#{oldKey} + + diff --git a/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java b/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java index 6e7ad7fffa7..876d5d33c73 100644 --- a/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java @@ -21,7 +21,9 @@ package org.sonar.core.properties; import com.google.common.collect.ImmutableMap; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.sonar.core.persistence.AbstractDaoTestCase; import java.util.List; @@ -34,6 +36,9 @@ public class PropertiesDaoTest extends AbstractDaoTestCase { private PropertiesDao dao; + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void createDao() { dao = new PropertiesDao(getMyBatis()); @@ -132,6 +137,36 @@ public class PropertiesDaoTest extends AbstractDaoTestCase { checkTable("updateGlobalProperties", "properties", "prop_key", "text_value", "resource_id", "user_id"); } + @Test + public void renamePropertyKey() { + setupData("renamePropertyKey"); + + dao.renamePropertyKey("sonar.license.secured", "sonar.license"); + + checkTable("renamePropertyKey", "properties", "prop_key", "text_value", "resource_id", "user_id"); + } + + @Test + public void should_not_rename_if_same_key() { + setupData("should_not_rename_if_same_key"); + + dao.renamePropertyKey("foo", "foo"); + + checkTable("should_not_rename_if_same_key", "properties", "prop_key", "text_value", "resource_id", "user_id"); + } + + @Test + public void should_not_rename_with_empty_key() { + thrown.expect(IllegalArgumentException.class); + dao.renamePropertyKey("foo", ""); + } + + @Test + public void should_not_rename_an_empty_key() { + thrown.expect(IllegalArgumentException.class); + dao.renamePropertyKey(null, "foo"); + } + private PropertyDto findById(List properties, int id) { for (PropertyDto property : properties) { if (property.getId() == id) {