diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-07-17 07:15:54 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-07-17 07:15:54 +0200 |
commit | 6749ac98969babae70241cdbe4ba80d71d5380c7 (patch) | |
tree | 3ac3a9d845f103bf438d285df15ce7ff42a59a30 /server/sonar-server | |
parent | c553274a3bcd0811c425b6b71fbabe9c37f4b7f5 (diff) | |
download | sonarqube-6749ac98969babae70241cdbe4ba80d71d5380c7.tar.gz sonarqube-6749ac98969babae70241cdbe4ba80d71d5380c7.zip |
wip
Diffstat (limited to 'server/sonar-server')
10 files changed, 240 insertions, 454 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/SqlUtil.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/SqlUtil.java index 9b4050fe6cc..965b1a4a2fd 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/SqlUtil.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/SqlUtil.java @@ -50,18 +50,6 @@ public class SqlUtil { } @CheckForNull - public static Double getDouble(ResultSet rs, int columnIndex) throws SQLException { - double d = rs.getDouble(columnIndex); - return rs.wasNull() ? null : d; - } - - @CheckForNull - public static Integer getInt(ResultSet rs, int columnIndex) throws SQLException { - int i = rs.getInt(columnIndex); - return rs.wasNull() ? null : i; - } - - @CheckForNull public static Long getLong(ResultSet rs, String columnName) throws SQLException { long l = rs.getLong(columnName); return rs.wasNull() ? null : l; diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v42/CompleteIssueMessageMigration.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v42/CompleteIssueMessageMigration.java index d8dcd62b620..4cec3c611dc 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v42/CompleteIssueMessageMigration.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v42/CompleteIssueMessageMigration.java @@ -20,12 +20,11 @@ package org.sonar.server.db.migrations.v42; 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 org.sonar.server.db.migrations.BaseDataChange; +import org.sonar.server.db.migrations.MassUpdate; +import org.sonar.server.db.migrations.Select; +import org.sonar.server.db.migrations.SqlStatement; -import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.sql.SQLException; /** @@ -33,49 +32,27 @@ import java.sql.SQLException; * * @since 4.2 */ -public class CompleteIssueMessageMigration implements DatabaseMigration { - - private final Database db; +public class CompleteIssueMessageMigration extends BaseDataChange { public CompleteIssueMessageMigration(Database database) { - this.db = database; + super(database); } @Override - public void execute() { - new MassUpdater(db).execute( - new MassUpdater.InputLoader<Row>() { - @Override - public String selectSql() { - return "SELECT i.id, r.name FROM issues i INNER JOIN rules r ON r.id=i.rule_id WHERE i.message IS NULL"; - } - - @Override - public Row load(ResultSet rs) throws SQLException { - Row row = new Row(); - row.issueId = SqlUtil.getLong(rs, 1); - row.ruleName = rs.getString(2); - return row; - } - }, - new MassUpdater.InputConverter<Row>() { - @Override - public String updateSql() { - return "UPDATE issues SET message=? WHERE id=?"; - } - - @Override - public boolean convert(Row row, PreparedStatement updateStatement) throws SQLException { - updateStatement.setString(1, row.ruleName); - updateStatement.setLong(2, row.issueId); - return true; - } + public void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("SELECT i.id, r.name FROM issues i INNER JOIN rules r ON r.id=i.rule_id WHERE i.message IS NULL"); + massUpdate.update("UPDATE issues SET message=? WHERE id=?"); + massUpdate.execute(new MassUpdate.Handler() { + @Override + public boolean handle(Select.Row row, SqlStatement update) throws SQLException { + Long issueId = row.getLong(1); + String ruleName = row.getString(2); + + update.setString(1, ruleName); + update.setLong(2, issueId); + return true; } - ); - } - - private static class Row { - private Long issueId; - private String ruleName; + }); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v42/PackageKeysMigration.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v42/PackageKeysMigration.java index 7e353a29ce9..a8621136fe3 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v42/PackageKeysMigration.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v42/PackageKeysMigration.java @@ -21,12 +21,11 @@ package org.sonar.server.db.migrations.v42; 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 org.sonar.server.db.migrations.BaseDataChange; +import org.sonar.server.db.migrations.MassUpdate; +import org.sonar.server.db.migrations.Select; +import org.sonar.server.db.migrations.SqlStatement; -import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.sql.SQLException; /** @@ -34,45 +33,27 @@ import java.sql.SQLException; * * @since 4.2 */ -public class PackageKeysMigration implements DatabaseMigration { - - private final Database db; +public class PackageKeysMigration extends BaseDataChange { public PackageKeysMigration(Database database) { - this.db = database; + super(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 boolean convert(Row row, PreparedStatement updateStatement) throws SQLException { - updateStatement.setString(1, convertKey(row.key)); - updateStatement.setLong(2, row.id); - return true; - } + public void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("SELECT id, kee FROM projects WHERE qualifier='PAC'"); + massUpdate.update("UPDATE projects SET qualifier='DIR', kee=? WHERE id=?"); + massUpdate.execute(new MassUpdate.Handler() { + @Override + public boolean handle(Select.Row row, SqlStatement update) throws SQLException { + Long id = row.getLong(1); + String key = row.getString(2); + update.setString(1, convertKey(key)); + update.setLong(2, id); + return true; } - ); + }); } String convertKey(String packageKey) { @@ -83,9 +64,4 @@ public class PackageKeysMigration implements DatabaseMigration { } return prefix + StringUtils.replace(key, ".", "/"); } - - private static class Row { - private Long id; - private String key; - } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/ConvertIssueDebtToMinutesMigration.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/ConvertIssueDebtToMinutesMigration.java index 6bb5b0f12e3..9d867cf4978 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/ConvertIssueDebtToMinutesMigration.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/ConvertIssueDebtToMinutesMigration.java @@ -20,86 +20,60 @@ package org.sonar.server.db.migrations.v43; -import com.google.common.annotations.VisibleForTesting; import org.sonar.api.utils.System2; import org.sonar.core.persistence.Database; import org.sonar.core.properties.PropertiesDao; -import org.sonar.server.db.migrations.DatabaseMigration; -import org.sonar.server.db.migrations.MassUpdater; -import org.sonar.server.db.migrations.SqlUtil; +import org.sonar.server.db.migrations.BaseDataChange; +import org.sonar.server.db.migrations.MassUpdate; +import org.sonar.server.db.migrations.Select; +import org.sonar.server.db.migrations.SqlStatement; -import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Timestamp; +import java.util.Date; /** - * Used in the Active Record Migration 513 + * Used in the Active Record Migration 513. + * WARNING - this migration is not re-entrant. + * * @since 4.3 */ -public class ConvertIssueDebtToMinutesMigration implements DatabaseMigration { +public class ConvertIssueDebtToMinutesMigration extends BaseDataChange { private final WorkDurationConvertor workDurationConvertor; private final System2 system2; - private final Database db; - - public ConvertIssueDebtToMinutesMigration(Database database, PropertiesDao propertiesDao) { - this(database, propertiesDao, System2.INSTANCE); - } - @VisibleForTesting - ConvertIssueDebtToMinutesMigration(Database database, PropertiesDao propertiesDao, System2 system2) { - this.db = database; + public ConvertIssueDebtToMinutesMigration(Database database, PropertiesDao propertiesDao, System2 system2) { + super(database); this.workDurationConvertor = new WorkDurationConvertor(propertiesDao); this.system2 = system2; } @Override - public void execute() { + public void execute(Context context) throws SQLException { workDurationConvertor.init(); + final Date now = new Date(system2.now()); + MassUpdate massUpdate = context.prepareMassUpdate(); - new MassUpdater(db).execute( - new MassUpdater.InputLoader<Row>() { - @Override - public String selectSql() { - return "SELECT i.id, i.technical_debt FROM issues i"; - } + // See https://jira.codehaus.org/browse/SONAR-5394 + // The SQL request should not set the filter on technical_debt is not null. There's no index + // on this column, so filtering is done programmatically. + massUpdate.select("select id, technical_debt from issues"); - @Override - public Row load(ResultSet rs) throws SQLException { - Long debt = SqlUtil.getLong(rs, 2); - if (!rs.wasNull() && debt != null) { - // See https://jira.codehaus.org/browse/SONAR-5394 - // The SQL request should not set the filter on technical_debt is not null. There's no index - // on this column, so filtering is done programmatically. - Row row = new Row(); - row.id = SqlUtil.getLong(rs, 1); - row.debt = debt; - return row; - } - return null; - } - }, - new MassUpdater.InputConverter<Row>() { - @Override - public String updateSql() { - return "UPDATE issues SET technical_debt=?,updated_at=? WHERE id=?"; - } - - @Override - public boolean convert(Row row, PreparedStatement updateStatement) throws SQLException { - updateStatement.setLong(1, workDurationConvertor.createFromLong(row.debt)); - updateStatement.setTimestamp(2, new Timestamp(system2.now())); - updateStatement.setLong(3, row.id); + massUpdate.update("update issues set technical_debt=?, updated_at=? where id=?"); + massUpdate.execute(new MassUpdate.Handler() { + @Override + public boolean handle(Select.Row row, SqlStatement update) throws SQLException { + Long debt = row.getLong(2); + if (debt != null) { + Long id = row.getLong(1); + update.setLong(1, workDurationConvertor.createFromLong(debt)); + update.setDate(2, now); + update.setLong(3, id); return true; } + return false; } - ); - } - - private static class Row { - private Long id; - private Long debt; + }); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/DevelopmentCostMeasuresMigration.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/DevelopmentCostMeasuresMigration.java index 6f629ee260a..5f419da982f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/DevelopmentCostMeasuresMigration.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/DevelopmentCostMeasuresMigration.java @@ -22,75 +22,53 @@ package org.sonar.server.db.migrations.v43; import org.sonar.core.persistence.Database; import org.sonar.core.properties.PropertiesDao; -import org.sonar.server.db.migrations.DatabaseMigration; -import org.sonar.server.db.migrations.MassUpdater; -import org.sonar.server.db.migrations.SqlUtil; +import org.sonar.server.db.migrations.BaseDataChange; +import org.sonar.server.db.migrations.MassUpdate; +import org.sonar.server.db.migrations.Select; +import org.sonar.server.db.migrations.SqlStatement; import javax.annotation.CheckForNull; - -import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.sql.SQLException; /** * Used in the Active Record Migration 516 + * * @since 4.3 */ -public class DevelopmentCostMeasuresMigration implements DatabaseMigration { +public class DevelopmentCostMeasuresMigration extends BaseDataChange { private final WorkDurationConvertor workDurationConvertor; - private final Database db; public DevelopmentCostMeasuresMigration(Database database, PropertiesDao propertiesDao) { - this.db = database; + super(database); this.workDurationConvertor = new WorkDurationConvertor(propertiesDao); } @Override - public void execute() { + public void execute(Context context) throws SQLException { workDurationConvertor.init(); - new MassUpdater(db).execute( - new MassUpdater.InputLoader<Row>() { - @Override - public String selectSql() { - return "SELECT pm.id, pm.value " + - " FROM project_measures pm INNER JOIN metrics m on m.id=pm.metric_id " + - " WHERE m.name='development_cost' AND pm.value IS NOT NULL"; - } - - @Override - public Row load(ResultSet rs) throws SQLException { - Row row = new Row(); - row.id = SqlUtil.getLong(rs, 1); - row.value = SqlUtil.getDouble(rs, 2); - return row; - } - }, - new MassUpdater.InputConverter<Row>() { + Long metricId = context.prepareSelect("select id from metrics where name='development_cost'").get(Select.RowReader.LONG); + if (metricId != null) { + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("select id, value from project_measures where metric_id=? and value is not null").setLong(1, metricId); + massUpdate.update("update project_measures set value=NULL,text_value=? where id=?"); + massUpdate.execute(new MassUpdate.Handler() { @Override - public String updateSql() { - return "UPDATE project_measures SET value=NULL,text_value=? WHERE id=?"; - } + public boolean handle(Select.Row row, SqlStatement update) throws SQLException { + Long id = row.getLong(1); + Double value = row.getDouble(2); - @Override - public boolean convert(Row row, PreparedStatement updateStatement) throws SQLException { - updateStatement.setString(1, convertDebtForDays(row.value)); - updateStatement.setLong(2, row.id); + update.setString(1, convertDebtForDays(value)); + update.setLong(2, id); return true; } - } - ); + }); + } } @CheckForNull private String convertDebtForDays(Double data) { return Long.toString(workDurationConvertor.createFromDays(data)); } - - private static class Row { - private Long id; - private Double value; - } - } diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/IssueChangelogMigration.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/IssueChangelogMigration.java index ef8ae771dfa..8fd72fc4cf8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/IssueChangelogMigration.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/IssueChangelogMigration.java @@ -25,26 +25,25 @@ import com.google.common.base.Strings; import org.sonar.api.utils.System2; import org.sonar.core.persistence.Database; import org.sonar.core.properties.PropertiesDao; -import org.sonar.server.db.migrations.DatabaseMigration; -import org.sonar.server.db.migrations.MassUpdater; -import org.sonar.server.db.migrations.SqlUtil; +import org.sonar.server.db.migrations.BaseDataChange; +import org.sonar.server.db.migrations.MassUpdate; +import org.sonar.server.db.migrations.Select; +import org.sonar.server.db.migrations.SqlStatement; -import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Timestamp; +import java.util.Date; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Used in the Active Record Migration 514 + * * @since 4.3 */ -public class IssueChangelogMigration implements DatabaseMigration { +public class IssueChangelogMigration extends BaseDataChange { private final WorkDurationConvertor workDurationConvertor; private final System2 system2; - private final Database db; public IssueChangelogMigration(Database database, System2 system2, PropertiesDao propertiesDao) { this(database, system2, new WorkDurationConvertor(propertiesDao)); @@ -52,46 +51,31 @@ public class IssueChangelogMigration implements DatabaseMigration { @VisibleForTesting IssueChangelogMigration(Database database, System2 system2, WorkDurationConvertor convertor) { - this.db = database; + super(database); this.workDurationConvertor = convertor; this.system2 = system2; } @Override - public void execute() { + public void execute(Context context) throws SQLException { workDurationConvertor.init(); + final Date now = new Date(system2.now()); + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("SELECT ic.id, ic.change_data FROM issue_changes ic " + + "WHERE ic.change_type = 'diff' AND ic.change_data LIKE '%technicalDebt%'"); + massUpdate.update("UPDATE issue_changes SET change_data=?,updated_at=? WHERE id=?"); + massUpdate.execute(new MassUpdate.Handler() { + @Override + public boolean handle(Select.Row row, SqlStatement update) throws SQLException { + Long id = row.getLong(1); + String changeData = row.getString(2); - new MassUpdater(db).execute( - new MassUpdater.InputLoader<Row>() { - @Override - public String selectSql() { - return "SELECT ic.id, ic.change_data FROM issue_changes ic " + - " WHERE ic.change_type = 'diff' AND ic.change_data LIKE '%technicalDebt%'"; - } - - @Override - public Row load(ResultSet rs) throws SQLException { - Row row = new Row(); - row.id = SqlUtil.getLong(rs, 1); - row.changeData = rs.getString(2); - return row; - } - }, - new MassUpdater.InputConverter<Row>() { - @Override - public String updateSql() { - return "UPDATE issue_changes SET change_data=?,updated_at=? WHERE id=?"; - } - - @Override - public boolean convert(Row row, PreparedStatement updateStatement) throws SQLException { - updateStatement.setString(1, convertChangelog(row.changeData)); - updateStatement.setTimestamp(2, new Timestamp(system2.now())); - updateStatement.setLong(3, row.id); - return true; - } + update.setString(1, convertChangelog(changeData)); + update.setDate(2, now); + update.setLong(3, id); + return true; } - ); + }); } @VisibleForTesting diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/NotResolvedIssuesOnRemovedComponentsMigration.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/NotResolvedIssuesOnRemovedComponentsMigration.java index 8563f27f283..6c5c518c893 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/NotResolvedIssuesOnRemovedComponentsMigration.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/NotResolvedIssuesOnRemovedComponentsMigration.java @@ -23,68 +23,46 @@ package org.sonar.server.db.migrations.v43; import org.sonar.api.issue.Issue; import org.sonar.api.utils.System2; 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 org.sonar.server.db.migrations.BaseDataChange; +import org.sonar.server.db.migrations.MassUpdate; +import org.sonar.server.db.migrations.Select; +import org.sonar.server.db.migrations.SqlStatement; -import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Timestamp; +import java.util.Date; /** * Used in the Active Record Migration 525 * * @since 4.3 */ -public class NotResolvedIssuesOnRemovedComponentsMigration implements DatabaseMigration { +public class NotResolvedIssuesOnRemovedComponentsMigration extends BaseDataChange { private final System2 system2; - private final Database db; public NotResolvedIssuesOnRemovedComponentsMigration(Database database, System2 system2) { - this.db = database; + super(database); this.system2 = system2; } @Override - public void execute() { - new MassUpdater(db).execute( - new MassUpdater.InputLoader<Row>() { - @Override - public String selectSql() { - return "SELECT i.id FROM issues i " + - "INNER JOIN projects p on p.id=i.component_id " + - "WHERE p.enabled=${_false} AND i.resolution IS NULL "; - } - - @Override - public Row load(ResultSet rs) throws SQLException { - Row row = new Row(); - row.id = SqlUtil.getLong(rs, 1); - return row; - } - }, - new MassUpdater.InputConverter<Row>() { - @Override - public String updateSql() { - return "UPDATE issues SET status=?,resolution=?,updated_at=? WHERE id=?"; - } - - @Override - public boolean convert(Row row, PreparedStatement updateStatement) throws SQLException { - updateStatement.setString(1, Issue.STATUS_CLOSED); - updateStatement.setString(2, Issue.RESOLUTION_REMOVED); - updateStatement.setTimestamp(3, new Timestamp(system2.now())); - updateStatement.setLong(4, row.id); - return true; - } + public void execute(Context context) throws SQLException { + final Date now = new Date(system2.now()); + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("SELECT i.id FROM issues i " + + "INNER JOIN projects p on p.id=i.component_id " + + "WHERE p.enabled=? AND i.resolution IS NULL ").setBoolean(1, false); + massUpdate.update("UPDATE issues SET status=?,resolution=?,updated_at=? WHERE id=?"); + massUpdate.execute(new MassUpdate.Handler() { + @Override + public boolean handle(Select.Row row, SqlStatement update) throws SQLException { + Long id = row.getLong(1); + update.setString(1, Issue.STATUS_CLOSED); + update.setString(2, Issue.RESOLUTION_REMOVED); + update.setDate(3, now); + update.setLong(4, id); + return true; } - ); + }); } - - private static class Row { - private Long id; - } - } diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/RequirementMeasuresMigration.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/RequirementMeasuresMigration.java index adb2de1286d..02a97f6a8f8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/RequirementMeasuresMigration.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/RequirementMeasuresMigration.java @@ -21,12 +21,11 @@ package org.sonar.server.db.migrations.v43; 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 org.sonar.server.db.migrations.BaseDataChange; +import org.sonar.server.db.migrations.MassUpdate; +import org.sonar.server.db.migrations.Select; +import org.sonar.server.db.migrations.SqlStatement; -import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.sql.SQLException; /** @@ -34,52 +33,29 @@ import java.sql.SQLException; * * @since 4.3 */ -public class RequirementMeasuresMigration implements DatabaseMigration { - - private final Database db; +public class RequirementMeasuresMigration extends BaseDataChange { public RequirementMeasuresMigration(Database database) { - this.db = database; + super(database); } @Override - public void execute() { - new MassUpdater(db).execute( - new MassUpdater.InputLoader<Row>() { - @Override - public String selectSql() { - return "SELECT project_measures.id,characteristics.rule_id FROM project_measures " + - "INNER JOIN characteristics ON characteristics.id = project_measures.characteristic_id " + - "WHERE characteristics.rule_id IS NOT NULL"; - } - - @Override - public Row load(ResultSet rs) throws SQLException { - Row row = new Row(); - row.id = SqlUtil.getLong(rs, 1); - row.ruleId = SqlUtil.getInt(rs, 2); - return row; - } - }, - new MassUpdater.InputConverter<Row>() { - @Override - public String updateSql() { - return "UPDATE project_measures SET characteristic_id=null,rule_id=? WHERE id=?"; - } - - @Override - public boolean convert(Row row, PreparedStatement updateStatement) throws SQLException { - updateStatement.setInt(1, row.ruleId); - updateStatement.setLong(2, row.id); - return true; - } + public void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("SELECT project_measures.id,characteristics.rule_id FROM project_measures " + + "INNER JOIN characteristics ON characteristics.id = project_measures.characteristic_id " + + "WHERE characteristics.rule_id IS NOT NULL"); + massUpdate.update("UPDATE project_measures SET characteristic_id=null,rule_id=? WHERE id=?"); + massUpdate.execute(new MassUpdate.Handler() { + @Override + public boolean handle(Select.Row row, SqlStatement update) throws SQLException { + Long id = row.getLong(1); + Long ruleId = row.getLong(2); + + update.setLong(1, ruleId); + update.setLong(2, id); + return true; } - ); + }); } - - private static class Row { - private Long id; - private Integer ruleId; - } - } diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/TechnicalDebtMeasuresMigration.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/TechnicalDebtMeasuresMigration.java index 9dd4ab82a4b..91317aa94d3 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/TechnicalDebtMeasuresMigration.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/TechnicalDebtMeasuresMigration.java @@ -20,110 +20,85 @@ package org.sonar.server.db.migrations.v43; +import org.apache.commons.lang.StringUtils; import org.sonar.core.persistence.Database; import org.sonar.core.properties.PropertiesDao; -import org.sonar.server.db.migrations.DatabaseMigration; -import org.sonar.server.db.migrations.MassUpdater; -import org.sonar.server.db.migrations.SqlUtil; +import org.sonar.server.db.migrations.BaseDataChange; +import org.sonar.server.db.migrations.MassUpdate; +import org.sonar.server.db.migrations.Select; +import org.sonar.server.db.migrations.SqlStatement; -import java.sql.PreparedStatement; -import java.sql.ResultSet; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import java.sql.SQLException; -import java.sql.Types; +import java.util.List; /** * Used in the Active Record Migration 515 + * * @since 4.3 */ -public class TechnicalDebtMeasuresMigration implements DatabaseMigration { - - private static final String SELECT_SQL = "SELECT pm.id, pm.value " + - ", pm.variation_value_1 , pm.variation_value_2, pm.variation_value_3 " + - ", pm.variation_value_4 , pm.variation_value_5 " + - " FROM project_measures pm INNER JOIN metrics m on m.id=pm.metric_id " + - " WHERE (m.name='sqale_index' or m.name='new_technical_debt' " + - // SQALE measures - " or m.name='sqale_effort_to_grade_a' or m.name='sqale_effort_to_grade_b' or m.name='sqale_effort_to_grade_c' or m.name='sqale_effort_to_grade_d' " + - " or m.name='blocker_remediation_cost' or m.name='critical_remediation_cost' or m.name='major_remediation_cost' or m.name='minor_remediation_cost' " + - " or m.name='info_remediation_cost' " + - ")"; - - private static final String UPDATE_SQL = "UPDATE project_measures SET value=?," + - "variation_value_1=?,variation_value_2=?,variation_value_3=?,variation_value_4=?,variation_value_5=? WHERE id=?"; +public class TechnicalDebtMeasuresMigration extends BaseDataChange { private final WorkDurationConvertor workDurationConvertor; - private final Database db; public TechnicalDebtMeasuresMigration(Database database, PropertiesDao propertiesDao) { - this.db = database; + super(database); this.workDurationConvertor = new WorkDurationConvertor(propertiesDao); } @Override - public void execute() { + public void execute(Context context) throws SQLException { workDurationConvertor.init(); - new MassUpdater(db).execute( - new MassUpdater.InputLoader<Row>() { - @Override - public String selectSql() { - return SELECT_SQL; - } - - @Override - public Row load(ResultSet rs) throws SQLException { - Row row = new Row(); - row.id = SqlUtil.getLong(rs, 1); - row.value = SqlUtil.getDouble(rs, 2); - row.var1 = SqlUtil.getDouble(rs, 3); - row.var2 = SqlUtil.getDouble(rs, 4); - row.var3 = SqlUtil.getDouble(rs, 5); - row.var4 = SqlUtil.getDouble(rs, 6); - row.var5 = SqlUtil.getDouble(rs, 7); - return row; - } - }, - new MassUpdater.InputConverter<Row>() { - @Override - public String updateSql() { - return UPDATE_SQL; - } - + List<Long> metricIds = context.prepareSelect("select id from metrics " + + "where name='sqale_index' or name='new_technical_debt' " + + "or name='sqale_effort_to_grade_a' or name='sqale_effort_to_grade_b' or name='sqale_effort_to_grade_c' " + + "or name='sqale_effort_to_grade_d' or name='blocker_remediation_cost' or name='critical_remediation_cost' " + + "or name='major_remediation_cost' or name='minor_remediation_cost' or name='info_remediation_cost'").list(Select.RowReader.LONG); + + if (!metricIds.isEmpty()) { + MassUpdate massUpdate = context.prepareMassUpdate(); + + SqlStatement select = massUpdate.select("SELECT pm.id, pm.value " + + ", pm.variation_value_1 , pm.variation_value_2, pm.variation_value_3 " + + ", pm.variation_value_4 , pm.variation_value_5 " + + " FROM project_measures pm " + + " WHERE pm.metric_id IN (" + StringUtils.repeat("?", ",", metricIds.size()) + ")"); + for (int i = 0; i < metricIds.size(); i++) { + select.setLong(i + 1, metricIds.get(i)); + } + massUpdate.update("UPDATE project_measures SET value=?," + + "variation_value_1=?,variation_value_2=?,variation_value_3=?,variation_value_4=?,variation_value_5=? WHERE id=?"); + massUpdate.execute(new MassUpdate.Handler() { @Override - public boolean convert(Row row, PreparedStatement updateStatement) throws SQLException { - setDouble(updateStatement, 1, row.value); - setDouble(updateStatement, 2, row.var1); - setDouble(updateStatement, 3, row.var2); - setDouble(updateStatement, 4, row.var3); - setDouble(updateStatement, 5, row.var4); - setDouble(updateStatement, 6, row.var5); - updateStatement.setLong(7, row.id); + public boolean handle(Select.Row row, SqlStatement update) throws SQLException { + Long id = row.getLong(1); + Double value = row.getDouble(2); + Double var1 = row.getDouble(3); + Double var2 = row.getDouble(4); + Double var3 = row.getDouble(5); + Double var4 = row.getDouble(6); + Double var5 = row.getDouble(7); + + update.setLong(1, convertDebtForDays(value)); + update.setLong(2, convertDebtForDays(var1)); + update.setLong(3, convertDebtForDays(var2)); + update.setLong(4, convertDebtForDays(var3)); + update.setLong(5, convertDebtForDays(var4)); + update.setLong(6, convertDebtForDays(var5)); + update.setLong(7, id); return true; } - } - ); - } - - private void setDouble(PreparedStatement statement, int index, Double value) throws SQLException { - if (value != null) { - statement.setDouble(index, convertDebtForDays(value)); - } else { - statement.setNull(index, Types.DOUBLE); + }); } } - private Long convertDebtForDays(Double data) { - return workDurationConvertor.createFromDays(data); - } - - private static class Row { - private Long id; - private Double value; - private Double var1; - private Double var2; - private Double var3; - private Double var4; - private Double var5; + @CheckForNull + private Long convertDebtForDays(@Nullable Double data) { + if (data != null) { + return workDurationConvertor.createFromDays(data); + } + return null; } - } diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigration.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigration.java index 129b56f862f..f15a77b1ff1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigration.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigration.java @@ -22,69 +22,49 @@ package org.sonar.server.db.migrations.v44; import org.sonar.api.utils.System2; 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 org.sonar.server.db.migrations.BaseDataChange; +import org.sonar.server.db.migrations.MassUpdate; +import org.sonar.server.db.migrations.Select; +import org.sonar.server.db.migrations.SqlStatement; -import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Timestamp; +import java.util.Date; /** * SONAR-5218 * Update all issues having action plan linked on removed action plan. - * + * <p/> * Used in the Active Record Migration 531. + * * @since 4.4 */ -public class IssueActionPlanKeyMigration implements DatabaseMigration { +public class IssueActionPlanKeyMigration extends BaseDataChange { private final System2 system2; - private final Database db; public IssueActionPlanKeyMigration(Database database, System2 system2) { - this.db = database; + super(database); this.system2 = system2; } @Override - public void execute() { - new MassUpdater(db).execute( - new MassUpdater.InputLoader<Row>() { - @Override - public String selectSql() { - return "SELECT i.id FROM issues i " + - "LEFT OUTER JOIN action_plans ap ON ap.kee=i.action_plan_key " + - "WHERE i.action_plan_key IS NOT NULL " + - "AND ap.kee is null "; - } - - @Override - public Row load(ResultSet rs) throws SQLException { - Row row = new Row(); - row.id = SqlUtil.getLong(rs, 1); - return row; - } - }, - new MassUpdater.InputConverter<Row>() { - @Override - public String updateSql() { - return "UPDATE issues SET action_plan_key=NULL,updated_at=? WHERE id=?"; - } + public void execute(Context context) throws SQLException { + final Date now = new Date(system2.now()); + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("SELECT i.id FROM issues i " + + "LEFT OUTER JOIN action_plans ap ON ap.kee=i.action_plan_key " + + "WHERE i.action_plan_key IS NOT NULL " + + "AND ap.kee is null"); + massUpdate.update("UPDATE issues SET action_plan_key=NULL,updated_at=? WHERE id=?"); + massUpdate.execute(new MassUpdate.Handler() { + @Override + public boolean handle(Select.Row row, SqlStatement update) throws SQLException { + Long id = row.getLong(1); - @Override - public boolean convert(Row row, PreparedStatement updateStatement) throws SQLException { - updateStatement.setTimestamp(1, new Timestamp(system2.now())); - updateStatement.setLong(2, row.id); - return true; - } + update.setDate(1, now); + update.setLong(2, id); + return true; } - ); + }); } - - private static class Row { - private Long id; - } - } |