From 3e934474787351585471e30b512c61f0ccc3e223 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 6 Mar 2014 15:56:33 +0100 Subject: [PATCH] SONAR-926 improve performance of db migration 490_migrate_package_keys --- .../db/migrations/DatabaseMigrations.java | 4 +- .../db/migrations/debt43/package-info.java | 24 +++++ .../packageKeys42/PackageKeysMigration.java | 90 +++++++++++++++++++ .../packageKeys42/package-info.java | 24 +++++ ...sources.rb => 490_migrate_package_keys.rb} | 18 +--- .../debt43/DebtMigrationExecutorTest.java | 11 +-- .../PackageKeysMigrationTest.java | 42 +++++++++ .../convert_package_keys-result.xml | 23 +++++ .../convert_package_keys.xml | 23 +++++ .../PackageKeysMigrationTest/schema.sql | 18 ++++ 10 files changed, 250 insertions(+), 27 deletions(-) create mode 100644 sonar-server/src/main/java/org/sonar/server/db/migrations/debt43/package-info.java create mode 100644 sonar-server/src/main/java/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigration.java create mode 100644 sonar-server/src/main/java/org/sonar/server/db/migrations/packageKeys42/package-info.java rename sonar-server/src/main/webapp/WEB-INF/db/migrate/{490_migrate_package_resources.rb => 490_migrate_package_keys.rb} (65%) create mode 100644 sonar-server/src/test/java/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigrationTest.java create mode 100644 sonar-server/src/test/resources/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigrationTest/convert_package_keys-result.xml create mode 100644 sonar-server/src/test/resources/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigrationTest/convert_package_keys.xml create mode 100644 sonar-server/src/test/resources/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigrationTest/schema.sql diff --git a/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrations.java b/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrations.java index b8640b747c1..335f66611a4 100644 --- a/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrations.java +++ b/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrations.java @@ -24,6 +24,7 @@ import org.sonar.server.db.migrations.debt43.DevelopmentCostMeasuresMigration; import org.sonar.server.db.migrations.debt43.IssueChangelogMigration; import org.sonar.server.db.migrations.debt43.IssueMigration; import org.sonar.server.db.migrations.debt43.TechnicalDebtMeasuresMigration; +import org.sonar.server.db.migrations.packageKeys42.PackageKeysMigration; import org.sonar.server.db.migrations.violation36.ViolationMigration; import java.util.List; @@ -35,7 +36,8 @@ public interface DatabaseMigrations { IssueMigration.class, IssueChangelogMigration.class, TechnicalDebtMeasuresMigration.class, - DevelopmentCostMeasuresMigration.class + DevelopmentCostMeasuresMigration.class, + PackageKeysMigration.class ); } diff --git a/sonar-server/src/main/java/org/sonar/server/db/migrations/debt43/package-info.java b/sonar-server/src/main/java/org/sonar/server/db/migrations/debt43/package-info.java new file mode 100644 index 00000000000..3d4254ff46c --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/db/migrations/debt43/package-info.java @@ -0,0 +1,24 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.db.migrations.debt43; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-server/src/main/java/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigration.java b/sonar-server/src/main/java/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigration.java new file mode 100644 index 00000000000..f0c57e4f73e --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigration.java @@ -0,0 +1,90 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.db.migrations.packageKeys42; + +import org.apache.commons.lang.StringUtils; +import org.sonar.core.persistence.Database; +import org.sonar.server.db.migrations.DatabaseMigration; +import org.sonar.server.db.migrations.MassUpdater; +import org.sonar.server.db.migrations.SqlUtil; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +/** + * Used in Rails migration 490 + * + * @since 4.2 + */ +public class PackageKeysMigration implements DatabaseMigration { + + private final Database db; + + public PackageKeysMigration(Database database) { + this.db = database; + } + + @Override + public void execute() { + new MassUpdater(db).execute( + new MassUpdater.InputLoader() { + @Override + public String selectSql() { + return "SELECT id, kee FROM projects WHERE qualifier='PAC'"; + } + + @Override + public Row load(ResultSet rs) throws SQLException { + Row row = new Row(); + row.id = SqlUtil.getLong(rs, 1); + row.key = rs.getString(2); + return row; + } + }, + new MassUpdater.InputConverter() { + @Override + public String updateSql() { + return "UPDATE projects SET qualifier='DIR', kee=? WHERE id=?"; + } + + @Override + public void convert(Row row, PreparedStatement updateStatement) throws SQLException { + updateStatement.setString(1, convertKey(row.key)); + updateStatement.setLong(2, row.id); + } + } + ); + } + + String convertKey(String packageKey) { + String prefix = StringUtils.substringBeforeLast(packageKey, ":") + ":"; + String key = StringUtils.substringAfterLast(packageKey, ":"); + if (key.equals("[default]")) { + return prefix + "[root]"; + } + return prefix + StringUtils.replace(key, ".", "/"); + } + + private static class Row { + private Long id; + private String key; + } +} diff --git a/sonar-server/src/main/java/org/sonar/server/db/migrations/packageKeys42/package-info.java b/sonar-server/src/main/java/org/sonar/server/db/migrations/packageKeys42/package-info.java new file mode 100644 index 00000000000..b0d4f921e62 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/db/migrations/packageKeys42/package-info.java @@ -0,0 +1,24 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.db.migrations.packageKeys42; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/490_migrate_package_resources.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/490_migrate_package_keys.rb similarity index 65% rename from sonar-server/src/main/webapp/WEB-INF/db/migrate/490_migrate_package_resources.rb rename to sonar-server/src/main/webapp/WEB-INF/db/migrate/490_migrate_package_keys.rb index 9acb81749bf..edbbcb64c8d 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/490_migrate_package_resources.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/490_migrate_package_keys.rb @@ -22,24 +22,10 @@ # SonarQube 4.2 # SONAR-926 # -class MigratePackageResources < ActiveRecord::Migration - - class Project < ActiveRecord::Base - end +class MigratePackageKeys < ActiveRecord::Migration def self.up - packages = Project.find(:all, :conditions => {:qualifier => 'PAC'}) - packages.each do |package| - key = package.kee.split(":").last - prefix = package.kee.chomp(key) - if key == '[default]' - package.kee = prefix + '[root]' - else - package.kee = prefix + key.gsub('.', '/') - end - package.qualifier = 'DIR' - package.save - end + Java::OrgSonarServerUi::JRubyFacade.getInstance().databaseMigrator().executeMigration('org.sonar.server.db.migrations.packageKeys42.PackageKeysMigration') end end diff --git a/sonar-server/src/test/java/org/sonar/server/db/migrations/debt43/DebtMigrationExecutorTest.java b/sonar-server/src/test/java/org/sonar/server/db/migrations/debt43/DebtMigrationExecutorTest.java index bea16d44a86..4fbdda9b256 100644 --- a/sonar-server/src/test/java/org/sonar/server/db/migrations/debt43/DebtMigrationExecutorTest.java +++ b/sonar-server/src/test/java/org/sonar/server/db/migrations/debt43/DebtMigrationExecutorTest.java @@ -20,10 +20,8 @@ package org.sonar.server.db.migrations.debt43; -import org.junit.Before; import org.junit.Test; import org.sonar.api.config.Settings; -import org.sonar.server.db.migrations.debt43.WorkDurationConvertor; import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.Fail.fail; @@ -31,19 +29,12 @@ import static org.fest.assertions.Fail.fail; public class DebtMigrationExecutorTest { static final int HOURS_IN_DAY = 8; - static final Long ONE_MINUTE = 1L; static final Long ONE_HOUR_IN_MINUTES = ONE_MINUTE * 60; static final Long ONE_DAY_IN_MINUTES = ONE_HOUR_IN_MINUTES * HOURS_IN_DAY; Settings settings = new Settings(); - - WorkDurationConvertor convertor; - - @Before - public void setUp() throws Exception { - convertor = new WorkDurationConvertor(settings); - } + WorkDurationConvertor convertor = new WorkDurationConvertor(settings); @Test public void convert_from_long() throws Exception { diff --git a/sonar-server/src/test/java/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigrationTest.java b/sonar-server/src/test/java/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigrationTest.java new file mode 100644 index 00000000000..d09bb1ec235 --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigrationTest.java @@ -0,0 +1,42 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.db.migrations.packageKeys42; + +import org.junit.ClassRule; +import org.junit.Test; +import org.sonar.core.persistence.TestDatabase; +import org.sonar.server.db.migrations.DatabaseMigration; + +public class PackageKeysMigrationTest { + + @ClassRule + public static TestDatabase db = new TestDatabase().schema(PackageKeysMigrationTest.class, "schema.sql"); + + DatabaseMigration migration = new PackageKeysMigration(db.database()); + + @Test + public void execute() throws Exception { + db.prepareDbUnit(getClass(), "convert_package_keys.xml"); + + migration.execute(); + + db.assertDbUnit(getClass(), "convert_package_keys-result.xml", "projects"); + } +} diff --git a/sonar-server/src/test/resources/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigrationTest/convert_package_keys-result.xml b/sonar-server/src/test/resources/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigrationTest/convert_package_keys-result.xml new file mode 100644 index 00000000000..142037441e8 --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigrationTest/convert_package_keys-result.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + diff --git a/sonar-server/src/test/resources/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigrationTest/convert_package_keys.xml b/sonar-server/src/test/resources/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigrationTest/convert_package_keys.xml new file mode 100644 index 00000000000..e750919484a --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigrationTest/convert_package_keys.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + diff --git a/sonar-server/src/test/resources/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigrationTest/schema.sql b/sonar-server/src/test/resources/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigrationTest/schema.sql new file mode 100644 index 00000000000..6860012ede7 --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigrationTest/schema.sql @@ -0,0 +1,18 @@ +-- 4.3 +CREATE TABLE "PROJECTS" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "NAME" VARCHAR(256), + "DESCRIPTION" VARCHAR(2000), + "ENABLED" BOOLEAN NOT NULL DEFAULT TRUE, + "SCOPE" VARCHAR(3), + "QUALIFIER" VARCHAR(10), + "KEE" VARCHAR(400), + "DEPRECATED_KEE" VARCHAR(400), + "PATH" VARCHAR(2000), + "ROOT_ID" INTEGER, + "LANGUAGE" VARCHAR(20), + "COPY_RESOURCE_ID" INTEGER, + "LONG_NAME" VARCHAR(256), + "PERSON_ID" INTEGER, + "CREATED_AT" TIMESTAMP +); -- 2.39.5