001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.server.startup;
021
022 import org.sonar.api.database.DatabaseSession;
023 import org.sonar.api.database.model.MeasureModel;
024 import org.sonar.api.platform.ServerUpgradeStatus;
025 import org.sonar.api.utils.Logs;
026 import org.sonar.jpa.session.DatabaseSessionFactory;
027
028 import javax.persistence.Query;
029 import java.util.List;
030
031 /**
032 * This purge script can not be executed with ActiveRecord. It fails with an out of memory error.
033 *
034 * @since 2.5 this component could be removed after 4 or 5 releases.
035 */
036 public final class DeleteDeprecatedMeasures {
037
038 private ServerUpgradeStatus status;
039 private DatabaseSessionFactory sessionFactory;
040 private static final int MAX_IN_ELEMENTS = 500;
041
042 public DeleteDeprecatedMeasures(DatabaseSessionFactory sessionFactory, ServerUpgradeStatus status) {
043 this.sessionFactory = sessionFactory;
044 this.status = status;
045 }
046
047 public void start() {
048 if (mustDoPurge()) {
049 doPurge();
050 }
051 }
052
053 boolean mustDoPurge() {
054 return status.isUpgraded() && status.getInitialDbVersion() <= 162;
055 }
056
057 void doPurge() {
058 Logs.INFO.info("Deleting measures with deprecated ISO category");
059 deleteRows("SELECT m.id FROM " + MeasureModel.class.getSimpleName() + " m WHERE m.ruleId IS NULL AND m.rulesCategoryId IS NOT NULL");
060
061 Logs.INFO.info("Deleting measures with deprecated priority");
062 deleteRows("SELECT m.id FROM " + MeasureModel.class.getSimpleName() + " m WHERE m.ruleId IS NULL AND m.rulePriority IS NOT NULL");
063 }
064
065 private void deleteRows(String hql) {
066 DatabaseSession session = sessionFactory.getSession();
067 List ids = session.getEntityManager().createQuery(hql).getResultList();
068 int index = 0;
069 while (index < ids.size()) {
070 List paginedSids = ids.subList(index, Math.min(ids.size(), index + MAX_IN_ELEMENTS));
071 Query query = session.createQuery("DELETE FROM " + MeasureModel.class.getSimpleName() + " WHERE id IN (:ids)");
072 query.setParameter("ids", paginedSids);
073 query.executeUpdate();
074 index += MAX_IN_ELEMENTS;
075 session.commit();
076 }
077 }
078 }