);
assertThat(picoContainer.getParent().getParent().getComponentAdapters()).hasSize(
CONTAINER_ITSELF
+ + 14 // MigrationConfigurationModule
+ 17 // level 2
- + 13 // MigrationConfigurationModule
);
assertThat(picoContainer.getParent().getParent().getParent().getComponentAdapters()).hasSize(
COMPONENTS_IN_LEVEL_1_AT_CONSTRUCTION
CREATE TABLE "QUALITY_GATES" (
"ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
"NAME" VARCHAR(100) NOT NULL,
+ "IS_BUILT_IN" BOOLEAN NULL,
"CREATED_AT" TIMESTAMP,
"UPDATED_AT" TIMESTAMP,
);
public class QualityGateDto {
private Long id;
-
private String name;
-
+ private boolean isBuiltIn;
private Date createdAt;
-
private Date updatedAt;
public Long getId() {
return this;
}
+ public boolean isBuiltIn() {
+ return isBuiltIn;
+ }
+
+ public QualityGateDto setBuiltIn(boolean builtIn) {
+ isBuiltIn = builtIn;
+ return this;
+ }
+
public Date getCreatedAt() {
return createdAt;
}
<mapper namespace="org.sonar.db.qualitygate.QualityGateMapper">
<insert id="insert" parameterType="QualityGate" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
- insert into quality_gates (name, created_at, updated_at)
- values (#{name}, #{createdAt}, #{updatedAt})
+ insert into quality_gates (name, is_built_in, created_at, updated_at)
+ values (#{name}, #{isBuiltIn}, #{createdAt}, #{updatedAt})
</insert>
<sql id="gateColumns">
- id, name, created_at as createdAt, updated_at as updatedAt
+ id, name, is_built_in as isBuiltIn, created_at as createdAt, updated_at as updatedAt
</sql>
<select id="selectAll" resultType="QualityGate">
public class QualityGateDaoTest {
@Rule
- public DbTester dbTester = DbTester.create(System2.INSTANCE);
+ public DbTester db = DbTester.create(System2.INSTANCE);
- private DbSession dbSession = dbTester.getSession();
- private QualityGateDao underTest = dbTester.getDbClient().qualityGateDao();
+ private DbSession dbSession = db.getSession();
+ private QualityGateDao underTest = db.getDbClient().qualityGateDao();
@Test
public void testInsert() throws Exception {
- dbTester.prepareDbUnit(getClass(), "insert.xml");
+ db.prepareDbUnit(getClass(), "insert.xml");
QualityGateDto newQgate = new QualityGateDto().setName("My Quality Gate");
underTest.insert(dbSession, newQgate);
dbSession.commit();
- dbTester.assertDbUnitTable(getClass(), "insert-result.xml", "quality_gates", "name");
+ db.assertDbUnitTable(getClass(), "insert-result.xml", "quality_gates", "name");
assertThat(newQgate.getId()).isNotNull();
}
+ @Test
+ public void insert_built_in() {
+ underTest.insert(db.getSession(), new QualityGateDto().setName("test").setBuiltIn(true));
+
+ QualityGateDto reloaded = underTest.selectByName(db.getSession(), "test");
+
+ assertThat(reloaded.isBuiltIn()).isTrue();
+ }
+
@Test
public void testSelectAll() throws Exception {
- dbTester.prepareDbUnit(getClass(), "selectAll.xml");
+ db.prepareDbUnit(getClass(), "selectAll.xml");
Collection<QualityGateDto> allQualityGates = underTest.selectAll(dbSession);
@Test
public void testSelectByName() throws Exception {
- dbTester.prepareDbUnit(getClass(), "selectAll.xml");
+ db.prepareDbUnit(getClass(), "selectAll.xml");
assertThat(underTest.selectByName(dbSession, "Balanced").getName()).isEqualTo("Balanced");
assertThat(underTest.selectByName(dbSession, "Unknown")).isNull();
}
@Test
public void testSelectById() throws Exception {
- dbTester.prepareDbUnit(getClass(), "selectAll.xml");
+ db.prepareDbUnit(getClass(), "selectAll.xml");
assertThat(underTest.selectById(dbSession, 1L).getName()).isEqualTo("Very strict");
assertThat(underTest.selectById(dbSession, 42L)).isNull();
}
@Test
public void testDelete() throws Exception {
- dbTester.prepareDbUnit(getClass(), "selectAll.xml");
+ db.prepareDbUnit(getClass(), "selectAll.xml");
underTest.delete(new QualityGateDto().setId(1L), dbSession);
dbSession.commit();
- dbTester.assertDbUnitTable(getClass(), "delete-result.xml", "quality_gates", "id", "name");
+ db.assertDbUnitTable(getClass(), "delete-result.xml", "quality_gates", "id", "name");
}
@Test
public void testUpdate() throws Exception {
- dbTester.prepareDbUnit(getClass(), "selectAll.xml");
+ db.prepareDbUnit(getClass(), "selectAll.xml");
underTest.update(new QualityGateDto().setId(1L).setName("Not so strict"), dbSession);
dbSession.commit();
- dbTester.assertDbUnitTable(getClass(), "update-result.xml", "quality_gates", "id", "name");
+ db.assertDbUnitTable(getClass(), "update-result.xml", "quality_gates", "id", "name");
}
}
}
public QualityGateDto insertQualityGate(String name) {
- QualityGateDto updatedUser = dbClient.qualityGateDao().insert(dbSession, new QualityGateDto().setName(name));
+ QualityGateDto updatedUser = dbClient.qualityGateDao().insert(dbSession, new QualityGateDto().setName(name).setBuiltIn(false));
db.commit();
return updatedUser;
}
<dataset>
<quality_gates id="42"
- name="Golden"/>
+ name="Golden" is_built_in="[true]"/>
<quality_gates id="43"
- name="Ninth"/>
+ name="Ninth" is_built_in="[false]"/>
<projects organization_uuid="org1"
uuid="A"
<dataset>
- <quality_gates id="2" name="Balanced"/>
- <quality_gates id="3" name="Lenient"/>
+ <quality_gates id="2" name="Balanced" is_built_in="[false]"/>
+ <quality_gates id="3" name="Lenient" is_built_in="[false]"/>
</dataset>
<dataset>
- <quality_gates id="1" name="My Quality Gate"/>
+ <quality_gates id="1" name="My Quality Gate" is_built_in="[false]"/>
</dataset>
<dataset>
- <quality_gates id="1" name="Very strict"/>
- <quality_gates id="2" name="Balanced"/>
- <quality_gates id="3" name="Lenient"/>
+ <quality_gates id="1" name="Very strict" is_built_in="[true]"/>
+ <quality_gates id="2" name="Balanced" is_built_in="[false]"/>
+ <quality_gates id="3" name="Lenient" is_built_in="[false]"/>
</dataset>
<dataset>
- <quality_gates id="1" name="Not so strict"/>
- <quality_gates id="2" name="Balanced"/>
- <quality_gates id="3" name="Lenient"/>
+ <quality_gates id="1" name="Not so strict" is_built_in="[false]"/>
+ <quality_gates id="2" name="Balanced" is_built_in="[false]"/>
+ <quality_gates id="3" name="Lenient" is_built_in="[false]"/>
</dataset>
import org.sonar.server.platform.db.migration.version.v65.DbVersion65;
import org.sonar.server.platform.db.migration.version.v66.DbVersion66;
import org.sonar.server.platform.db.migration.version.v67.DbVersion67;
+import org.sonar.server.platform.db.migration.version.v70.DbVersion70;
public class MigrationConfigurationModule extends Module {
@Override
DbVersion65.class,
DbVersion66.class,
DbVersion67.class,
+ DbVersion70.class,
// migration steps
MigrationStepRegistryImpl.class,
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 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.v70;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+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.BooleanColumnDef.newBooleanColumnDefBuilder;
+
+public class AddIsBuiltInToQualityGates extends DdlChange {
+
+ public AddIsBuiltInToQualityGates(Database db) {
+ super(db);
+ }
+
+ @Override
+ public void execute(Context context) throws SQLException {
+ context.execute(new AddColumnsBuilder(getDialect(), "quality_gates")
+ .addColumn(newBooleanColumnDefBuilder()
+ .setColumnName("is_built_in")
+ .setIsNullable(true)
+ .build())
+ .build());
+ }
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 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.v70;
+
+import org.sonar.server.platform.db.migration.step.MigrationStepRegistry;
+import org.sonar.server.platform.db.migration.version.DbVersion;
+
+public class DbVersion70 implements DbVersion {
+
+ @Override
+ public void addSteps(MigrationStepRegistry registry) {
+ registry
+ .add(1900, "Add QUALITY_GATES.IS_BUILT_IN", AddIsBuiltInToQualityGates.class);
+ }
+
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 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.
+ */
+@ParametersAreNonnullByDefault
+package org.sonar.server.platform.db.migration.version.v70;
+
+import javax.annotation.ParametersAreNonnullByDefault;
+
assertThat(container.getPicoContainer().getComponentAdapters())
.hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER
// DbVersion classes
- + 10
+ + 11
// Others
+ 3);
}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 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.v70;
+
+import java.sql.SQLException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.db.CoreDbTester;
+
+import static java.sql.Types.BOOLEAN;
+
+public class AddIsBuiltInToQualityGatesTest {
+
+ @Rule
+ public final CoreDbTester dbTester = CoreDbTester.createForSchema(AddIsBuiltInToQualityGatesTest.class, "quality_gates_7_0.sql");
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ private AddIsBuiltInToQualityGates underTest = new AddIsBuiltInToQualityGates(dbTester.database());
+
+ @Test
+ public void column_is_added_to_table() throws SQLException {
+ underTest.execute();
+
+ dbTester.assertColumnDefinition("quality_gates", "is_built_in", BOOLEAN, null, true);
+ }
+
+ @Test
+ public void migration_is_not_reentrant() throws SQLException {
+ underTest.execute();
+
+ expectedException.expect(IllegalStateException.class);
+
+ underTest.execute();
+ }
+
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 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.v70;
+
+import org.junit.Test;
+
+import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMigrationCount;
+import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMinimumMigrationNumber;
+
+public class DbVersion70Test {
+
+ private DbVersion70 underTest = new DbVersion70();
+
+ @Test
+ public void migrationNumber_starts_at_1830() {
+ verifyMinimumMigrationNumber(underTest, 1900);
+ }
+
+ @Test
+ public void verify_migration_count() {
+ verifyMigrationCount(underTest, 1);
+ }
+
+}
--- /dev/null
+CREATE TABLE "QUALITY_GATES" (
+ "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+ "NAME" VARCHAR(100) NOT NULL,
+ "CREATED_AT" TIMESTAMP,
+ "UPDATED_AT" TIMESTAMP,
+);
+CREATE UNIQUE INDEX "UNIQ_QUALITY_GATES" ON "QUALITY_GATES" ("NAME");
private static final int LEAK_PERIOD = 1;
private final DbClient dbClient;
- private final QualityGateUpdater qualityGateUpdater;
private final QualityGateConditionsUpdater qualityGateConditionsUpdater;
private final LoadedTemplateDao loadedTemplateDao;
private final QualityGates qualityGates;
- public RegisterQualityGates(DbClient dbClient, QualityGateUpdater qualityGateUpdater, QualityGateConditionsUpdater qualityGateConditionsUpdater,
+ public RegisterQualityGates(DbClient dbClient, QualityGateConditionsUpdater qualityGateConditionsUpdater,
LoadedTemplateDao loadedTemplateDao, QualityGates qualityGates) {
this.dbClient = dbClient;
- this.qualityGateUpdater = qualityGateUpdater;
this.qualityGateConditionsUpdater = qualityGateConditionsUpdater;
this.loadedTemplateDao = loadedTemplateDao;
this.qualityGates = qualityGates;
private void createBuiltinQualityGate(DbSession dbSession) {
String ratingAValue = Integer.toString(RatingGrid.Rating.A.getIndex());
- QualityGateDto builtin = qualityGateUpdater.create(dbSession, BUILTIN_QUALITY_GATE);
+ QualityGateDto builtin = createQualityGate(dbSession, BUILTIN_QUALITY_GATE);
qualityGateConditionsUpdater.createCondition(dbSession, builtin.getId(),
NEW_SECURITY_RATING_KEY, OPERATOR_GREATER_THAN, null, ratingAValue, LEAK_PERIOD);
qualityGateConditionsUpdater.createCondition(dbSession, builtin.getId(),
qualityGates.setDefault(dbSession, builtin.getId());
}
+ private QualityGateDto createQualityGate(DbSession dbSession, String name){
+ QualityGateDto qualityGate = new QualityGateDto()
+ .setName(name)
+ .setBuiltIn(true);
+ dbClient.qualityGateDao().insert(dbSession, qualityGate);
+ return qualityGate;
+ }
+
private void registerBuiltinQualityGate(DbSession dbSession) {
loadedTemplateDao.insert(new LoadedTemplateDto(BUILTIN_QUALITY_GATE, LoadedTemplateDto.QUALITY_GATE_TYPE), dbSession);
}
QualityGates qualityGates = mock(QualityGates.class);
RegisterQualityGates task = new RegisterQualityGates(dbClient,
- new QualityGateUpdater(dbClient),
new QualityGateConditionsUpdater(dbClient),
dbClient.loadedTemplateDao(),
qualityGates);
assertThat(dbClient.loadedTemplateDao().countByTypeAndKey("QUALITY_GATE", "SonarQube way", dbSession)).isEqualTo(1);
QualityGateDto qualityGateDto = dbClient.qualityGateDao().selectByName(dbSession, "SonarQube way");
assertThat(qualityGateDto).isNotNull();
+ assertThat(qualityGateDto.isBuiltIn()).isTrue();
assertThat(dbClient.gateConditionDao().selectForQualityGate(dbSession, qualityGateDto.getId()))
.extracting(QualityGateConditionDto::getMetricId, QualityGateConditionDto::getOperator, QualityGateConditionDto::getWarningThreshold,
QualityGateConditionDto::getErrorThreshold, QualityGateConditionDto::getPeriod)