From: simonbrandhof Date: Tue, 4 Jan 2011 15:52:43 +0000 (+0000) Subject: fix migration to delete unvalid snapshots generated in sonar <= 2.3 X-Git-Tag: 2.6~194 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=32de21d20578c3202141cd1965a8f3d53c8ba786;p=sonarqube.git fix migration to delete unvalid snapshots generated in sonar <= 2.3 --- diff --git a/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java b/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java index 4c0ee4aa224..a80961ba4ce 100644 --- a/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java +++ b/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java @@ -31,7 +31,7 @@ import javax.persistence.*; public class SchemaMigration { public final static int VERSION_UNKNOWN = -1; - public static final int LAST_VERSION = 169; + public static final int LAST_VERSION = 170; public final static String TABLE_NAME = "schema_migrations"; diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/152_delete_duplicated_lib_snapshots.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/152_delete_duplicated_lib_snapshots.rb deleted file mode 100644 index f9906e36a8f..00000000000 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/152_delete_duplicated_lib_snapshots.rb +++ /dev/null @@ -1,65 +0,0 @@ -# -# Sonar, entreprise quality control tool. -# Copyright (C) 2009 SonarSource SA -# mailto:contact AT sonarsource DOT com -# -# Sonar 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. -# -# Sonar 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 Sonar; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 -# - -# -# Sonar 2.4 -# -class DeleteDuplicatedLibSnapshots < ActiveRecord::Migration - def self.up - metric=Metric.find(:first, :conditions => ['name=?','lines']) - if metric - snapshots=select_snapshots_without_measures(metric) - delete_snapshots(snapshots) - end - end - - def self.select_snapshots_without_measures(metric) - snapshots=[] - say_with_time "Select project snapshots without measures..." do - snapshots=Snapshot.find_by_sql ["SELECT s.id FROM snapshots s WHERE s.scope='PRJ' and s.qualifier IN ('TRK', 'BRC') AND status='P' AND NOT EXISTS (select m.id from project_measures m WHERE m.snapshot_id=s.id AND m.metric_id=?) and not exists(select * from dependencies d where d.to_snapshot_id=s.id)", metric.id] - end - snapshots - end - - def self.filter_involved_in_dependencies(snapshots) - result=[] - if snapshots.size>0 - say_with_time "Filter #{snapshots.size} snapshots..." do - result=Snapshot.find_by_sql ["SELECT s.id FROM snapshots s where s.id in (?) and not exists(select * from dependencies d where d.to_snapshot_id=s.id", snapshots.map{|s| s.id}] - end - end - return result - end - - def self.delete_snapshots(snapshots) - if snapshots.size > 0 - say_with_time "Deleting #{snapshots.size} orphan snapshots..." do - sids=snapshots.map{|s| s.id} - page_size=100 - page_count=(sids.size/page_size) - page_count+=1 if (sids.size % page_size)>0 - page_count.times do |page_index| - page_sids=sids[page_index*page_size...(page_index+1)*page_size] - Snapshot.delete(page_sids) - end - end - end - end -end diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/170_delete_unvalid_project_snapshots.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/170_delete_unvalid_project_snapshots.rb new file mode 100644 index 00000000000..623f1c8087f --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/170_delete_unvalid_project_snapshots.rb @@ -0,0 +1,56 @@ +# +# Sonar, entreprise quality control tool. +# Copyright (C) 2009 SonarSource SA +# mailto:contact AT sonarsource DOT com +# +# Sonar 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. +# +# Sonar 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 Sonar; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 +# + +# +# Sonar 2.5 +# +class DeleteUnvalidProjectSnapshots < ActiveRecord::Migration + + def self.up + metric=Metric.find(:first, :conditions => ['name=?','lines']) + if metric + snapshots=select_snapshots_without_measures(metric) + delete_snapshots(snapshots) + end + end + + def self.select_snapshots_without_measures(metric) + snapshots=nil + say_with_time "Select project snapshots without measures..." do + snapshots=Snapshot.find_by_sql ["SELECT s.id FROM snapshots s WHERE s.scope='PRJ' and s.qualifier IN ('TRK', 'BRC') AND status='P' AND islast=? AND NOT EXISTS (select m.id from project_measures m WHERE m.snapshot_id=s.id AND m.metric_id=?)", false, metric.id] + end + snapshots + end + + def self.delete_snapshots(snapshots) + if snapshots.size>0 + say_with_time "Deleting #{snapshots.size} orphan snapshots..." do + sids=snapshots.map{|s| s.id} + page_size=100 + page_count=(sids.size/page_size) + page_count+=1 if (sids.size % page_size)>0 + page_count.times do |page_index| + page_sids=sids[page_index*page_size...(page_index+1)*page_size] + Snapshot.delete(page_sids) + end + end + end + end +end