Browse Source

SONAR-10089 Add a migration removing loaded_templates for quality gates

tags/7.0-RC1
Eric Hartmann 6 years ago
parent
commit
71f554e821

+ 1
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70.java View File

@@ -30,6 +30,7 @@ public class DbVersion70 implements DbVersion {
.add(1900, "Add QUALITY_GATES.IS_BUILT_IN", AddIsBuiltInToQualityGates.class)
.add(1901, "Populate QUALITY_GATES.IS_BUILT_IN", PopulateQualityGatesIsBuiltIn.class)
.add(1902, "Make QUALITY_GATES.IS_BUILT_IN not null", MakeQualityGatesIsBuiltInNotNullable.class)
.add(1903, "Remove quality gates loaded templates", RemoveQualityGateLoadedTemplates.class)
;
}


+ 47
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/RemoveQualityGateLoadedTemplates.java View File

@@ -0,0 +1,47 @@
/*
* 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.step.DataChange;
import org.sonar.server.platform.db.migration.step.MassUpdate;

public class RemoveQualityGateLoadedTemplates extends DataChange {

public RemoveQualityGateLoadedTemplates(Database db) {
super(db);
}

@Override
protected void execute(Context context) throws SQLException {
MassUpdate massUpdate = context.prepareMassUpdate();
massUpdate.select("SELECT id " +
"FROM loaded_templates " +
"WHERE template_type = ?")
.setString(1, "QUALITY_GATE");
massUpdate.update("DELETE from loaded_templates WHERE id=?");
massUpdate.rowPluralName("delete loaded templates for quality gate");
massUpdate.execute((row, update) -> {
update.setLong(1, row.getLong(1));
return true;
});
}
}

+ 2
- 2
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70Test.java View File

@@ -29,13 +29,13 @@ public class DbVersion70Test {
private DbVersion70 underTest = new DbVersion70();

@Test
public void migrationNumber_starts_at_1830() {
public void migrationNumber_starts_at_1900() {
verifyMinimumMigrationNumber(underTest, 1900);
}

@Test
public void verify_migration_count() {
verifyMigrationCount(underTest, 3);
verifyMigrationCount(underTest, 4);
}

}

+ 92
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/RemoveQualityGateLoadedTemplatesTest.java View File

@@ -0,0 +1,92 @@
/*
* 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 java.util.stream.IntStream;
import org.apache.commons.lang.RandomStringUtils;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.db.CoreDbTester;

import static org.assertj.core.api.Assertions.assertThat;

public class RemoveQualityGateLoadedTemplatesTest {
public static final String QUALITY_GATE_TYPE = "QUALITY_GATE";
@Rule
public CoreDbTester db = CoreDbTester.createForSchema(RemoveQualityGateLoadedTemplatesTest.class, "loaded_templates.sql");

private RemoveQualityGateLoadedTemplates underTest = new RemoveQualityGateLoadedTemplates(db.database());

@Test
public void has_no_effect_if_table_is_empty() throws SQLException {
underTest.execute();

assertThat(db.countRowsOfTable("loaded_templates")).isEqualTo(0);
}

@Test
public void migration_should_remove_all_quality_gate_loaded_templates() throws SQLException {
db.executeInsert("loaded_templates", "kee", "KEE_1", "template_type", QUALITY_GATE_TYPE);
db.executeInsert("loaded_templates", "kee", "KEE_2", "template_type", QUALITY_GATE_TYPE);
db.executeInsert("loaded_templates", "kee", "KEE_3", "template_type", QUALITY_GATE_TYPE);
db.executeInsert("loaded_templates", "kee", "KEE_4", "template_type", QUALITY_GATE_TYPE);
db.executeInsert("loaded_templates", "kee", "KEE_5", "template_type", QUALITY_GATE_TYPE);

underTest.execute();

assertThat(db.countRowsOfTable("loaded_templates")).isEqualTo(0);
}

@Test
public void migration_should_NOT_remove_other_gate_loaded_templates() throws SQLException {
db.executeInsert("loaded_templates", "kee", "KEE_1", "template_type", "WHATEVER1");
db.executeInsert("loaded_templates", "kee", "KEE_2", "template_type", "WHATEVER2");
db.executeInsert("loaded_templates", "kee", "KEE_3", "template_type", "WHATEVER3");
db.executeInsert("loaded_templates", "kee", "KEE_4", "template_type", "WHATEVER4");
db.executeInsert("loaded_templates", "kee", "KEE_5", "template_type", "WHATEVER5");

underTest.execute();

assertThat(db.countSql("SELECT count(*) FROM loaded_templates WHERE template_type = '" + QUALITY_GATE_TYPE + "'")).isEqualTo(0);
assertThat(db.countRowsOfTable("loaded_templates")).isEqualTo(5);
}

@Test
public void migration_is_reentrant() throws SQLException {
db.executeInsert("loaded_templates", "kee", "KEE_1", "template_type", "WHATEVER1");
db.executeInsert("loaded_templates", "kee", "KEE_2", "template_type", "WHATEVER2");
db.executeInsert("loaded_templates", "kee", "KEE_3", "template_type", "WHATEVER3");
db.executeInsert("loaded_templates", "kee", "KEE_4", "template_type", "WHATEVER4");
db.executeInsert("loaded_templates", "kee", "KEE_5", "template_type", "WHATEVER5");

db.executeInsert("loaded_templates", "kee", "KEE_1", "template_type", QUALITY_GATE_TYPE);
db.executeInsert("loaded_templates", "kee", "KEE_2", "template_type", QUALITY_GATE_TYPE);
db.executeInsert("loaded_templates", "kee", "KEE_3", "template_type", QUALITY_GATE_TYPE);
db.executeInsert("loaded_templates", "kee", "KEE_4", "template_type", QUALITY_GATE_TYPE);
db.executeInsert("loaded_templates", "kee", "KEE_5", "template_type", QUALITY_GATE_TYPE);

underTest.execute();
underTest.execute();

assertThat(db.countSql("SELECT count(*) FROM loaded_templates WHERE template_type = '" + QUALITY_GATE_TYPE + "'")).isEqualTo(0);
assertThat(db.countRowsOfTable("loaded_templates")).isEqualTo(5);
}
}

+ 6
- 0
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/RemoveQualityGateLoadedTemplatesTest/loaded_templates.sql View File

@@ -0,0 +1,6 @@
CREATE TABLE "LOADED_TEMPLATES" (
"ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
"KEE" VARCHAR(200),
"TEMPLATE_TYPE" VARCHAR(64) NOT NULL
);
CREATE INDEX "IX_LOADED_TEMPLATES_TYPE" ON "LOADED_TEMPLATES" ("TEMPLATE_TYPE");

Loading…
Cancel
Save