Skip to content

Commit

Permalink
SONAR-8931 extend size of LOADED_TEMPLATES.TEMPLATE_TYPE
Browse files Browse the repository at this point in the history
  • Loading branch information
sns-seb committed Mar 23, 2017
1 parent 42982e2 commit 0856ba6
Show file tree
Hide file tree
Showing 19 changed files with 679 additions and 34 deletions.
Expand Up @@ -543,6 +543,10 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1607');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1608'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1608');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1609'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1609');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1610'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1610');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1611');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1612');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1613');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1614');


INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, IS_ROOT, CREATED_AT, UPDATED_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', true, 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', false, '1418215735482', '1418215735482'); INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, IS_ROOT, CREATED_AT, UPDATED_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', true, 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', false, '1418215735482', '1418215735482');
ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2; ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2;
Expand Down
Expand Up @@ -406,8 +406,9 @@ CREATE UNIQUE INDEX "METRICS_UNIQUE_NAME" ON "METRICS" ("NAME");
CREATE TABLE "LOADED_TEMPLATES" ( CREATE TABLE "LOADED_TEMPLATES" (
"ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
"KEE" VARCHAR(200), "KEE" VARCHAR(200),
"TEMPLATE_TYPE" VARCHAR(15) "TEMPLATE_TYPE" VARCHAR(64) NOT NULL
); );
CREATE INDEX "IX_LOADED_TEMPLATES_TYPE" ON "LOADED_TEMPLATES" ("TEMPLATE_TYPE");




