]> source.dussan.org Git - sonarqube.git/commitdiff
Fix migration of manual measures (again)
authorSimon Brandhof <simon.brandhof@gmail.com>
Wed, 17 Aug 2011 14:30:15 +0000 (16:30 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Wed, 17 Aug 2011 14:30:15 +0000 (16:30 +0200)
sonar-server/src/main/webapp/WEB-INF/db/migrate/212_move_async_measures.rb

index 6b651a24e7d9e9b2a3f975c0d57566831e451535..6b2d41db9a4bb15ea729e4bd0847dc9059bc08eb 100644 (file)
@@ -29,26 +29,25 @@ class MoveAsyncMeasures < ActiveRecord::Migration
   class ProjectMeasure < ActiveRecord::Base
   end
 
-  class AsyncMeasureSnapshot < ActiveRecord::Base
-    belongs_to :measure, :foreign_key => 'project_measure_id', :class_name => "ProjectMeasure"
-  end
-
   def self.up
-    deprecated_measures=AsyncMeasureSnapshot.find(:all, :include => 'measure', :conditions => 'snapshot_id is not null')
+    deprecated_measures=ProjectMeasure.find_by_sql("select p1.* from project_measures p1 where p1.snapshot_id is null and p1.measure_date is not null and not exists(select id from project_measures p2 where p2.project_id=p1.project_id and p2.metric_id=p1.metric_id and p2.measure_date is not null and p2.measure_date>p1.measure_date)")
+
     say_with_time "Moving #{deprecated_measures.size} measures" do
       deprecated_measures.each do |dm|
-        if dm.measure && dm.project_id
+        if dm.project_id
           ManualMeasure.create(
               :resource_id => dm.project_id,
-              :metric_id => dm.measure.metric_id,
-              :value => dm.measure.value,
-              :text_value => dm.measure.text_value,
+              :metric_id => dm.metric_id,
+              :value => dm.value,
+              :text_value => dm.text_value,
               :created_at => dm.measure_date,
               :updated_at => dm.measure_date,
-              :description => dm.measure.description)
+              :description => dm.description)
         end
       end
     end
+
+    ProjectMeasure.delete_all("snapshot_id is null and measure_date is not null")
   end
 
 end