import javax.annotation.Nonnull;
import org.sonar.core.util.ProgressLogger;
import org.sonar.db.Database;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
import org.sonar.db.version.BaseDataChange;
import org.sonar.db.version.Select;
import org.sonar.db.version.Upsert;
+import org.sonar.db.version.v53.Component;
+import org.sonar.db.version.v53.Migration53Mapper;
/**
* Remove all duplicated component that have the same keys.
public class RemoveDuplicatedComponentKeys extends BaseDataChange {
private final AtomicLong counter = new AtomicLong(0L);
+ private final DbClient dbClient;
- public RemoveDuplicatedComponentKeys(Database db) {
+ public RemoveDuplicatedComponentKeys(Database db, DbClient dbClient) {
super(db);
+ this.dbClient = dbClient;
}
@Override
Upsert componentUpdate = context.prepareUpsert("DELETE FROM projects WHERE id=?");
Upsert issuesUpdate = context.prepareUpsert("UPDATE issues SET component_uuid=?, project_uuid=? WHERE component_uuid=?");
+ DbSession readSession = dbClient.openSession(false);
+ Migration53Mapper mapper = readSession.getMapper(Migration53Mapper.class);
+
ProgressLogger progress = ProgressLogger.create(getClass(), counter);
progress.start();
try {
- RemoveDuplicatedComponentHandler handler = new RemoveDuplicatedComponentHandler(context, componentUpdate, issuesUpdate);
+ RemoveDuplicatedComponentHandler handler = new RemoveDuplicatedComponentHandler(mapper, componentUpdate, issuesUpdate);
context.prepareSelect(
"SELECT p.kee, COUNT(p.kee) FROM projects p " +
"GROUP BY p.kee " +
progress.log();
} finally {
progress.stop();
+ dbClient.closeSession(readSession);
componentUpdate.close();
issuesUpdate.close();
}
}
private class RemoveDuplicatedComponentHandler implements Select.RowHandler {
- private final Context context;
+ private final Migration53Mapper mapper;
private final Upsert componentUpdate;
private final Upsert issuesUpdate;
private boolean isEmpty = true;
- public RemoveDuplicatedComponentHandler(Context context, Upsert componentUpdate, Upsert issuesUpdate) {
- this.context = context;
+ public RemoveDuplicatedComponentHandler(Migration53Mapper mapper, Upsert componentUpdate, Upsert issuesUpdate) {
+ this.mapper = mapper;
this.componentUpdate = componentUpdate;
this.issuesUpdate = issuesUpdate;
}
@Override
public void handle(Select.Row row) throws SQLException {
- List<Component> components = context
- .prepareSelect("SELECT p.id, p.uuid, p.project_uuid, p.enabled FROM projects p WHERE p.kee=? ORDER BY id")
- .setString(1, row.getString(1))
- .list(ComponentRowReader.INSTANCE);
+ String componentKey = row.getString(1);
+ List<Component> components = mapper.selectComponentsByKey(componentKey);
// We keep the enabled component or the last component of the list
Component refComponent = FluentIterable.from(components).firstMatch(EnabledComponent.INSTANCE).or(components.get(components.size() - 1));
- for (Component componentToRemove : FluentIterable.from(components).filter(Predicates.not(new MatchComponentId(refComponent.id)))) {
+ for (Component componentToRemove : FluentIterable.from(components).filter(Predicates.not(new MatchComponentId(refComponent.getId())))) {
componentUpdate
- .setLong(1, componentToRemove.id)
+ .setLong(1, componentToRemove.getId())
.addBatch();
issuesUpdate
- .setString(1, refComponent.uuid)
- .setString(2, refComponent.projectUuid)
- .setString(3, componentToRemove.uuid)
+ .setString(1, refComponent.getUuid())
+ .setString(2, refComponent.getProjectUuid())
+ .setString(3, componentToRemove.getUuid())
.addBatch();
counter.getAndIncrement();
isEmpty = false;
@Override
public boolean apply(@Nonnull Component input) {
- return input.enabled;
+ return input.isEnabled();
}
}
@Override
public boolean apply(@Nonnull Component input) {
- return input.id == this.id;
- }
- }
-
- private enum ComponentRowReader implements Select.RowReader<Component> {
- INSTANCE;
-
- @Override
- public Component read(Select.Row row) throws SQLException {
- return new Component(row.getLong(1), row.getString(2), row.getString(3), row.getBoolean(4));
+ return input.getId() == this.id;
}
}
- private static class Component {
- private final long id;
- private final String uuid;
- private final String projectUuid;
- private final boolean enabled;
-
- public Component(long id, String uuid, String projectUuid, boolean enabled) {
- this.id = id;
- this.uuid = uuid;
- this.projectUuid = projectUuid;
- this.enabled = enabled;
- }
- }
}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 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.db.version.v53;
+
+public class Component {
+
+ private long id;
+ private String uuid;
+ private String projectUuid;
+ private boolean enabled;
+
+ public long getId() {
+ return id;
+ }
+
+ public Component setId(long id) {
+ this.id = id;
+ return this;
+ }
+
+ public String getUuid() {
+ return uuid;
+ }
+
+ public Component setUuid(String uuid) {
+ this.uuid = uuid;
+ return this;
+ }
+
+ public String getProjectUuid() {
+ return projectUuid;
+ }
+
+ public Component setProjectUuid(String projectUuid) {
+ this.projectUuid = projectUuid;
+ return this;
+ }
+
+ public boolean isEnabled() {
+ return enabled;
+ }
+
+ public Component setEnabled(boolean enabled) {
+ this.enabled = enabled;
+ return this;
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 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.db.version.v53;
+
+import java.util.List;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Result;
+import org.apache.ibatis.annotations.Select;
+
+public interface Migration53Mapper {
+
+ /**
+ * Return components having the same given key
+ */
+ @Select("SELECT p.id AS \"id\", p.uuid AS \"uuid\", p.project_uuid AS \"projectUuid\", p.enabled AS \"enabled\" FROM projects p WHERE p.kee=#{key} ORDER BY id")
+ @Result(javaType = Component.class)
+ List<Component> selectComponentsByKey(@Param("key") String componentKey);
+
+}