]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-926 improve performance of db migration 490_migrate_package_keys
authorSimon Brandhof <simon.brandhof@gmail.com>
Thu, 6 Mar 2014 14:56:33 +0000 (15:56 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Thu, 6 Mar 2014 15:20:27 +0000 (16:20 +0100)
sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrations.java
sonar-server/src/main/java/org/sonar/server/db/migrations/debt43/package-info.java [new file with mode: 0644]
sonar-server/src/main/java/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigration.java [new file with mode: 0644]
sonar-server/src/main/java/org/sonar/server/db/migrations/packageKeys42/package-info.java [new file with mode: 0644]
sonar-server/src/main/webapp/WEB-INF/db/migrate/490_migrate_package_keys.rb [new file with mode: 0644]
sonar-server/src/main/webapp/WEB-INF/db/migrate/490_migrate_package_resources.rb [deleted file]
sonar-server/src/test/java/org/sonar/server/db/migrations/debt43/DebtMigrationExecutorTest.java
sonar-server/src/test/java/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigrationTest.java [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigrationTest/convert_package_keys-result.xml [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigrationTest/convert_package_keys.xml [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/db/migrations/packageKeys42/PackageKeysMigrationTest/schema.sql [new file with mode: 0644]

index b8640b747c1e9340032f80818d1fc1afb5e6a413..335f66611a44751ff1deff3fcaf83404f112926c 100644 (file)
@@ -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 (file)
index 0000000..3d4254f
--- /dev/null
@@ -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 (file)
index 0000000..f0c57e4
--- /dev/null
@@ -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<Row>() {
+        @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<Row>() {
+        @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 (file)
index 0000000..b0d4f92
--- /dev/null
@@ -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_keys.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/490_migrate_package_keys.rb
new file mode 100644 (file)
index 0000000..edbbcb6
--- /dev/null
@@ -0,0 +1,31 @@
+#
+# 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.
+#
+
+#
+# SonarQube 4.2
+# SONAR-926
+#
+class MigratePackageKeys < ActiveRecord::Migration
+
+  def self.up
+    Java::OrgSonarServerUi::JRubyFacade.getInstance().databaseMigrator().executeMigration('org.sonar.server.db.migrations.packageKeys42.PackageKeysMigration')
+  end
+
+end
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_resources.rb
deleted file mode 100644 (file)
index 9acb817..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-#
-# 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.
-#
-
-#
-# SonarQube 4.2
-# SONAR-926
-#
-class MigratePackageResources < ActiveRecord::Migration
-
-  class Project < ActiveRecord::Base
-  end
-
-  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
-  end
-
-end
index bea16d44a86b4f4f28c3eaa5df0e04b489e0ec00..4fbdda9b256e8745115842b4c23bb52c16814eb4 100644 (file)
 
 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 (file)
index 0000000..d09bb1e
--- /dev/null
@@ -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 (file)
index 0000000..1420374
--- /dev/null
@@ -0,0 +1,23 @@
+<dataset>
+
+  <!-- do not touch this directory -->
+  <projects id="1" long_name="[null]" scope="DIR" qualifier="DIR" kee="project1:org/foo/Bar"
+            name="org/foo/bar" root_id="[null]"
+            description="[null]"
+            enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]"
+            created_at="[null]" deprecated_kee="[null]" path="[null]" />
+
+
+  <!-- packages to be changed -->
+  <projects id="2" long_name="[null]" scope="DIR" qualifier="DIR" kee="project2:org/foo/Bar"
+            name="org.foo.Bar" root_id="[null]"
+            description="[null]"
+            enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]"
+            created_at="[null]" deprecated_kee="[null]" path="[null]"/>
+
+  <projects id="3" long_name="[null]" scope="DIR" qualifier="DIR" kee="project2:branch2:[root]"
+            name="[default]" root_id="[null]"
+            description="[null]"
+            enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]"
+            created_at="[null]" deprecated_kee="[null]" path="[null]"/>
+</dataset>
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 (file)
index 0000000..e750919
--- /dev/null
@@ -0,0 +1,23 @@
+<dataset>
+
+  <!-- do not touch this directory -->
+  <projects id="1" long_name="[null]" scope="DIR" qualifier="DIR" kee="project1:org/foo/Bar"
+            name="org/foo/bar" root_id="[null]"
+            description="[null]"
+            enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]"
+            created_at="[null]" deprecated_kee="[null]" path="[null]" />
+
+
+  <!-- packages to be changed -->
+  <projects id="2" long_name="[null]" scope="DIR" qualifier="PAC" kee="project2:org.foo.Bar"
+            name="org.foo.Bar" root_id="[null]"
+            description="[null]"
+            enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]"
+            created_at="[null]" deprecated_kee="[null]" path="[null]"/>
+
+  <projects id="3" long_name="[null]" scope="DIR" qualifier="PAC" kee="project2:branch2:[default]"
+            name="[default]" root_id="[null]"
+            description="[null]"
+            enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]"
+            created_at="[null]" deprecated_kee="[null]" path="[null]"/>
+</dataset>
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 (file)
index 0000000..6860012
--- /dev/null
@@ -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
+);