CREATE TABLE "AUTHORS" ( CREATE TABLE "AUTHORS" (
Expand Down
@@ -0,0 +1,46 @@
/*
* 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.v64;

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.CreateIndexBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

public class AddIndexLoadedTemplatesType extends DdlChange {
public AddIndexLoadedTemplatesType(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(
new CreateIndexBuilder(getDialect())
.setTable("loaded_templates")
.setName("ix_loaded_templates_type")
.addColumn(VarcharColumnDef.newVarcharColumnDefBuilder()
.setColumnName("template_type")
.setIsNullable(false)
.setLimit(64)
.build())
.build());
}
}
@@ -0,0 +1,37 @@
/*
* 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.v64;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.step.DataChange;

public class CleanLoadedTemplateOrphans extends DataChange {
public CleanLoadedTemplateOrphans(Database db) {
super(db);
}

@Override
protected void execute(Context context) throws SQLException {
context.prepareUpsert("delete from loaded_templates where template_type is null")
.execute()
.commit();
}
}
Expand Up @@ -41,6 +41,11 @@ public void addSteps(MigrationStepRegistry registry) {
.add(1608, "Populate ORGANIZATION_MEMBERS table", PopulateOrganizationMembersTable.class) .add(1608, "Populate ORGANIZATION_MEMBERS table", PopulateOrganizationMembersTable.class)


.add(1609, "Drop unique index on RULES_PROFILES.ORGANIZATION_UUID and KEE", DropUniqueIndexOnQualityProfileOrganizationUuidAndKey.class) .add(1609, "Drop unique index on RULES_PROFILES.ORGANIZATION_UUID and KEE", DropUniqueIndexOnQualityProfileOrganizationUuidAndKey.class)
.add(1610, "Make RULES_PROFILES.KEE unique", MakeQualityProfileKeyUnique.class); .add(1610, "Make RULES_PROFILES.KEE unique", MakeQualityProfileKeyUnique.class)

.add(1611, "Clean LOADED_TEMPLATES rows without type", CleanLoadedTemplateOrphans.class)
.add(1612, "Extend size of column LOADED_TEMPLATES.TEMPLATE_TYPE", ExtendLoadedTemplateTypeColumn.class)
.add(1613, "Add index LOADED_TEMPLATES_TYPE", AddIndexLoadedTemplatesType.class)
.add(1614, "Upgrade loaded template entries for quality profiles", UpgradeQualityTemplateLoadedTemplates.class);
} }
} }
@@ -0,0 +1,44 @@
/*
* 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.v64;

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;

public class ExtendLoadedTemplateTypeColumn extends DdlChange {
public ExtendLoadedTemplateTypeColumn(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(
new AlterColumnsBuilder(getDialect(), "loaded_templates")
.updateColumn(VarcharColumnDef.newVarcharColumnDefBuilder()
.setColumnName("template_type")
.setIsNullable(false)
.setLimit(64)
.build())
.build());
}
}
@@ -0,0 +1,69 @@
/*
* 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.v64;

import java.security.MessageDigest;
import java.sql.SQLException;
import org.apache.commons.codec.digest.DigestUtils;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.step.DataChange;
import org.sonar.server.platform.db.migration.step.MassUpdate;
import org.sonar.server.platform.db.migration.version.v63.DefaultOrganizationUuid;

import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.commons.codec.binary.Hex.encodeHexString;

public class UpgradeQualityTemplateLoadedTemplates extends DataChange {
private static final String QUALITY_PROFILE_TYPE = "QUALITY_PROFILE";

private final DefaultOrganizationUuid defaultOrganizationUuid;

public UpgradeQualityTemplateLoadedTemplates(Database db, DefaultOrganizationUuid defaultOrganizationUuid) {
super(db);
this.defaultOrganizationUuid = defaultOrganizationUuid;
}

@Override
protected void execute(Context context) throws SQLException {
String defaultOrganizationUuid = this.defaultOrganizationUuid.getAndCheck(context);

MassUpdate massUpdate = context.prepareMassUpdate();
massUpdate
.select("select id,kee from loaded_templates where template_type=?")
.setString(1, QUALITY_PROFILE_TYPE);
massUpdate.rowPluralName("loaded templates for quality profiles");
massUpdate.update("update loaded_templates set template_type=?,kee=? where id=?");
MessageDigest md5Digest = DigestUtils.getMd5Digest();
massUpdate.execute((row, update) -> {
int id = row.getInt(1);
String key = row.getString(2);

update.setString(1, computeLoadedTemplateType(key, md5Digest));
update.setString(2, defaultOrganizationUuid);
update.setInt(3, id);
return true;
});
}

private static String computeLoadedTemplateType(String currentKey, MessageDigest messageDigest) {
return format("%s.%s", QUALITY_PROFILE_TYPE, encodeHexString(messageDigest.digest(currentKey.getBytes(UTF_8))));
}
}
@@ -0,0 +1,54 @@
/*
* 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.v64;

import java.sql.SQLException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.db.CoreDbTester;

public class AddIndexLoadedTemplatesTypeTest {
private static final String TABLE_LOADED_TEMPLATES = "loaded_templates";

@Rule
public CoreDbTester db = CoreDbTester.createForSchema(AddIndexLoadedTemplatesTypeTest.class, "loaded_templates_without_index.sql");
@Rule
public ExpectedException expectedException = ExpectedException.none();

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

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

db.assertIndex(TABLE_LOADED_TEMPLATES, "ix_loaded_templates_type", "template_type");
}

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

expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Fail to execute");

underTest.execute();
}
}
@@ -0,0 +1,66 @@
/*
* 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.v64;

import java.sql.SQLException;
import javax.annotation.Nullable;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.db.CoreDbTester;

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

public class CleanLoadedTemplateOrphansTest {

private static final String TABLE_LOADED_TEMPLATES = "loaded_templates";

@Rule
public CoreDbTester db = CoreDbTester.createForSchema(CleanLoadedTemplateOrphansTest.class, "loaded-templates.sql");
@Rule
public ExpectedException expectedException = ExpectedException.none();

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

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

@Test
public void execute_deletes_rows_with_null_in_template_type_column() throws SQLException {
insertLoadedTemplate(null, "value1");
insertLoadedTemplate("", "value2");
insertLoadedTemplate("non_empty", "value3");

underTest.execute();

assertThat(db.select("select kee as \"value\" from loaded_templates"))
.extracting(s -> s.get("value"))
.containsOnly("value2", "value3");
}

private void insertLoadedTemplate(@Nullable String type, String key) {
db.executeInsert(
TABLE_LOADED_TEMPLATES,
"TEMPLATE_TYPE", type,
"KEE", key);
}
}
Expand Up @@ -35,7 +35,7 @@ public void migrationNumber_starts_at_1600() {


@Test @Test
public void verify_migration_count() { public void verify_migration_count() {
verifyMigrationCount(underTest, 11); verifyMigrationCount(underTest, 15);
} }


} }

0 comments on commit 0856ba6

Please sign in to comment.