diff options
45 files changed, 1239 insertions, 44 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java index 68e156594d0..bea2f1a725a 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java @@ -153,7 +153,7 @@ public class ActiveRuleDao implements Dao { public ActiveRuleParamDto insertParam(DbSession dbSession, ActiveRuleDto activeRule, ActiveRuleParamDto activeRuleParam) { checkArgument(activeRule.getUuid() != null, ACTIVE_RULE_IS_NOT_PERSISTED); checkArgument(activeRuleParam.getUuid() == null, ACTIVE_RULE_PARAM_IS_ALREADY_PERSISTED); - Preconditions.checkNotNull(activeRuleParam.getRulesParameterId(), RULE_PARAM_IS_NOT_PERSISTED); + Preconditions.checkNotNull(activeRuleParam.getRulesParameterUuid(), RULE_PARAM_IS_NOT_PERSISTED); activeRuleParam.setActiveRuleUuid(activeRule.getUuid()); activeRuleParam.setUuid(uuidFactory.create()); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleParamDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleParamDto.java index c42c89923a4..ebfcda04ff8 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleParamDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleParamDto.java @@ -31,7 +31,7 @@ public class ActiveRuleParamDto { private String uuid; private String activeRuleUuid; - private Integer rulesParameterId; + private String rulesParameterUuid; private String kee; private String value; @@ -53,13 +53,13 @@ public class ActiveRuleParamDto { return this; } - public Integer getRulesParameterId() { - return rulesParameterId; + public String getRulesParameterUuid() { + return rulesParameterUuid; } // TODO set private or drop - public ActiveRuleParamDto setRulesParameterId(Integer rulesParameterId) { - this.rulesParameterId = rulesParameterId; + public ActiveRuleParamDto setRulesParameterUuid(String rulesParameterUuid) { + this.rulesParameterUuid = rulesParameterUuid; return this; } @@ -87,10 +87,10 @@ public class ActiveRuleParamDto { } public static ActiveRuleParamDto createFor(RuleParamDto param) { - Preconditions.checkArgument(param.getId() != null, "Parameter is not persisted"); + Preconditions.checkArgument(param.getUuid() != null, "Parameter is not persisted"); return new ActiveRuleParamDto() .setKey(param.getName()) - .setRulesParameterId(param.getId()); + .setRulesParameterUuid(param.getUuid()); } public static Map<String, ActiveRuleParamDto> groupByKey(Collection<ActiveRuleParamDto> params) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java index 2139c8670a7..7fa6d8a3554 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java @@ -28,6 +28,7 @@ import javax.annotation.Nullable; import org.apache.ibatis.session.ResultHandler; import org.sonar.api.rule.RuleKey; import org.sonar.api.rules.RuleQuery; +import org.sonar.core.util.UuidFactory; import org.sonar.db.Dao; import org.sonar.db.DbSession; import org.sonar.db.RowNotFoundException; @@ -43,6 +44,12 @@ import static org.sonar.db.DatabaseUtils.executeLargeUpdates; public class RuleDao implements Dao { + private final UuidFactory uuidFactory; + + public RuleDao(UuidFactory uuidFactory) { + this.uuidFactory = uuidFactory; + } + public Optional<RuleDto> selectByKey(DbSession session, String organizationUuid, RuleKey key) { RuleDto res = mapper(session).selectByKey(organizationUuid, key); ensureOrganizationIsSet(organizationUuid, res); @@ -235,19 +242,21 @@ public class RuleDao implements Dao { public void insertRuleParam(DbSession session, RuleDefinitionDto rule, RuleParamDto param) { checkNotNull(rule.getId(), "Rule id must be set"); param.setRuleId(rule.getId()); + + param.setUuid(uuidFactory.create()); mapper(session).insertParameter(param); } public RuleParamDto updateRuleParam(DbSession session, RuleDefinitionDto rule, RuleParamDto param) { checkNotNull(rule.getId(), "Rule id must be set"); - checkNotNull(param.getId(), "Rule parameter is not yet persisted must be set"); + checkNotNull(param.getUuid(), "Rule parameter is not yet persisted must be set"); param.setRuleId(rule.getId()); mapper(session).updateParameter(param); return param; } - public void deleteRuleParam(DbSession session, int ruleParameterId) { - mapper(session).deleteParameter(ruleParameterId); + public void deleteRuleParam(DbSession session, String ruleParameterUuid) { + mapper(session).deleteParameter(ruleParameterUuid); } public Set<DeprecatedRuleKeyDto> selectAllDeprecatedRuleKeys(DbSession session) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMapper.java index 308a422e4a7..8c26caa5e10 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMapper.java @@ -23,9 +23,9 @@ import java.util.List; import java.util.Set; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.session.ResultHandler; -import org.sonar.db.es.RuleExtensionId; import org.sonar.api.rule.RuleKey; import org.sonar.api.rules.RuleQuery; +import org.sonar.db.es.RuleExtensionId; public interface RuleMapper { @@ -85,7 +85,7 @@ public interface RuleMapper { void updateParameter(RuleParamDto param); - void deleteParameter(Integer paramId); + void deleteParameter(String paramUuid); Set<DeprecatedRuleKeyDto> selectAllDeprecatedRuleKeys(); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleParamDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleParamDto.java index 7d35788103e..d30ad25377e 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleParamDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleParamDto.java @@ -28,19 +28,19 @@ import static com.google.common.base.Preconditions.checkArgument; public class RuleParamDto { - private Integer id; + private String uuid; private Integer ruleId; private String name; private String type; private String defaultValue; private String description; - public Integer getId() { - return id; + public String getUuid() { + return uuid; } - public RuleParamDto setId(Integer id) { - this.id = id; + public RuleParamDto setUuid(String uuid) { + this.uuid = uuid; return this; } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml index 7649914ce45..26e013c0bfb 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml @@ -198,7 +198,7 @@ <sql id="activeRuleParamColumns"> p.uuid, p.active_rule_uuid as activeRuleUuid, - p.rules_parameter_id as rulesParameterId, + p.rules_parameter_uuid as rulesParameterUuid, p.rules_parameter_key as kee, p.value as value </sql> @@ -207,13 +207,13 @@ insert into active_rule_parameters ( uuid, active_rule_uuid, - rules_parameter_id, + rules_parameter_uuid, rules_parameter_key, value ) values ( #{uuid, jdbcType=VARCHAR}, #{activeRuleUuid, jdbcType=VARCHAR}, - #{rulesParameterId, jdbcType=BIGINT}, + #{rulesParameterUuid, jdbcType=BIGINT}, #{key, jdbcType=VARCHAR}, #{value, jdbcType=VARCHAR} ) diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleMapper.xml index 1c976aacead..b55391d4cbc 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleMapper.xml @@ -461,15 +461,15 @@ and organization_uuid=#{organizationUuid,jdbcType=VARCHAR} </update> - <delete id="deleteParams" parameterType="Integer"> + <delete id="deleteParams" parameterType="String"> delete from active_rule_parameters where - rules_parameter_id=#{id,jdbcType=INTEGER} + rules_parameter_uuid=#{uuid,jdbcType=VARCHAR} </delete> <sql id="paramColumns"> - p.id as "id", + p.uuid as "uuid", p.rule_id as "ruleId", p.name as "name", p.param_type as "type", @@ -512,15 +512,16 @@ </foreach> </select> - <delete id="deleteParameter" parameterType="Integer"> + <delete id="deleteParameter" parameterType="String"> delete from rules_parameters where - id=#{id,jdbcType=INTEGER} + uuid=#{uuid,jdbcType=INTEGER} </delete> - <insert id="insertParameter" parameterType="RuleParam" keyColumn="id" useGeneratedKeys="true" keyProperty="id"> + <insert id="insertParameter" parameterType="RuleParam" useGeneratedKeys="false"> insert into rules_parameters ( + uuid, rule_id, name, param_type, @@ -528,6 +529,7 @@ description ) values ( + #{uuid,jdbcType=VARCHAR}, #{ruleId,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{type,jdbcType=VARCHAR}, @@ -542,7 +544,7 @@ default_value=#{defaultValue,jdbcType=VARCHAR}, description=#{description,jdbcType=VARCHAR} where - id=#{id,jdbcType=INTEGER} + uuid=#{uuid,jdbcType=VARCHAR} </update> <select id="selectAllDeprecatedRuleKeys" resultType="org.sonar.db.rule.DeprecatedRuleKeyDto"> diff --git a/server/sonar-db-dao/src/schema/schema-sq.ddl b/server/sonar-db-dao/src/schema/schema-sq.ddl index 98df38e2973..bd57c4aec5d 100644 --- a/server/sonar-db-dao/src/schema/schema-sq.ddl +++ b/server/sonar-db-dao/src/schema/schema-sq.ddl @@ -14,11 +14,11 @@ CREATE TABLE "SCHEMA_MIGRATIONS"( ); CREATE TABLE "ACTIVE_RULE_PARAMETERS"( - "RULES_PARAMETER_ID" INTEGER NOT NULL, "VALUE" VARCHAR(4000), "RULES_PARAMETER_KEY" VARCHAR(128), "UUID" VARCHAR(40) NOT NULL, - "ACTIVE_RULE_UUID" VARCHAR(40) NOT NULL + "ACTIVE_RULE_UUID" VARCHAR(40) NOT NULL, + "RULES_PARAMETER_UUID" VARCHAR(40) NOT NULL ); ALTER TABLE "ACTIVE_RULE_PARAMETERS" ADD CONSTRAINT "PK_ACTIVE_RULE_PARAMETERS" PRIMARY KEY("UUID"); CREATE INDEX "ARP_ACTIVE_RULE_UUID" ON "ACTIVE_RULE_PARAMETERS"("ACTIVE_RULE_UUID"); @@ -852,14 +852,14 @@ CREATE TABLE "RULES_METADATA"( ALTER TABLE "RULES_METADATA" ADD CONSTRAINT "PK_RULES_METADATA" PRIMARY KEY("RULE_ID", "ORGANIZATION_UUID"); CREATE TABLE "RULES_PARAMETERS"( - "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1), "RULE_ID" INTEGER NOT NULL, "NAME" VARCHAR(128) NOT NULL, "DESCRIPTION" VARCHAR(4000), "PARAM_TYPE" VARCHAR(512) NOT NULL, - "DEFAULT_VALUE" VARCHAR(4000) + "DEFAULT_VALUE" VARCHAR(4000), + "UUID" VARCHAR(40) NOT NULL ); -ALTER TABLE "RULES_PARAMETERS" ADD CONSTRAINT "PK_RULES_PARAMETERS" PRIMARY KEY("ID"); +ALTER TABLE "RULES_PARAMETERS" ADD CONSTRAINT "PK_RULES_PARAMETERS" PRIMARY KEY("UUID"); CREATE INDEX "RULES_PARAMETERS_RULE_ID" ON "RULES_PARAMETERS"("RULE_ID"); CREATE UNIQUE INDEX "RULES_PARAMETERS_UNIQUE" ON "RULES_PARAMETERS"("RULE_ID", "NAME"); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java index 418fc1b8efb..738d46df976 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java @@ -465,7 +465,7 @@ public class ActiveRuleDaoTest { .matches(p -> Objects.equals(p.getUuid(), activeRuleParam.getUuid())) .matches(p -> p.getKey().equals(activeRuleParam.getKey())) .matches(p -> p.getActiveRuleUuid().equals(activeRule.getUuid())) - .matches(p -> p.getRulesParameterId().equals(rule1Param1.getId())) + .matches(p -> p.getRulesParameterUuid().equals(rule1Param1.getUuid())) .matches(p -> p.getValue().equals("foo")); } @@ -496,7 +496,7 @@ public class ActiveRuleDaoTest { underTest.insertParam(dbSession, createFor(profile1, rule1).setUuid("uuid"), - ActiveRuleParamDto.createFor(rule1Param1).setValue("activeValue1").setRulesParameterId(null)); + ActiveRuleParamDto.createFor(rule1Param1).setValue("activeValue1").setRulesParameterUuid(null)); } @Test @@ -514,7 +514,7 @@ public class ActiveRuleDaoTest { .matches(p -> Objects.equals(p.getUuid(), activeRuleParam.getUuid())) .matches(p -> p.getKey().equals(activeRuleParam.getKey())) .matches(p -> p.getActiveRuleUuid().equals(activeRule.getUuid())) - .matches(p -> p.getRulesParameterId().equals(rule1Param1.getId())) + .matches(p -> p.getRulesParameterUuid().equals(rule1Param1.getUuid())) .matches(p -> p.getValue().equals("bar")); } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java index 5fc68e9ab2d..e93569a7d1e 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java @@ -831,7 +831,7 @@ public class RuleDaoTest { assertThat(ruleDtos.size()).isEqualTo(1); RuleParamDto ruleDto = ruleDtos.get(0); - assertThat(ruleDto.getId()).isEqualTo(ruleParam.getId()); + assertThat(ruleDto.getUuid()).isEqualTo(ruleParam.getUuid()); assertThat(ruleDto.getName()).isEqualTo(ruleParam.getName()); assertThat(ruleDto.getDescription()).isEqualTo(ruleParam.getDescription()); assertThat(ruleDto.getType()).isEqualTo(ruleParam.getType()); @@ -899,7 +899,7 @@ public class RuleDaoTest { List<RuleParamDto> params = underTest.selectRuleParamsByRuleKey(db.getSession(), rule.getKey()); assertThat(params).hasSize(1); RuleParamDto param = new RuleParamDto() - .setId(ruleParam.getId()) + .setUuid(ruleParam.getUuid()) .setRuleId(rule.getId()) // Name will not be updated .setName("format") @@ -920,7 +920,7 @@ public class RuleDaoTest { RuleParamDto ruleParam = db.rules().insertRuleParam(rule); assertThat(underTest.selectRuleParamsByRuleKey(db.getSession(), rule.getKey())).hasSize(1); - underTest.deleteRuleParam(db.getSession(), ruleParam.getId()); + underTest.deleteRuleParam(db.getSession(), ruleParam.getUuid()); assertThat(underTest.selectRuleParamsByRuleKey(db.getSession(), rule.getKey())).isEmpty(); } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/DbVersion83.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/DbVersion83.java index 4e028bead2d..d2e45c4e5be 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/DbVersion83.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/DbVersion83.java @@ -115,6 +115,16 @@ import org.sonar.server.platform.db.migration.version.v83.projectmeasures.DropId import org.sonar.server.platform.db.migration.version.v83.projectmeasures.DropPrimaryKeyOnIdColumnOfProjectMeasuresTable; import org.sonar.server.platform.db.migration.version.v83.projectmeasures.MakeProjectMeasuresUuidColumnNotNullable; import org.sonar.server.platform.db.migration.version.v83.projectmeasures.PopulateProjectMeasureUuid; +import org.sonar.server.platform.db.migration.version.v83.rulesparameters.AddPrimaryKeyOnUuidColumnOfRulesParametersTable; +import org.sonar.server.platform.db.migration.version.v83.rulesparameters.AddUuidColumnToRulesParameters; +import org.sonar.server.platform.db.migration.version.v83.rulesparameters.DropIdColumnOfRulesParametersTable; +import org.sonar.server.platform.db.migration.version.v83.rulesparameters.DropPrimaryKeyOnIdColumnOfRulesParametersTable; +import org.sonar.server.platform.db.migration.version.v83.rulesparameters.MakeRulesParametersUuidColumnNotNullable; +import org.sonar.server.platform.db.migration.version.v83.rulesparameters.PopulateRulesParametersUuid; +import org.sonar.server.platform.db.migration.version.v83.rulesparameters.fk.AddRulesParameterUuidColumnToActiveRuleParameters; +import org.sonar.server.platform.db.migration.version.v83.rulesparameters.fk.DropRulesParameterIdColumnOfActiveRuleParametersTable; +import org.sonar.server.platform.db.migration.version.v83.rulesparameters.fk.MakeActiveRuleParametersRulesParameterUuidColumnNotNullable; +import org.sonar.server.platform.db.migration.version.v83.rulesparameters.fk.PopulateActiveRuleParametersRulesParameterUuid; import org.sonar.server.platform.db.migration.version.v83.projectqprofiles.AddPrimaryKeyOnUuidColumnOfProjectQProfilesTable; import org.sonar.server.platform.db.migration.version.v83.projectqprofiles.AddUuidColumnToProjectQProfilesTable; import org.sonar.server.platform.db.migration.version.v83.projectqprofiles.DropIdColumnOfProjectQProfilesTable; @@ -330,6 +340,22 @@ public class DbVersion83 implements DbVersion { .add(3516, "Drop column 'ID' of 'ACTIVE_RULES' table", DropIdColumnOfActiveRulesTable.class) .add(3517, "Drop column 'active_rule_id' of 'ACTIVE_RULE_PARAMETERS' table", DropActiveRuleIdColumnOfActiveRuleParametersTable.class) + // Migration on RULES_PARAMETERS table - populate uuid column + .add(3518, "Add 'uuid' column for 'RULES_PARAMETERS'", AddUuidColumnToRulesParameters.class) + .add(3519, "Populate 'uuid' column for 'RULES_PARAMETERS'", PopulateRulesParametersUuid.class) + .add(3520, "Make 'uuid' column not nullable for 'RULES_PARAMETERS'", MakeRulesParametersUuidColumnNotNullable.class) + + // Migration of ACTIVE_RULE_PARAMS FK to RULES_PARAMETERS, switch from ruleParamId to ruleParamUuid + .add(3521, "Add 'rules_parameter_uuid' column for 'ACTIVE_RULE_PARAMS' table", AddRulesParameterUuidColumnToActiveRuleParameters.class) + .add(3522, "Populate 'rules_parameter_uuid' column for 'ACTIVE_RULE_PARAMS' table", PopulateActiveRuleParametersRulesParameterUuid.class) + .add(3523, "Make 'rules_parameter_uuid' column not nullable for 'ACTIVE_RULE_PARAMS' table", MakeActiveRuleParametersRulesParameterUuidColumnNotNullable.class) + .add(3524, "Drop column 'rules_parameter_id' of 'ACTIVE_RULE_PARAMS' table", DropRulesParameterIdColumnOfActiveRuleParametersTable.class) + + // Migration on RULES_PARAMETERS table change PK + .add(3525, "Drop primary key on 'ID' column of 'RULES_PARAMETERS' table", DropPrimaryKeyOnIdColumnOfRulesParametersTable.class) + .add(3526, "Add primary key on 'UUID' column of 'RULES_PARAMETERS' table", AddPrimaryKeyOnUuidColumnOfRulesParametersTable.class) + .add(3527, "Drop column 'ID' of 'RULES_PARAMETERS' table", DropIdColumnOfRulesParametersTable.class) + ; } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/AddPrimaryKeyOnUuidColumnOfRulesParametersTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/AddPrimaryKeyOnUuidColumnOfRulesParametersTable.java new file mode 100644 index 00000000000..2b32201ac4d --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/AddPrimaryKeyOnUuidColumnOfRulesParametersTable.java @@ -0,0 +1,38 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.rulesparameters; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.DdlChange; +import org.sonar.server.platform.db.migration.version.v83.util.AddPrimaryKeyBuilder; + +public class AddPrimaryKeyOnUuidColumnOfRulesParametersTable extends DdlChange { + + public AddPrimaryKeyOnUuidColumnOfRulesParametersTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AddPrimaryKeyBuilder("rules_parameters", "uuid").build()); + } + +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/AddUuidColumnToRulesParameters.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/AddUuidColumnToRulesParameters.java new file mode 100644 index 00000000000..7bc9377cac6 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/AddUuidColumnToRulesParameters.java @@ -0,0 +1,31 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.rulesparameters; + +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.version.v83.common.AddUuidColumnToTable; + +public class AddUuidColumnToRulesParameters extends AddUuidColumnToTable { + private static final String TABLE_NAME = "rules_parameters"; + + public AddUuidColumnToRulesParameters(Database db) { + super(db, TABLE_NAME); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/DropIdColumnOfRulesParametersTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/DropIdColumnOfRulesParametersTable.java new file mode 100644 index 00000000000..3a169211fad --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/DropIdColumnOfRulesParametersTable.java @@ -0,0 +1,31 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.rulesparameters; + +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.version.v83.common.DropIdColumn; + +public class DropIdColumnOfRulesParametersTable extends DropIdColumn { + private static final String TABLE_NAME = "rules_parameters"; + + public DropIdColumnOfRulesParametersTable(Database db) { + super(db, TABLE_NAME); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/DropPrimaryKeyOnIdColumnOfRulesParametersTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/DropPrimaryKeyOnIdColumnOfRulesParametersTable.java new file mode 100644 index 00000000000..fde1307624a --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/DropPrimaryKeyOnIdColumnOfRulesParametersTable.java @@ -0,0 +1,32 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.rulesparameters; + +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.version.v83.common.DropPrimaryKeyOnIdColumn; +import org.sonar.server.platform.db.migration.version.v83.util.DropPrimaryKeySqlGenerator; + +public class DropPrimaryKeyOnIdColumnOfRulesParametersTable extends DropPrimaryKeyOnIdColumn { + private static final String TABLE_NAME = "rules_parameters"; + + public DropPrimaryKeyOnIdColumnOfRulesParametersTable(Database db, DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator) { + super(db, dropPrimaryKeySqlGenerator, TABLE_NAME); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/MakeRulesParametersUuidColumnNotNullable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/MakeRulesParametersUuidColumnNotNullable.java new file mode 100644 index 00000000000..86b023e8446 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/MakeRulesParametersUuidColumnNotNullable.java @@ -0,0 +1,31 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.rulesparameters; + +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.version.v83.common.MakeUuidColumnNotNullable; + +public class MakeRulesParametersUuidColumnNotNullable extends MakeUuidColumnNotNullable { + private static final String TABLE_NAME = "rules_parameters"; + + public MakeRulesParametersUuidColumnNotNullable(Database db) { + super(db, TABLE_NAME); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/PopulateRulesParametersUuid.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/PopulateRulesParametersUuid.java new file mode 100644 index 00000000000..c86a5d0cbce --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/PopulateRulesParametersUuid.java @@ -0,0 +1,50 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.rulesparameters; + +import java.sql.SQLException; +import org.sonar.core.util.UuidFactory; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.DataChange; +import org.sonar.server.platform.db.migration.step.MassUpdate; + +public class PopulateRulesParametersUuid extends DataChange { + + private final UuidFactory uuidFactory; + + public PopulateRulesParametersUuid(Database db, UuidFactory uuidFactory) { + super(db); + this.uuidFactory = uuidFactory; + } + + @Override + protected void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + + massUpdate.select("select id from rules_parameters where uuid is null"); + massUpdate.update("update rules_parameters set uuid = ? where id = ?"); + + massUpdate.execute((row, update) -> { + update.setString(1, uuidFactory.create()); + update.setLong(2, row.getLong(1)); + return true; + }); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/AddRulesParameterUuidColumnToActiveRuleParameters.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/AddRulesParameterUuidColumnToActiveRuleParameters.java new file mode 100644 index 00000000000..a38d87cf8e8 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/AddRulesParameterUuidColumnToActiveRuleParameters.java @@ -0,0 +1,50 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.rulesparameters.fk; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.def.VarcharColumnDef; +import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class AddRulesParameterUuidColumnToActiveRuleParameters extends DdlChange { + private static final String TABLE = "active_rule_parameters"; + + private static final VarcharColumnDef uuidColumnDefinition = newVarcharColumnDefBuilder() + .setColumnName("rules_parameter_uuid") + .setIsNullable(true) + .setDefaultValue(null) + .setLimit(VarcharColumnDef.UUID_SIZE) + .build(); + + public AddRulesParameterUuidColumnToActiveRuleParameters(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AddColumnsBuilder(getDialect(), TABLE) + .addColumn(uuidColumnDefinition) + .build()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/DropRulesParameterIdColumnOfActiveRuleParametersTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/DropRulesParameterIdColumnOfActiveRuleParametersTable.java new file mode 100644 index 00000000000..aaad275ba86 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/DropRulesParameterIdColumnOfActiveRuleParametersTable.java @@ -0,0 +1,37 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.rulesparameters.fk; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.sql.DropColumnsBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +public class DropRulesParameterIdColumnOfActiveRuleParametersTable extends DdlChange { + + public DropRulesParameterIdColumnOfActiveRuleParametersTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new DropColumnsBuilder(getDialect(), "active_rule_parameters", "rules_parameter_id").build()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/MakeActiveRuleParametersRulesParameterUuidColumnNotNullable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/MakeActiveRuleParametersRulesParameterUuidColumnNotNullable.java new file mode 100644 index 00000000000..bd236b9ed8d --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/MakeActiveRuleParametersRulesParameterUuidColumnNotNullable.java @@ -0,0 +1,50 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.rulesparameters.fk; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.def.VarcharColumnDef; +import org.sonar.server.platform.db.migration.sql.AlterColumnsBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class MakeActiveRuleParametersRulesParameterUuidColumnNotNullable extends DdlChange { + private static final String TABLE = "active_rule_parameters"; + + private static final VarcharColumnDef uuidColumnDefinition = newVarcharColumnDefBuilder() + .setColumnName("rules_parameter_uuid") + .setIsNullable(false) + .setDefaultValue(null) + .setLimit(VarcharColumnDef.UUID_SIZE) + .build(); + + public MakeActiveRuleParametersRulesParameterUuidColumnNotNullable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AlterColumnsBuilder(getDialect(), TABLE) + .updateColumn(uuidColumnDefinition) + .build()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/PopulateActiveRuleParametersRulesParameterUuid.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/PopulateActiveRuleParametersRulesParameterUuid.java new file mode 100644 index 00000000000..62ae81a95cd --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/PopulateActiveRuleParametersRulesParameterUuid.java @@ -0,0 +1,49 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.rulesparameters.fk; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.DataChange; +import org.sonar.server.platform.db.migration.step.MassUpdate; + +public class PopulateActiveRuleParametersRulesParameterUuid extends DataChange { + + public PopulateActiveRuleParametersRulesParameterUuid(Database db) { + super(db); + } + + @Override + protected void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + + massUpdate.select("select arp.uuid, rp.uuid " + + "from active_rule_parameters arp " + + "join rules_parameters rp on arp.rules_parameter_id = rp.id " + + "where arp.rules_parameter_uuid is null"); + massUpdate.update("update active_rule_parameters set rules_parameter_uuid = ? where uuid = ?"); + + massUpdate.execute((row, update) -> { + update.setString(1, row.getString(2)); + update.setString(2, row.getString(1)); + return true; + }); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/AddPrimaryKeyOnUuidColumnOfRulesParametersTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/AddPrimaryKeyOnUuidColumnOfRulesParametersTableTest.java new file mode 100644 index 00000000000..5b8f6d6fa5b --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/AddPrimaryKeyOnUuidColumnOfRulesParametersTableTest.java @@ -0,0 +1,51 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.rulesparameters; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.MigrationStep; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class AddPrimaryKeyOnUuidColumnOfRulesParametersTableTest { + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(AddPrimaryKeyOnUuidColumnOfRulesParametersTableTest.class, "schema.sql"); + + private MigrationStep underTest = new AddPrimaryKeyOnUuidColumnOfRulesParametersTable(db.database()); + + @Test + public void execute() throws SQLException { + underTest.execute(); + + db.assertPrimaryKey("rules_parameters", "pk_rules_parameters", "uuid"); + } + + @Test + public void migration_is_not_re_entrant() throws SQLException { + underTest.execute(); + + assertThatThrownBy(() -> underTest.execute()).isInstanceOf(IllegalStateException.class); + } + +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/AddUuidColumnToRulesParametersTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/AddUuidColumnToRulesParametersTest.java new file mode 100644 index 00000000000..ce27de8579c --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/AddUuidColumnToRulesParametersTest.java @@ -0,0 +1,67 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.rulesparameters; + +import java.sql.SQLException; +import java.sql.Types; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.core.util.UuidFactory; +import org.sonar.core.util.UuidFactoryFast; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.assertj.core.api.Assertions.assertThat; + +public class AddUuidColumnToRulesParametersTest { + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(AddUuidColumnToRulesParametersTest.class, "schema.sql"); + + private UuidFactory uuidFactory = UuidFactoryFast.getInstance(); + + private DdlChange underTest = new AddUuidColumnToRulesParameters(db.database()); + + @Before + public void setup() { + insertRuleParameter(1L); + insertRuleParameter(2L); + insertRuleParameter(3L); + } + + @Test + public void add_uuid_column_to_project_measures() throws SQLException { + underTest.execute(); + + db.assertColumnDefinition("rules_parameters", "uuid", Types.VARCHAR, 40, true); + + assertThat(db.countSql("select count(id) from rules_parameters")) + .isEqualTo(3); + } + + private void insertRuleParameter(Long id) { + db.executeInsert("rules_parameters", + "id", id, + "rule_id", id + 100, + "name", uuidFactory.create(), + "param_type", uuidFactory.create()); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/DropIdColumnOfRulesParametersTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/DropIdColumnOfRulesParametersTableTest.java new file mode 100644 index 00000000000..4d39980a948 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/DropIdColumnOfRulesParametersTableTest.java @@ -0,0 +1,51 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.rulesparameters; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.MigrationStep; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class DropIdColumnOfRulesParametersTableTest { + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(DropIdColumnOfRulesParametersTableTest.class, "schema.sql"); + + private MigrationStep underTest = new DropIdColumnOfRulesParametersTable(db.database()); + + @Test + public void execute() throws SQLException { + underTest.execute(); + + db.assertColumnDoesNotExist("rules_parameters", "id"); + } + + @Test + public void migration_is_not_re_entrant() throws SQLException { + underTest.execute(); + + assertThatThrownBy(() -> underTest.execute()).isInstanceOf(IllegalStateException.class); + } + +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/DropPrimaryKeyOnIdColumnOfRulesParametersTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/DropPrimaryKeyOnIdColumnOfRulesParametersTableTest.java new file mode 100644 index 00000000000..d07fa6118b7 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/DropPrimaryKeyOnIdColumnOfRulesParametersTableTest.java @@ -0,0 +1,56 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.rulesparameters; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.MigrationStep; +import org.sonar.server.platform.db.migration.version.v83.util.DropPrimaryKeySqlGenerator; +import org.sonar.server.platform.db.migration.version.v83.util.SqlHelper; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class DropPrimaryKeyOnIdColumnOfRulesParametersTableTest { + + private static final String TABLE_NAME = "rules_parameters"; + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(DropPrimaryKeyOnIdColumnOfRulesParametersTableTest.class, "schema.sql"); + + private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), new SqlHelper(db.database())); + + private MigrationStep underTest = new DropPrimaryKeyOnIdColumnOfRulesParametersTable(db.database(), dropPrimaryKeySqlGenerator); + + @Test + public void execute() throws SQLException { + underTest.execute(); + + db.assertNoPrimaryKey(TABLE_NAME); + } + + @Test + public void migration_is_not_re_entrant() throws SQLException { + underTest.execute(); + + assertThatThrownBy(() -> underTest.execute()).isInstanceOf(IllegalStateException.class); + } + +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/MakeRulesParametersUuidColumnNotNullableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/MakeRulesParametersUuidColumnNotNullableTest.java new file mode 100644 index 00000000000..d2f29cf376d --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/MakeRulesParametersUuidColumnNotNullableTest.java @@ -0,0 +1,43 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.rulesparameters; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.MigrationStep; + +import static java.sql.Types.VARCHAR; + +public class MakeRulesParametersUuidColumnNotNullableTest { + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(MakeRulesParametersUuidColumnNotNullableTest.class, "schema.sql"); + + private MigrationStep underTest = new MakeRulesParametersUuidColumnNotNullable(db.database()); + + @Test + public void uuid_column_is_not_null() throws SQLException { + underTest.execute(); + + db.assertColumnDefinition("rules_parameters", "uuid", VARCHAR, null, false); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/PopulateRulesParametersUuidTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/PopulateRulesParametersUuidTest.java new file mode 100644 index 00000000000..0da6eba665f --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/PopulateRulesParametersUuidTest.java @@ -0,0 +1,82 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.rulesparameters; + +import java.sql.SQLException; +import java.util.Objects; +import java.util.stream.Collectors; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.core.util.UuidFactory; +import org.sonar.core.util.UuidFactoryFast; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DataChange; + +import static org.assertj.core.api.Assertions.assertThat; + +public class PopulateRulesParametersUuidTest { + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(PopulateRulesParametersUuidTest.class, "schema.sql"); + + private UuidFactory uuidFactory = UuidFactoryFast.getInstance(); + private DataChange underTest = new PopulateRulesParametersUuid(db.database(), uuidFactory); + + @Test + public void populate_uuids_and_created_at() throws SQLException { + insertRuleParameter(1L); + insertRuleParameter(2L); + insertRuleParameter(3L); + + underTest.execute(); + + verifyUuidsAreNotNull(); + } + + @Test + public void migration_is_reentrant() throws SQLException { + insertRuleParameter(1L); + insertRuleParameter(2L); + insertRuleParameter(3L); + + underTest.execute(); + // re-entrant + underTest.execute(); + + verifyUuidsAreNotNull(); + } + + private void verifyUuidsAreNotNull() { + assertThat(db.select("select uuid from rules_parameters") + .stream() + .map(row -> row.get("UUID")) + .filter(Objects::isNull) + .collect(Collectors.toList())).isEmpty(); + } + + private void insertRuleParameter(Long id) { + db.executeInsert("rules_parameters", + "id", id, + "rule_id", id + 100, + "name", uuidFactory.create(), + "param_type", uuidFactory.create()); + } + +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/AddRulesParameterUuidColumnToActiveRuleParametersTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/AddRulesParameterUuidColumnToActiveRuleParametersTest.java new file mode 100644 index 00000000000..5303f2fe192 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/AddRulesParameterUuidColumnToActiveRuleParametersTest.java @@ -0,0 +1,66 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.rulesparameters.fk; + +import java.sql.SQLException; +import java.sql.Types; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.core.util.UuidFactory; +import org.sonar.core.util.UuidFactoryFast; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.assertj.core.api.Assertions.assertThat; + +public class AddRulesParameterUuidColumnToActiveRuleParametersTest { + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(AddRulesParameterUuidColumnToActiveRuleParametersTest.class, "schema.sql"); + + private UuidFactory uuidFactory = UuidFactoryFast.getInstance(); + + private DdlChange underTest = new AddRulesParameterUuidColumnToActiveRuleParameters(db.database()); + + @Before + public void setup() { + insertActiveRuleParameter(uuidFactory.create(), 1L, 4L); + insertActiveRuleParameter(uuidFactory.create(), 2L, 5L); + insertActiveRuleParameter(uuidFactory.create(), 3L, 6L); + } + + @Test + public void add_rules_parameter_uuid_column_to_active_rule_parameters() throws SQLException { + underTest.execute(); + + db.assertColumnDefinition("active_rule_parameters", "rules_parameter_uuid", Types.VARCHAR, 40, true); + + assertThat(db.countRowsOfTable("active_rule_parameters")) + .isEqualTo(3); + } + + private void insertActiveRuleParameter(String uuid, Long ruleId, Long rulesParameterId) { + db.executeInsert("active_rule_parameters", + "uuid", uuid, + "active_rule_id", ruleId, + "rules_parameter_id", rulesParameterId); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/DropRulesParameterIdColumnOfActiveRuleParametersTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/DropRulesParameterIdColumnOfActiveRuleParametersTableTest.java new file mode 100644 index 00000000000..da25ea3d878 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/DropRulesParameterIdColumnOfActiveRuleParametersTableTest.java @@ -0,0 +1,51 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.rulesparameters.fk; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.MigrationStep; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class DropRulesParameterIdColumnOfActiveRuleParametersTableTest { + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(DropRulesParameterIdColumnOfActiveRuleParametersTableTest.class, "schema.sql"); + + private MigrationStep underTest = new DropRulesParameterIdColumnOfActiveRuleParametersTable(db.database()); + + @Test + public void execute() throws SQLException { + underTest.execute(); + + db.assertColumnDoesNotExist("active_rule_parameters", "rules_parameter_id"); + } + + @Test + public void migration_is_not_re_entrant() throws SQLException { + underTest.execute(); + + assertThatThrownBy(() -> underTest.execute()).isInstanceOf(IllegalStateException.class); + } + +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/MakeActiveRuleParametersRulesParameterUuidColumnNotNullableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/MakeActiveRuleParametersRulesParameterUuidColumnNotNullableTest.java new file mode 100644 index 00000000000..b9951ffc3c2 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/MakeActiveRuleParametersRulesParameterUuidColumnNotNullableTest.java @@ -0,0 +1,43 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.rulesparameters.fk; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.MigrationStep; + +import static java.sql.Types.VARCHAR; + +public class MakeActiveRuleParametersRulesParameterUuidColumnNotNullableTest { + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(MakeActiveRuleParametersRulesParameterUuidColumnNotNullableTest.class, "schema.sql"); + + private MigrationStep underTest = new MakeActiveRuleParametersRulesParameterUuidColumnNotNullable(db.database()); + + @Test + public void uuid_column_is_not_null() throws SQLException { + underTest.execute(); + + db.assertColumnDefinition("active_rule_parameters", "rules_parameter_uuid", VARCHAR, null, false); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/PopulateActiveRuleParametersRulesParameterUuidTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/PopulateActiveRuleParametersRulesParameterUuidTest.java new file mode 100644 index 00000000000..f0b6c3254db --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/PopulateActiveRuleParametersRulesParameterUuidTest.java @@ -0,0 +1,127 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v83.rulesparameters.fk; + +import java.sql.SQLException; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.core.util.UuidFactory; +import org.sonar.core.util.UuidFactoryFast; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DataChange; + +import static java.lang.String.format; +import static org.assertj.core.api.Assertions.assertThat; + +public class PopulateActiveRuleParametersRulesParameterUuidTest { + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(PopulateActiveRuleParametersRulesParameterUuidTest.class, "schema.sql"); + + private UuidFactory uuidFactory = UuidFactoryFast.getInstance(); + private DataChange underTest = new PopulateActiveRuleParametersRulesParameterUuid(db.database()); + + @Test + public void populate_rules_parameter_uuids() throws SQLException { + String ruleParamUuid1 = uuidFactory.create(); + long ruleParamId1 = 1L; + insertRuleParameter(ruleParamId1, ruleParamUuid1, 101L); + String activeRuleParameter11 = insertActiveRuleParameter(101L, ruleParamId1); + String activeRuleParameter12 = insertActiveRuleParameter(101L, ruleParamId1); + String activeRuleParameter13 = insertActiveRuleParameter(101L, ruleParamId1); + + String ruleParamUuid2 = uuidFactory.create(); + long ruleParamId2 = 2L; + insertRuleParameter(ruleParamId2, ruleParamUuid2, 101L); + String activeRuleParameter21 = insertActiveRuleParameter(101L, ruleParamId2); + String activeRuleParameter22 = insertActiveRuleParameter(101L, ruleParamId2); + + insertRuleParameter(3L, uuidFactory.create(), 101L); + + underTest.execute(); + + assertRulesParameterUuidsAreNotNull(); + assertThatRulesParametersUuidAreSet(ruleParamUuid1, activeRuleParameter11, activeRuleParameter12, activeRuleParameter13); + assertThatRulesParametersUuidAreSet(ruleParamUuid2, activeRuleParameter21, activeRuleParameter22); + } + + @Test + public void migration_is_reentrant() throws SQLException { + String ruleParamUuid1 = uuidFactory.create(); + long ruleParamId1 = 1L; + insertRuleParameter(ruleParamId1, ruleParamUuid1, 101L); + String activeRuleParameter11 = insertActiveRuleParameter(101L, ruleParamId1); + String activeRuleParameter12 = insertActiveRuleParameter(101L, ruleParamId1); + String activeRuleParameter13 = insertActiveRuleParameter(101L, ruleParamId1); + + underTest.execute(); + + String ruleParamUuid2 = uuidFactory.create(); + long ruleParamId2 = 2L; + insertRuleParameter(ruleParamId2, ruleParamUuid2, 101L); + String activeRuleParameter21 = insertActiveRuleParameter(101L, ruleParamId2); + String activeRuleParameter22 = insertActiveRuleParameter(101L, ruleParamId2); + // re-entrant + underTest.execute(); + + assertRulesParameterUuidsAreNotNull(); + assertThatRulesParametersUuidAreSet(ruleParamUuid1, activeRuleParameter11, activeRuleParameter12, activeRuleParameter13); + assertThatRulesParametersUuidAreSet(ruleParamUuid2, activeRuleParameter21, activeRuleParameter22); + } + + private void assertThatRulesParametersUuidAreSet(String ruleParameterUuid, String... activeRuleParameters) { + assertThat(db.select("select rules_parameter_uuid from active_rule_parameters where uuid in (" + + Stream.of(activeRuleParameters).map(s -> format("'%s'", s)).collect(Collectors.joining(",")) + ")") + .stream() + .map(row -> row.get("RULES_PARAMETER_UUID")) + .filter(o -> !Objects.equals(o, ruleParameterUuid)) + .collect(Collectors.toList())).isEmpty(); + } + + private void assertRulesParameterUuidsAreNotNull() { + assertThat(db.select("select rules_parameter_uuid from active_rule_parameters") + .stream() + .map(row -> row.get("RULES_PARAMETER_UUID")) + .filter(Objects::isNull) + .collect(Collectors.toList())).isEmpty(); + } + + private void insertRuleParameter(Long id, String uuid, Long ruleId) { + db.executeInsert("rules_parameters", + "id", id, + "uuid", uuid, + "rule_id", ruleId, + "name", uuidFactory.create(), + "param_type", uuidFactory.create()); + } + + private String insertActiveRuleParameter(Long ruleId, Long rulesParameterId) { + String uuid = uuidFactory.create(); + db.executeInsert("active_rule_parameters", + "uuid", uuid, + "active_rule_id", ruleId, + "rules_parameter_id", rulesParameterId); + return uuid; + } + +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/AddPrimaryKeyOnUuidColumnOfRulesParametersTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/AddPrimaryKeyOnUuidColumnOfRulesParametersTableTest/schema.sql new file mode 100644 index 00000000000..961a5cb8cac --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/AddPrimaryKeyOnUuidColumnOfRulesParametersTableTest/schema.sql @@ -0,0 +1,11 @@ +CREATE TABLE "RULES_PARAMETERS"( + "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1), + "UUID" VARCHAR(40) NOT NULL, + "RULE_ID" INTEGER NOT NULL, + "NAME" VARCHAR(128) NOT NULL, + "DESCRIPTION" VARCHAR(4000), + "PARAM_TYPE" VARCHAR(512) NOT NULL, + "DEFAULT_VALUE" VARCHAR(4000) +); +CREATE INDEX "RULES_PARAMETERS_RULE_ID" ON "RULES_PARAMETERS"("RULE_ID"); +CREATE UNIQUE INDEX "RULES_PARAMETERS_UNIQUE" ON "RULES_PARAMETERS"("RULE_ID", "NAME"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/AddUuidColumnToRulesParametersTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/AddUuidColumnToRulesParametersTest/schema.sql new file mode 100644 index 00000000000..98a62c638d9 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/AddUuidColumnToRulesParametersTest/schema.sql @@ -0,0 +1,11 @@ +CREATE TABLE "RULES_PARAMETERS"( + "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1), + "RULE_ID" INTEGER NOT NULL, + "NAME" VARCHAR(128) NOT NULL, + "DESCRIPTION" VARCHAR(4000), + "PARAM_TYPE" VARCHAR(512) NOT NULL, + "DEFAULT_VALUE" VARCHAR(4000) +); +ALTER TABLE "RULES_PARAMETERS" ADD CONSTRAINT "PK_RULES_PARAMETERS" PRIMARY KEY("ID"); +CREATE INDEX "RULES_PARAMETERS_RULE_ID" ON "RULES_PARAMETERS"("RULE_ID"); +CREATE UNIQUE INDEX "RULES_PARAMETERS_UNIQUE" ON "RULES_PARAMETERS"("RULE_ID", "NAME"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/DropIdColumnOfRulesParametersTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/DropIdColumnOfRulesParametersTableTest/schema.sql new file mode 100644 index 00000000000..60597fff321 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/DropIdColumnOfRulesParametersTableTest/schema.sql @@ -0,0 +1,12 @@ +CREATE TABLE "RULES_PARAMETERS"( + "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1), + "UUID" VARCHAR(40) NOT NULL, + "RULE_ID" INTEGER NOT NULL, + "NAME" VARCHAR(128) NOT NULL, + "DESCRIPTION" VARCHAR(4000), + "PARAM_TYPE" VARCHAR(512) NOT NULL, + "DEFAULT_VALUE" VARCHAR(4000) +); +ALTER TABLE "RULES_PARAMETERS" ADD CONSTRAINT "PK_RULES_PARAMETERS" PRIMARY KEY("UUID"); +CREATE INDEX "RULES_PARAMETERS_RULE_ID" ON "RULES_PARAMETERS"("RULE_ID"); +CREATE UNIQUE INDEX "RULES_PARAMETERS_UNIQUE" ON "RULES_PARAMETERS"("RULE_ID", "NAME"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/DropPrimaryKeyOnIdColumnOfRulesParametersTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/DropPrimaryKeyOnIdColumnOfRulesParametersTableTest/schema.sql new file mode 100644 index 00000000000..efdec30874f --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/DropPrimaryKeyOnIdColumnOfRulesParametersTableTest/schema.sql @@ -0,0 +1,12 @@ +CREATE TABLE "RULES_PARAMETERS"( + "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1), + "UUID" VARCHAR(40) NOT NULL, + "RULE_ID" INTEGER NOT NULL, + "NAME" VARCHAR(128) NOT NULL, + "DESCRIPTION" VARCHAR(4000), + "PARAM_TYPE" VARCHAR(512) NOT NULL, + "DEFAULT_VALUE" VARCHAR(4000) +); +ALTER TABLE "RULES_PARAMETERS" ADD CONSTRAINT "PK_RULES_PARAMETERS" PRIMARY KEY("ID"); +CREATE INDEX "RULES_PARAMETERS_RULE_ID" ON "RULES_PARAMETERS"("RULE_ID"); +CREATE UNIQUE INDEX "RULES_PARAMETERS_UNIQUE" ON "RULES_PARAMETERS"("RULE_ID", "NAME"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/MakeRulesParametersUuidColumnNotNullableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/MakeRulesParametersUuidColumnNotNullableTest/schema.sql new file mode 100644 index 00000000000..55efee58993 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/MakeRulesParametersUuidColumnNotNullableTest/schema.sql @@ -0,0 +1,12 @@ +CREATE TABLE "RULES_PARAMETERS"( + "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1), + "UUID" VARCHAR(40), + "RULE_ID" INTEGER NOT NULL, + "NAME" VARCHAR(128) NOT NULL, + "DESCRIPTION" VARCHAR(4000), + "PARAM_TYPE" VARCHAR(512) NOT NULL, + "DEFAULT_VALUE" VARCHAR(4000) +); +ALTER TABLE "RULES_PARAMETERS" ADD CONSTRAINT "PK_RULES_PARAMETERS" PRIMARY KEY("ID"); +CREATE INDEX "RULES_PARAMETERS_RULE_ID" ON "RULES_PARAMETERS"("RULE_ID"); +CREATE UNIQUE INDEX "RULES_PARAMETERS_UNIQUE" ON "RULES_PARAMETERS"("RULE_ID", "NAME"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/PopulateRulesParametersUuidTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/PopulateRulesParametersUuidTest/schema.sql new file mode 100644 index 00000000000..55efee58993 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/PopulateRulesParametersUuidTest/schema.sql @@ -0,0 +1,12 @@ +CREATE TABLE "RULES_PARAMETERS"( + "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1), + "UUID" VARCHAR(40), + "RULE_ID" INTEGER NOT NULL, + "NAME" VARCHAR(128) NOT NULL, + "DESCRIPTION" VARCHAR(4000), + "PARAM_TYPE" VARCHAR(512) NOT NULL, + "DEFAULT_VALUE" VARCHAR(4000) +); +ALTER TABLE "RULES_PARAMETERS" ADD CONSTRAINT "PK_RULES_PARAMETERS" PRIMARY KEY("ID"); +CREATE INDEX "RULES_PARAMETERS_RULE_ID" ON "RULES_PARAMETERS"("RULE_ID"); +CREATE UNIQUE INDEX "RULES_PARAMETERS_UNIQUE" ON "RULES_PARAMETERS"("RULE_ID", "NAME"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/AddRulesParameterUuidColumnToActiveRuleParametersTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/AddRulesParameterUuidColumnToActiveRuleParametersTest/schema.sql new file mode 100644 index 00000000000..04d58e9edf0 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/AddRulesParameterUuidColumnToActiveRuleParametersTest/schema.sql @@ -0,0 +1,9 @@ +CREATE TABLE "ACTIVE_RULE_PARAMETERS"( + "ACTIVE_RULE_ID" INTEGER NOT NULL, + "RULES_PARAMETER_ID" INTEGER NOT NULL, + "VALUE" VARCHAR(4000), + "RULES_PARAMETER_KEY" VARCHAR(128), + "UUID" VARCHAR(40) NOT NULL +); +ALTER TABLE "ACTIVE_RULE_PARAMETERS" ADD CONSTRAINT "PK_ACTIVE_RULE_PARAMETERS" PRIMARY KEY("UUID"); +CREATE INDEX "IX_ARP_ON_ACTIVE_RULE_ID" ON "ACTIVE_RULE_PARAMETERS"("ACTIVE_RULE_ID"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/DropRulesParameterIdColumnOfActiveRuleParametersTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/DropRulesParameterIdColumnOfActiveRuleParametersTableTest/schema.sql new file mode 100644 index 00000000000..3782982a167 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/DropRulesParameterIdColumnOfActiveRuleParametersTableTest/schema.sql @@ -0,0 +1,10 @@ +CREATE TABLE "ACTIVE_RULE_PARAMETERS"( + "ACTIVE_RULE_ID" INTEGER NOT NULL, + "RULES_PARAMETER_ID" INTEGER NOT NULL, + "RULES_PARAMETER_UUID" VARCHAR(40) NOT NULL, + "VALUE" VARCHAR(4000), + "RULES_PARAMETER_KEY" VARCHAR(128), + "UUID" VARCHAR(40) NOT NULL +); +ALTER TABLE "ACTIVE_RULE_PARAMETERS" ADD CONSTRAINT "PK_ACTIVE_RULE_PARAMETERS" PRIMARY KEY("UUID"); +CREATE INDEX "IX_ARP_ON_ACTIVE_RULE_ID" ON "ACTIVE_RULE_PARAMETERS"("ACTIVE_RULE_ID"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/MakeActiveRuleParametersRulesParameterUuidColumnNotNullableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/MakeActiveRuleParametersRulesParameterUuidColumnNotNullableTest/schema.sql new file mode 100644 index 00000000000..de06e416db2 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/MakeActiveRuleParametersRulesParameterUuidColumnNotNullableTest/schema.sql @@ -0,0 +1,10 @@ +CREATE TABLE "ACTIVE_RULE_PARAMETERS"( + "ACTIVE_RULE_ID" INTEGER NOT NULL, + "RULES_PARAMETER_ID" INTEGER NOT NULL, + "RULES_PARAMETER_UUID" VARCHAR(40), + "VALUE" VARCHAR(4000), + "RULES_PARAMETER_KEY" VARCHAR(128), + "UUID" VARCHAR(40) NOT NULL +); +ALTER TABLE "ACTIVE_RULE_PARAMETERS" ADD CONSTRAINT "PK_ACTIVE_RULE_PARAMETERS" PRIMARY KEY("UUID"); +CREATE INDEX "IX_ARP_ON_ACTIVE_RULE_ID" ON "ACTIVE_RULE_PARAMETERS"("ACTIVE_RULE_ID"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/PopulateActiveRuleParametersRulesParameterUuidTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/PopulateActiveRuleParametersRulesParameterUuidTest/schema.sql new file mode 100644 index 00000000000..0bb696c346c --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/rulesparameters/fk/PopulateActiveRuleParametersRulesParameterUuidTest/schema.sql @@ -0,0 +1,23 @@ +CREATE TABLE "RULES_PARAMETERS"( + "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1), + "UUID" VARCHAR(40) NOT NULL, + "RULE_ID" INTEGER NOT NULL, + "NAME" VARCHAR(128) NOT NULL, + "DESCRIPTION" VARCHAR(4000), + "PARAM_TYPE" VARCHAR(512) NOT NULL, + "DEFAULT_VALUE" VARCHAR(4000) +); +ALTER TABLE "RULES_PARAMETERS" ADD CONSTRAINT "PK_RULES_PARAMETERS" PRIMARY KEY("ID"); +CREATE INDEX "RULES_PARAMETERS_RULE_ID" ON "RULES_PARAMETERS"("RULE_ID"); +CREATE UNIQUE INDEX "RULES_PARAMETERS_UNIQUE" ON "RULES_PARAMETERS"("RULE_ID", "NAME"); + +CREATE TABLE "ACTIVE_RULE_PARAMETERS"( + "ACTIVE_RULE_ID" INTEGER NOT NULL, + "RULES_PARAMETER_ID" INTEGER NOT NULL, + "RULES_PARAMETER_UUID" VARCHAR(40), + "VALUE" VARCHAR(4000), + "RULES_PARAMETER_KEY" VARCHAR(128), + "UUID" VARCHAR(40) NOT NULL +); +ALTER TABLE "ACTIVE_RULE_PARAMETERS" ADD CONSTRAINT "PK_ACTIVE_RULE_PARAMETERS" PRIMARY KEY("UUID"); +CREATE INDEX "IX_ARP_ON_ACTIVE_RULE_ID" ON "ACTIVE_RULE_PARAMETERS"("ACTIVE_RULE_ID"); diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/RegisterRules.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/RegisterRules.java index 799cd207ba7..232778f3572 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/RegisterRules.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/RegisterRules.java @@ -554,7 +554,7 @@ public class RegisterRules implements Startable { profiler.start(); dbClient.activeRuleDao().deleteParamsByRuleParamOfAllOrganizations(session, paramDto); profiler.stopDebug(format("Propagate deleted param with name %s to active rules of rule %s", paramDto.getName(), rule.getKey())); - dbClient.ruleDao().deleteRuleParam(session, paramDto.getId()); + dbClient.ruleDao().deleteRuleParam(session, paramDto.getUuid()); } else { if (mergeParam(paramDto, paramDef)) { dbClient.ruleDao().updateRuleParam(session, rule, paramDto); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryImplTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryImplTest.java index 7de814aad06..f365dadb1bb 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryImplTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryImplTest.java @@ -251,7 +251,7 @@ public class QProfileFactoryImplTest { ActiveRuleDto activeRuleDto = db.qualityProfiles().activateRule(profile, rule); ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto() - .setRulesParameterId(ruleParam.getId()) + .setRulesParameterUuid(ruleParam.getUuid()) .setKey("foo") .setValue("bar"); db.getDbClient().activeRuleDao().insertParam(dbSession, activeRuleDto, activeRuleParam); @@ -277,7 +277,7 @@ public class QProfileFactoryImplTest { db.getDbClient().activeRuleDao().insert(dbSession, activeRuleDto); ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto() - .setRulesParameterId(ruleParam.getId()) + .setRulesParameterUuid(ruleParam.getUuid()) .setKey("foo") .setValue("bar"); db.getDbClient().activeRuleDao().insertParam(dbSession, activeRuleDto, activeRuleParam); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/RuleUpdaterTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/RuleUpdaterTest.java index 4832470ba06..adc22dd84b2 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/RuleUpdaterTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/RuleUpdaterTest.java @@ -458,7 +458,7 @@ public class RuleUpdaterTest { db.getDbClient().activeRuleDao().insert(dbSession, activeRuleDto); db.getDbClient().activeRuleDao().insertParam(dbSession, activeRuleDto, new ActiveRuleParamDto() .setActiveRuleUuid(activeRuleDto.getUuid()) - .setRulesParameterId(ruleParam1.getId()) + .setRulesParameterUuid(ruleParam1.getUuid()) .setKey(ruleParam1.getName()) .setValue(ruleParam1.getDefaultValue())); dbSession.commit(); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/ShowActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/ShowActionTest.java index 50f33b8de1e..454001cb508 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/ShowActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/ShowActionTest.java @@ -421,7 +421,7 @@ public class ShowActionTest { QProfileDto qProfile = db.qualityProfiles().insert(organization); ActiveRuleDto activeRule = db.qualityProfiles().activateRule(qProfile, rule); db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRule, new ActiveRuleParamDto() - .setRulesParameterId(ruleParam.getId()) + .setRulesParameterUuid(ruleParam.getUuid()) .setKey(ruleParam.getName()) .setValue(".*?")); db.commit(); |