diff options
author | Pierre <pierre.guillot@sonarsource.com> | 2020-04-07 14:46:34 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2020-05-25 20:05:21 +0000 |
commit | 370f5d3e2052a6cf4920b1ee10b1fba34eb2f11a (patch) | |
tree | 1d0cd4b717a1cdf927cc9abddf7a4333cab79964 /server/sonar-db-dao/src | |
parent | 94711e1ee7271e44e2061c3a2916ebb77aa4c956 (diff) | |
download | sonarqube-370f5d3e2052a6cf4920b1ee10b1fba34eb2f11a.tar.gz sonarqube-370f5d3e2052a6cf4920b1ee10b1fba34eb2f11a.zip |
SONAR-13221 change PK of properties
Diffstat (limited to 'server/sonar-db-dao/src')
8 files changed, 129 insertions, 108 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertiesDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertiesDao.java index 53a1db397e5..1d304cf3ff7 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertiesDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertiesDao.java @@ -31,8 +31,10 @@ import java.util.Map; import java.util.Set; import javax.annotation.CheckForNull; import javax.annotation.Nullable; +import org.apache.ibatis.annotations.Param; import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; +import org.sonar.core.util.UuidFactory; import org.sonar.db.Dao; import org.sonar.db.DbSession; import org.sonar.db.EmailSubscriberDto; @@ -51,10 +53,12 @@ public class PropertiesDao implements Dao { private final MyBatis mybatis; private final System2 system2; + private UuidFactory uuidFactory; - public PropertiesDao(MyBatis mybatis, System2 system2) { + public PropertiesDao(MyBatis mybatis, System2 system2, UuidFactory uuidFactory) { this.mybatis = mybatis; this.system2 = system2; + this.uuidFactory = uuidFactory; } /** @@ -203,18 +207,19 @@ public class PropertiesDao implements Dao { save(getMapper(session), property.getKey(), property.getUserId(), property.getComponentUuid(), property.getValue()); } - private void save(PropertiesMapper mapper, - String key, @Nullable Integer userId, @Nullable String componentUuid, @Nullable String value) { + private void save(PropertiesMapper mapper, String key, + @Nullable Integer userId, @Nullable String componentUuid, @Nullable String value) { checkKey(key); long now = system2.now(); mapper.delete(key, userId, componentUuid); + String uuid = uuidFactory.create(); if (isEmpty(value)) { - mapper.insertAsEmpty(key, userId, componentUuid, now); + mapper.insertAsEmpty(uuid, key, userId, componentUuid, now); } else if (mustBeStoredInClob(value)) { - mapper.insertAsClob(key, userId, componentUuid, value, now); + mapper.insertAsClob(uuid, key, userId, componentUuid, value, now); } else { - mapper.insertAsText(key, userId, componentUuid, value, now); + mapper.insertAsText(uuid, key, userId, componentUuid, value, now); } } @@ -286,13 +291,13 @@ public class PropertiesDao implements Dao { } public void deleteByOrganizationAndUser(DbSession dbSession, String organizationUuid, int userId) { - List<Long> ids = getMapper(dbSession).selectIdsByOrganizationAndUser(organizationUuid, userId); - executeLargeInputsWithoutOutput(ids, subList -> getMapper(dbSession).deleteByIds(subList)); + List<String> uuids = getMapper(dbSession).selectUuidsByOrganizationAndUser(organizationUuid, userId); + executeLargeInputsWithoutOutput(uuids, subList -> getMapper(dbSession).deleteByUuids(subList)); } public void deleteByOrganizationAndMatchingLogin(DbSession dbSession, String organizationUuid, String login, List<String> propertyKeys) { - List<Long> ids = getMapper(dbSession).selectIdsByOrganizationAndMatchingLogin(organizationUuid, login, propertyKeys); - executeLargeInputsWithoutOutput(ids, list -> getMapper(dbSession).deleteByIds(list)); + List<String> uuids = getMapper(dbSession).selectIdsByOrganizationAndMatchingLogin(organizationUuid, login, propertyKeys); + executeLargeInputsWithoutOutput(uuids, list -> getMapper(dbSession).deleteByUuids(list)); } public void deleteByKeyAndValue(DbSession dbSession, String key, String value) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertiesMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertiesMapper.java index 5e8c4912860..f7c3c4b029a 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertiesMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertiesMapper.java @@ -50,18 +50,18 @@ public interface PropertiesMapper { List<PropertyDto> selectByKeyAndMatchingValue(@Param("key") String key, @Param("value") String value); - List<Long> selectIdsByOrganizationAndUser(@Param("organizationUuid") String organizationUuid, @Param("userId") int userId); + List<String> selectUuidsByOrganizationAndUser(@Param("organizationUuid") String organizationUuid, @Param("userId") int userId); - List<Long> selectIdsByOrganizationAndMatchingLogin(@Param("organizationUuid") String organizationUuid, @Param("login") String login, + List<String> selectIdsByOrganizationAndMatchingLogin(@Param("organizationUuid") String organizationUuid, @Param("login") String login, @Param("propertyKeys") List<String> propertyKeys); - void insertAsEmpty(@Param("key") String key, @Nullable @Param("userId") Integer userId, @Nullable @Param("componentUuid") String componentUuid, + void insertAsEmpty(@Param("uuid") String uuid, @Param("key") String key, @Nullable @Param("userId") Integer userId, @Nullable @Param("componentUuid") String componentUuid, @Param("now") long now); - void insertAsText(@Param("key") String key, @Nullable @Param("userId") Integer userId, @Nullable @Param("componentUuid") String componentUuid, + void insertAsText(@Param("uuid") String uuid, @Param("key") String key, @Nullable @Param("userId") Integer userId, @Nullable @Param("componentUuid") String componentUuid, @Param("value") String value, @Param("now") long now); - void insertAsClob(@Param("key") String key, @Nullable @Param("userId") Integer userId, @Nullable @Param("componentUuid") String componentUuid, + void insertAsClob(@Param("uuid") String uuid, @Param("key") String key, @Nullable @Param("userId") Integer userId, @Nullable @Param("componentUuid") String componentUuid, @Param("value") String value, @Param("now") long now); int delete(@Param("key") String key, @Nullable @Param("userId") Integer userId, @Nullable @Param("componentUuid") String componentUuid); @@ -76,7 +76,7 @@ public interface PropertiesMapper { int deleteByQuery(@Param("query") PropertyQuery query); - void deleteByIds(@Param("ids") List<Long> ids); + void deleteByUuids(@Param("uuids") List<String> uuids); void deleteByKeyAndValue(@Param("key") String key, @Param("value") String value); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertyDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertyDto.java index aa257f20997..2b75f616ae1 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertyDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/property/PropertyDto.java @@ -29,11 +29,21 @@ import static com.google.common.base.Preconditions.checkArgument; public class PropertyDto { private static final int MAX_KEY_LENGTH = 512; + private String uuid; private String key; private String value; private String componentUuid; private Integer userId; + String getUuid() { + return uuid; + } + + PropertyDto setUuid(String uuid) { + this.uuid = uuid; + return this; + } + public String getKey() { return key; } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/property/PropertiesMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/property/PropertiesMapper.xml index d7c96de6d65..828c63cb10a 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/property/PropertiesMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/property/PropertiesMapper.xml @@ -201,8 +201,8 @@ </where> </select> - <select id="selectIdsByOrganizationAndUser" parameterType="map" resultType="long"> - select py.id + <select id="selectUuidsByOrganizationAndUser" parameterType="map" resultType="String"> + select py.uuid from properties py inner join components ps on py.component_uuid = ps.uuid where @@ -210,8 +210,8 @@ and ps.organization_uuid=#{organizationUuid,jdbcType=VARCHAR} </select> - <select id="selectIdsByOrganizationAndMatchingLogin" parameterType="String" resultType="long"> - select py.id + <select id="selectIdsByOrganizationAndMatchingLogin" parameterType="String" resultType="String"> + select py.uuid from properties py inner join components ps on py.component_uuid = ps.uuid where @@ -236,6 +236,7 @@ <insert id="insertAsEmpty" parameterType="Map" useGeneratedKeys="false"> insert into properties ( + uuid, prop_key, component_uuid, user_id, @@ -243,6 +244,7 @@ created_at ) values ( + #{uuid}, #{key}, #{componentUuid}, #{userId,jdbcType=INTEGER}, @@ -254,6 +256,7 @@ <insert id="insertAsText" parameterType="Map" useGeneratedKeys="false"> insert into properties ( + uuid, prop_key, component_uuid, user_id, @@ -262,6 +265,7 @@ created_at ) values ( + #{uuid}, #{key}, #{componentUuid}, #{userId,jdbcType=INTEGER}, @@ -274,6 +278,7 @@ <insert id="insertAsClob" parameterType="Map" useGeneratedKeys="false"> insert into properties ( + uuid, prop_key, component_uuid, user_id, @@ -282,6 +287,7 @@ created_at ) values ( + #{uuid}, #{key}, #{componentUuid}, #{userId,jdbcType=INTEGER}, @@ -370,12 +376,12 @@ </where> </delete> - <delete id="deleteByIds" parameterType="long"> + <delete id="deleteByUuids" parameterType="String"> delete from properties where - id in - <foreach collection="ids" open="(" close=")" item="id" separator=","> - #{id} + uuid in + <foreach collection="uuids" open="(" close=")" item="uuid" separator=","> + #{uuid} </foreach> </delete> diff --git a/server/sonar-db-dao/src/schema/schema-sq.ddl b/server/sonar-db-dao/src/schema/schema-sq.ddl index 3df56d3fc64..9f21471b46c 100644 --- a/server/sonar-db-dao/src/schema/schema-sq.ddl +++ b/server/sonar-db-dao/src/schema/schema-sq.ddl @@ -725,16 +725,16 @@ CREATE UNIQUE INDEX "UNIQ_PROJECTS_KEE" ON "PROJECTS"("KEE"); CREATE INDEX "IDX_QUALIFIER" ON "PROJECTS"("QUALIFIER"); CREATE TABLE "PROPERTIES"( - "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1), "PROP_KEY" VARCHAR(512) NOT NULL, "USER_ID" BIGINT, "IS_EMPTY" BOOLEAN NOT NULL, "TEXT_VALUE" VARCHAR(4000), "CLOB_VALUE" CLOB(2147483647), "CREATED_AT" BIGINT NOT NULL, - "COMPONENT_UUID" VARCHAR(40) + "COMPONENT_UUID" VARCHAR(40), + "UUID" VARCHAR(40) NOT NULL ); -ALTER TABLE "PROPERTIES" ADD CONSTRAINT "PK_PROPERTIES" PRIMARY KEY("ID"); +ALTER TABLE "PROPERTIES" ADD CONSTRAINT "PK_PROPERTIES" PRIMARY KEY("UUID"); CREATE INDEX "PROPERTIES_KEY" ON "PROPERTIES"("PROP_KEY"); CREATE TABLE "QPROFILE_CHANGES"( diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/property/PropertiesDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/property/PropertiesDaoTest.java index 1ad6a2b0e63..12b4553e728 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/property/PropertiesDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/property/PropertiesDaoTest.java @@ -403,8 +403,8 @@ public class PropertiesDaoTest { @Test public void selectGlobalProperties() { // global - long id1 = insertProperty("global.one", "one", null, null); - long id2 = insertProperty("global.two", "two", null, null); + insertProperty("global.one", "one", null, null); + insertProperty("global.two", "two", null, null); List<PropertyDto> properties = underTest.selectGlobalProperties(); assertThat(properties.size()) @@ -501,7 +501,7 @@ public class PropertiesDaoTest { @DataProvider public static Object[][] allValuesForSelect() { - return new Object[][]{ + return new Object[][] { {null, ""}, {"", ""}, {"some value", "some value"}, @@ -599,9 +599,9 @@ public class PropertiesDaoTest { .extracting("key", "componentUuid").containsOnly(tuple(key, project.uuid())); assertThat(underTest.selectPropertiesByComponentUuids(session, newHashSet(project.uuid(), project2.uuid()))) .extracting("key", "componentUuid").containsOnly( - tuple(key, project.uuid()), - tuple(key, project2.uuid()), - tuple(anotherKey, project2.uuid())); + tuple(key, project.uuid()), + tuple(key, project2.uuid()), + tuple(anotherKey, project2.uuid())); assertThat(underTest.selectPropertiesByComponentUuids(session, newHashSet("uuid123456789"))).isEmpty(); } @@ -625,13 +625,13 @@ public class PropertiesDaoTest { .extracting("key", "componentUuid").containsOnly(tuple(key, project.uuid())); assertThat(underTest.selectPropertiesByKeysAndComponentUuids(session, newHashSet(key), newHashSet(project.uuid(), project2.uuid()))) .extracting("key", "componentUuid").containsOnly( - tuple(key, project.uuid()), - tuple(key, project2.uuid())); + tuple(key, project.uuid()), + tuple(key, project2.uuid())); assertThat(underTest.selectPropertiesByKeysAndComponentUuids(session, newHashSet(key, anotherKey), newHashSet(project.uuid(), project2.uuid()))) .extracting("key", "componentUuid").containsOnly( - tuple(key, project.uuid()), - tuple(key, project2.uuid()), - tuple(anotherKey, project2.uuid())); + tuple(key, project.uuid()), + tuple(key, project2.uuid()), + tuple(anotherKey, project2.uuid())); assertThat(underTest.selectPropertiesByKeysAndComponentUuids(session, newHashSet("unknown"), newHashSet(project.uuid()))).isEmpty(); assertThat(underTest.selectPropertiesByKeysAndComponentUuids(session, newHashSet("key"), newHashSet("uuid123456789"))).isEmpty(); @@ -788,11 +788,11 @@ public class PropertiesDaoTest { @Test @UseDataProvider("valueUpdatesDataProvider") public void saveProperty_deletes_then_inserts_global_properties_when_they_exist_in_db(@Nullable String oldValue, @Nullable String newValue) { - long id = insertProperty("global", oldValue, null, null); + String uuid = insertProperty("global", oldValue, null, null); underTest.saveProperty(new PropertyDto().setKey("global").setValue(newValue)); - assertThatPropertiesRow(id) + assertThatPropertiesRowByUuid(uuid) .doesNotExist(); PropertiesRowAssert propertiesRowAssert = assertThatPropertiesRow("global") @@ -812,11 +812,11 @@ public class PropertiesDaoTest { @UseDataProvider("valueUpdatesDataProvider") public void saveProperty_deletes_then_inserts_component_properties_when_they_exist_in_db(@Nullable String oldValue, @Nullable String newValue) { String componentUuid = "uuid999"; - long id = insertProperty("global", oldValue, componentUuid, null); + String uuid = insertProperty("global", oldValue, componentUuid, null); underTest.saveProperty(new PropertyDto().setKey("global").setComponentUuid(componentUuid).setValue(newValue)); - assertThatPropertiesRow(id) + assertThatPropertiesRowByUuid(uuid) .doesNotExist(); PropertiesRowAssert propertiesRowAssert = assertThatPropertiesRow("global") .hasComponentUuid(componentUuid) @@ -835,11 +835,11 @@ public class PropertiesDaoTest { @UseDataProvider("valueUpdatesDataProvider") public void saveProperty_deletes_then_inserts_user_properties_when_they_exist_in_db(@Nullable String oldValue, @Nullable String newValue) { int userId = 90; - long id = insertProperty("global", oldValue, null, userId); + String uuid = insertProperty("global", oldValue, null, userId); underTest.saveProperty(new PropertyDto().setKey("global").setUserId(userId).setValue(newValue)); - assertThatPropertiesRow(id) + assertThatPropertiesRowByUuid(uuid) .doesNotExist(); PropertiesRowAssert propertiesRowAssert = assertThatPropertiesRow("global") @@ -857,7 +857,7 @@ public class PropertiesDaoTest { @DataProvider public static Object[][] valueUpdatesDataProvider() { - return new Object[][]{ + return new Object[][] { {null, null}, {null, ""}, {null, "some value"}, @@ -894,44 +894,44 @@ public class PropertiesDaoTest { insertPrivateProject("A"); insertPrivateProject("B"); insertPrivateProject("C"); - long id1 = insertProperty("global.one", "one", null, null); - long id2 = insertProperty("global.two", "two", null, null); - long id3 = insertProperty("struts.one", "one", "project1", null); - long id4 = insertProperty("commonslang.one", "one", "project2", null); - long id5 = insertProperty("user.one", "one", null, 100); - long id6 = insertProperty("user.two", "two", null, 100); - long id7 = insertProperty("other.one", "one", "project3", null); + String uuid1 = insertProperty("global.one", "one", null, null); + String uuid2 = insertProperty("global.two", "two", null, null); + String uuid3 = insertProperty("struts.one", "one", "project1", null); + String uuid4 = insertProperty("commonslang.one", "one", "project2", null); + String uuid5 = insertProperty("user.one", "one", null, 100); + String uuid6 = insertProperty("user.two", "two", null, 100); + String uuid7 = insertProperty("other.one", "one", "project3", null); underTest.deleteProjectProperty("struts.one", "project1"); - assertThatPropertiesRow(id1) + assertThatPropertiesRowByUuid(uuid1) .hasKey("global.one") .hasNoComponentUuid() .hasNoUserId() .hasTextValue("one"); - assertThatPropertiesRow(id2) + assertThatPropertiesRowByUuid(uuid2) .hasKey("global.two") .hasNoComponentUuid() .hasNoUserId() .hasTextValue("two"); - assertThatPropertiesRow(id3) + assertThatPropertiesRowByUuid(uuid3) .doesNotExist(); - assertThatPropertiesRow(id4) + assertThatPropertiesRowByUuid(uuid4) .hasKey("commonslang.one") .hasComponentUuid("project2") .hasNoUserId() .hasTextValue("one"); - assertThatPropertiesRow(id5) + assertThatPropertiesRowByUuid(uuid5) .hasKey("user.one") .hasNoComponentUuid() .hasUserId(100) .hasTextValue("one"); - assertThatPropertiesRow(id6) + assertThatPropertiesRowByUuid(uuid6) .hasKey("user.two") .hasNoComponentUuid() .hasUserId(100) .hasTextValue("two"); - assertThatPropertiesRow(id7) + assertThatPropertiesRowByUuid(uuid7) .hasKey("other.one") .hasComponentUuid("project3") .hasNoUserId() @@ -940,37 +940,37 @@ public class PropertiesDaoTest { @Test public void delete_project_properties() { - long id1 = insertProperty("sonar.profile.java", "Sonar Way", "uuid1", null); - long id2 = insertProperty("sonar.profile.java", "Sonar Way", "uuid2", null); + String uuid1 = insertProperty("sonar.profile.java", "Sonar Way", "uuid1", null); + String uuid2 = insertProperty("sonar.profile.java", "Sonar Way", "uuid2", null); - long id3 = insertProperty("sonar.profile.java", "Sonar Way", null, null); + String uuid3 = insertProperty("sonar.profile.java", "Sonar Way", null, null); - long id4 = insertProperty("sonar.profile.js", "Sonar Way", "uuid1", null); - long id5 = insertProperty("sonar.profile.js", "Sonar Way", "uuid2", null); - long id6 = insertProperty("sonar.profile.js", "Sonar Way", null, null); + String uuid4 = insertProperty("sonar.profile.js", "Sonar Way", "uuid1", null); + String uuid5 = insertProperty("sonar.profile.js", "Sonar Way", "uuid2", null); + String uuid6 = insertProperty("sonar.profile.js", "Sonar Way", null, null); underTest.deleteProjectProperties("sonar.profile.java", "Sonar Way"); - assertThatPropertiesRow(id1) + assertThatPropertiesRowByUuid(uuid1) .doesNotExist(); - assertThatPropertiesRow(id2) + assertThatPropertiesRowByUuid(uuid2) .doesNotExist(); - assertThatPropertiesRow(id3) + assertThatPropertiesRowByUuid(uuid3) .hasKey("sonar.profile.java") .hasNoComponentUuid() .hasNoUserId() .hasTextValue("Sonar Way"); - assertThatPropertiesRow(id4) + assertThatPropertiesRowByUuid(uuid4) .hasKey("sonar.profile.js") .hasComponentUuid("uuid1") .hasNoUserId() .hasTextValue("Sonar Way"); - assertThatPropertiesRow(id5) + assertThatPropertiesRowByUuid(uuid5) .hasKey("sonar.profile.js") .hasComponentUuid("uuid2") .hasNoUserId() .hasTextValue("Sonar Way"); - assertThatPropertiesRow(id6) + assertThatPropertiesRowByUuid(uuid6) .hasKey("sonar.profile.js") .hasNoComponentUuid() .hasNoUserId() @@ -980,30 +980,30 @@ public class PropertiesDaoTest { @Test public void deleteGlobalProperty() { // global - long id1 = insertProperty("global.key", "new_global", null, null); - long id2 = insertProperty("to_be_deleted", "xxx", null, null); + String uuid1 = insertProperty("global.key", "new_global", null, null); + String uuid2 = insertProperty("to_be_deleted", "xxx", null, null); // project - do not delete this project property that has the same key - long id3 = insertProperty("to_be_deleted", "new_project", "to_be_deleted", null); + String uuid3 = insertProperty("to_be_deleted", "new_project", "to_be_deleted", null); // user - long id4 = insertProperty("user.key", "new_user", null, 100); + String uuid4 = insertProperty("user.key", "new_user", null, 100); underTest.deleteGlobalProperty("to_be_deleted"); - assertThatPropertiesRow(id1) + assertThatPropertiesRowByUuid(uuid1) .hasKey("global.key") .hasNoUserId() .hasNoComponentUuid() .hasTextValue("new_global"); - assertThatPropertiesRow(id2) + assertThatPropertiesRowByUuid(uuid2) .doesNotExist(); assertThatPropertiesRow("to_be_deleted", null, null) .doesNotExist(); - assertThatPropertiesRow(id3) + assertThatPropertiesRowByUuid(uuid3) .hasKey("to_be_deleted") .hasComponentUuid("to_be_deleted") .hasNoUserId() .hasTextValue("new_project"); - assertThatPropertiesRow(id4) + assertThatPropertiesRowByUuid(uuid4) .hasKey("user.key") .hasNoComponentUuid() .hasUserId(100) @@ -1115,11 +1115,11 @@ public class PropertiesDaoTest { @Test public void saveGlobalProperties_delete_and_insert_new_value_when_property_exists_in_db() { - long id = insertProperty("to_be_updated", "old_value", null, null); + String uuid = insertProperty("to_be_updated", "old_value", null, null); underTest.saveGlobalProperties(ImmutableMap.of("to_be_updated", "new value")); - assertThatPropertiesRow(id) + assertThatPropertiesRowByUuid(uuid) .doesNotExist(); assertThatPropertiesRow("to_be_updated") @@ -1141,46 +1141,46 @@ public class PropertiesDaoTest { @Test public void renamePropertyKey_updates_global_component_and_user_properties() { - long id1 = insertProperty("foo", "bar", null, null); - long id2 = insertProperty("old_name", "doc1", null, null); - long id3 = insertProperty("old_name", "doc2", "15", null); - long id4 = insertProperty("old_name", "doc3", "16", null); - long id5 = insertProperty("old_name", "doc4", null, 100); - long id6 = insertProperty("old_name", "doc5", null, 101); + String uuid1 = insertProperty("foo", "bar", null, null); + String uuid2 = insertProperty("old_name", "doc1", null, null); + String uuid3 = insertProperty("old_name", "doc2", "15", null); + String uuid4 = insertProperty("old_name", "doc3", "16", null); + String uuid5 = insertProperty("old_name", "doc4", null, 100); + String uuid6 = insertProperty("old_name", "doc5", null, 101); underTest.renamePropertyKey("old_name", "new_name"); - assertThatPropertiesRow(id1) + assertThatPropertiesRowByUuid(uuid1) .hasKey("foo") .hasNoUserId() .hasNoComponentUuid() .hasTextValue("bar") .hasCreatedAt(INITIAL_DATE + 2); - assertThatPropertiesRow(id2) + assertThatPropertiesRowByUuid(uuid2) .hasKey("new_name") .hasNoComponentUuid() .hasNoUserId() .hasTextValue("doc1") .hasCreatedAt(INITIAL_DATE + 3); - assertThatPropertiesRow(id3) + assertThatPropertiesRowByUuid(uuid3) .hasKey("new_name") .hasComponentUuid("15") .hasNoUserId() .hasTextValue("doc2") .hasCreatedAt(INITIAL_DATE + 4); - assertThatPropertiesRow(id4) + assertThatPropertiesRowByUuid(uuid4) .hasKey("new_name") .hasComponentUuid("16") .hasNoUserId() .hasTextValue("doc3") .hasCreatedAt(INITIAL_DATE + 5); - assertThatPropertiesRow(id5) + assertThatPropertiesRowByUuid(uuid5) .hasKey("new_name") .hasNoComponentUuid() .hasUserId(100) .hasTextValue("doc4") .hasCreatedAt(INITIAL_DATE + 6); - assertThatPropertiesRow(id6) + assertThatPropertiesRowByUuid(uuid6) .hasKey("new_name") .hasNoComponentUuid() .hasUserId(101) @@ -1190,14 +1190,14 @@ public class PropertiesDaoTest { @Test public void rename_to_same_key_has_no_effect() { - long id = insertProperty("foo", "bar", null, null); + String uuid = insertProperty("foo", "bar", null, null); - assertThatPropertiesRow(id) + assertThatPropertiesRowByUuid(uuid) .hasCreatedAt(INITIAL_DATE + 2); underTest.renamePropertyKey("foo", "foo"); - assertThatPropertiesRow(id) + assertThatPropertiesRowByUuid(uuid) .hasKey("foo") .hasNoUserId() .hasNoComponentUuid() @@ -1233,17 +1233,17 @@ public class PropertiesDaoTest { session.commit(); } - private long insertProperty(String key, @Nullable String value, @Nullable String componentUuid, @Nullable Integer userId) { + private String insertProperty(String key, @Nullable String value, @Nullable String componentUuid, @Nullable Integer userId) { PropertyDto dto = new PropertyDto().setKey(key) .setComponentUuid(componentUuid) .setUserId(userId) .setValue(value); db.properties().insertProperty(dto); - return (long) db.selectFirst(session, "select id as \"id\" from properties" + + return (String) db.selectFirst(session, "select uuid as \"uuid\" from properties" + " where prop_key='" + key + "'" + " and user_id" + (userId == null ? " is null" : "='" + userId + "'") + - " and component_uuid" + (componentUuid == null ? " is null" : "='" + componentUuid + "'")).get("id"); + " and component_uuid" + (componentUuid == null ? " is null" : "='" + componentUuid + "'")).get("uuid"); } private ComponentDto insertPrivateProject(String projectKey) { @@ -1274,8 +1274,8 @@ public class PropertiesDaoTest { return new PropertiesRowAssert(db, key); } - private PropertiesRowAssert assertThatPropertiesRow(long id) { - return new PropertiesRowAssert(db, id); + private PropertiesRowAssert assertThatPropertiesRowByUuid(String uuid) { + return PropertiesRowAssert.byUuid(db, uuid); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/property/PropertiesRowAssert.java b/server/sonar-db-dao/src/test/java/org/sonar/db/property/PropertiesRowAssert.java index c723b98978a..2bdd491c2f8 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/property/PropertiesRowAssert.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/property/PropertiesRowAssert.java @@ -49,8 +49,12 @@ final class PropertiesRowAssert extends AbstractAssert<PropertiesRowAssert, Prop super(asInternalProperty(dbTester, () -> " where prop_key='" + key + "'"), PropertiesRowAssert.class); } - PropertiesRowAssert(DbTester dbTester, long id) { - super(asInternalProperty(dbTester, () -> " where id=" + id), PropertiesRowAssert.class); + private PropertiesRowAssert(PropertiesRow propertiesRow) { + super(propertiesRow, PropertiesRowAssert.class); + } + + public static PropertiesRowAssert byUuid(DbTester dbTester, String uuid){ + return new PropertiesRowAssert(asInternalProperty(dbTester, () -> " where uuid='" + uuid + "'")); } @CheckForNull diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/property/PropertyDbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/property/PropertyDbTester.java index 7bc8e9d39e1..e440f1a9556 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/property/PropertyDbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/property/PropertyDbTester.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; import javax.annotation.Nullable; +import org.sonar.core.util.UuidFactory; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; @@ -105,9 +106,4 @@ public class PropertyDbTester { return this; } - public PropertyDbTester insertEmptyInternal(String key) { - dbClient.internalPropertiesDao().saveAsEmpty(dbSession, key); - dbSession.commit(); - return this; - } } |