From 8d0f9c65e49865225ec42e29c455a3308be66040 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 18 Feb 2015 09:31:03 +0100 Subject: [PATCH] SONAR-6187 Nothing should be done when there's no characteristic in db --- ...SubCharacteristicsComplianceMigration.java | 55 ++++++++++--------- ...haracteristicsComplianceMigrationTest.java | 7 +++ .../empty.xml | 3 + 3 files changed, 40 insertions(+), 25 deletions(-) create mode 100644 server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v501/AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigrationTest/empty.xml diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v501/AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigration.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v501/AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigration.java index 3ccc765c1b2..536c531b6f8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v501/AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigration.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v501/AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigration.java @@ -43,6 +43,8 @@ import static com.google.common.collect.Lists.newArrayList; * Add a new Characteristic 'Usability' with 2 sub-characteristics 'Accessibility' and 'Ease of Use' * and add a new sub-characteristic 'Compliance' for all characteristics. * + * Nothing will be done if there's no characteristics in db, as they're all gonna be created by {@link org.sonar.server.startup.RegisterDebtModel} + * * @since 5.0.1 */ public class AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigration extends BaseDataChange { @@ -63,17 +65,20 @@ public class AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigration public void execute(Context context) throws SQLException { CharacteristicsContext characteristicsContext = new CharacteristicsContext(context, system); - int usabilityOder = moveCharacteristicsDownToBeAbleToInsertUsability(characteristicsContext); - createOrUpdateUsabilityCharacteristicAndItsSubCharacteristic(characteristicsContext, usabilityOder); - - createSubCharacteristic(characteristicsContext, "REUSABILITY" + COMPLIANCE_KEY_SUFFIX, "Reusability " + COMPLIANCE_NAME, "REUSABILITY"); - createSubCharacteristic(characteristicsContext, "PORTABILITY" + COMPLIANCE_KEY_SUFFIX, "Portability " + COMPLIANCE_NAME, "PORTABILITY"); - createSubCharacteristic(characteristicsContext, "MAINTAINABILITY" + COMPLIANCE_KEY_SUFFIX, "Maintainability " + COMPLIANCE_NAME, "MAINTAINABILITY"); - createSubCharacteristic(characteristicsContext, "SECURITY" + COMPLIANCE_KEY_SUFFIX, "Security " + COMPLIANCE_NAME, "SECURITY"); - createSubCharacteristic(characteristicsContext, "EFFICIENCY" + COMPLIANCE_KEY_SUFFIX, "Efficiency " + COMPLIANCE_NAME, "EFFICIENCY"); - createSubCharacteristic(characteristicsContext, "CHANGEABILITY" + COMPLIANCE_KEY_SUFFIX, "Changeability " + COMPLIANCE_NAME, "CHANGEABILITY"); - createSubCharacteristic(characteristicsContext, "RELIABILITY" + COMPLIANCE_KEY_SUFFIX, "Reliability " + COMPLIANCE_NAME, "RELIABILITY"); - createSubCharacteristic(characteristicsContext, "TESTABILITY" + COMPLIANCE_KEY_SUFFIX, "Testability " + COMPLIANCE_NAME, "TESTABILITY"); + // On an empty DB, there are no characteristics, they're all gonna be created after in RegisterDebtModel + if (!characteristicsContext.characteristics().isEmpty()) { + int usabilityOder = moveCharacteristicsDownToBeAbleToInsertUsability(characteristicsContext); + createOrUpdateUsabilityCharacteristicAndItsSubCharacteristic(characteristicsContext, usabilityOder); + + createSubCharacteristic(characteristicsContext, "REUSABILITY" + COMPLIANCE_KEY_SUFFIX, "Reusability " + COMPLIANCE_NAME, "REUSABILITY"); + createSubCharacteristic(characteristicsContext, "PORTABILITY" + COMPLIANCE_KEY_SUFFIX, "Portability " + COMPLIANCE_NAME, "PORTABILITY"); + createSubCharacteristic(characteristicsContext, "MAINTAINABILITY" + COMPLIANCE_KEY_SUFFIX, "Maintainability " + COMPLIANCE_NAME, "MAINTAINABILITY"); + createSubCharacteristic(characteristicsContext, "SECURITY" + COMPLIANCE_KEY_SUFFIX, "Security " + COMPLIANCE_NAME, "SECURITY"); + createSubCharacteristic(characteristicsContext, "EFFICIENCY" + COMPLIANCE_KEY_SUFFIX, "Efficiency " + COMPLIANCE_NAME, "EFFICIENCY"); + createSubCharacteristic(characteristicsContext, "CHANGEABILITY" + COMPLIANCE_KEY_SUFFIX, "Changeability " + COMPLIANCE_NAME, "CHANGEABILITY"); + createSubCharacteristic(characteristicsContext, "RELIABILITY" + COMPLIANCE_KEY_SUFFIX, "Reliability " + COMPLIANCE_NAME, "RELIABILITY"); + createSubCharacteristic(characteristicsContext, "TESTABILITY" + COMPLIANCE_KEY_SUFFIX, "Testability " + COMPLIANCE_NAME, "TESTABILITY"); + } } /** @@ -146,17 +151,12 @@ public class AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigration private Integer order; private Integer parentId; - public Characteristic setId(Integer id) { - this.id = id; - return this; - } - public Integer getId() { return id; } - public Characteristic setKey(String key) { - this.key = key; + public Characteristic setId(Integer id) { + this.id = id; return this; } @@ -164,8 +164,8 @@ public class AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigration return key; } - public Characteristic setName(String name) { - this.name = name; + public Characteristic setKey(String key) { + this.key = key; return this; } @@ -173,8 +173,8 @@ public class AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigration return name; } - public Characteristic setOrder(@Nullable Integer order) { - this.order = order; + public Characteristic setName(String name) { + this.name = name; return this; } @@ -183,8 +183,8 @@ public class AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigration return order; } - public Characteristic setParentId(@Nullable Integer parentId) { - this.parentId = parentId; + public Characteristic setOrder(@Nullable Integer order) { + this.order = order; return this; } @@ -192,11 +192,16 @@ public class AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigration public Integer getParentId() { return parentId; } + + public Characteristic setParentId(@Nullable Integer parentId) { + this.parentId = parentId; + return this; + } } private static class CharacteristicsContext { - Context context; private final System2 system; + Context context; Date now; List characteristics; diff --git a/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v501/AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigrationTest.java b/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v501/AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigrationTest.java index d540cd19953..d55148fd56e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v501/AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigrationTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v501/AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigrationTest.java @@ -64,6 +64,13 @@ public class AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigrationT db.assertDbUnit(getClass(), "do_nothing_when_already_migrated.xml", "characteristics"); } + @Test + public void do_nothing_when_no_characteristics() throws Exception { + db.prepareDbUnit(getClass(), "empty.xml"); + migration.execute(); + assertThat(db.count("select count(id) from characteristics")).isEqualTo(0); + } + @Test public void insert_usability_at_the_top_if_security_does_exists() throws Exception { db.prepareDbUnit(getClass(), "insert_usability_at_the_top_if_security_does_exists.xml"); diff --git a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v501/AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigrationTest/empty.xml b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v501/AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigrationTest/empty.xml new file mode 100644 index 00000000000..871dedcb5e9 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v501/AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigrationTest/empty.xml @@ -0,0 +1,3 @@ + + + -- 2.39.5