From 992f24fff1a88e7e2d542308fb60190844b2a9a2 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 6 Jul 2016 10:40:14 +0200 Subject: [PATCH] SONAR-7705 Stop using SNAPSHOTS tree columns in Ruby codebase --- .../app/controllers/api/events_controller.rb | 48 ++++++++----------- .../app/controllers/project_controller.rb | 6 +-- .../webapp/WEB-INF/app/models/snapshot.rb | 7 ++- 3 files changed, 28 insertions(+), 33 deletions(-) diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/api/events_controller.rb b/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/api/events_controller.rb index 613f5cc2af5..1f3a7f78502 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/api/events_controller.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/api/events_controller.rb @@ -126,48 +126,40 @@ class Api::EventsController < Api::ApiController load_resource(:admin, params[:resource]) raise "Resource must be a root project" unless @resource.scope=='PRJ' - root_snapshot=nil + analysis=nil if (params[:dateTime]) # try to find a snapshot on that day date = parse_datetime(params[:dateTime], true) - root_snapshot = Snapshot.find(:last, :conditions => ["component_uuid = ? AND created_at >= ? AND created_at <= ?", @resource.uuid, date.to_i*1000, (date + 1.day).to_i*1000], :order => :created_at) - raise "No snapshot exists for given date" unless root_snapshot + analysis = Snapshot.find(:last, :conditions => ["component_uuid = ? AND created_at >= ? AND created_at <= ?", @resource.uuid, date.to_i*1000, (date + 1.day).to_i*1000], :order => :created_at) + raise "No snapshot exists for given date" unless analysis else - root_snapshot = Snapshot.find(:last, :conditions => ["component_uuid = ?", @resource.uuid], :order => :created_at) + analysis = Snapshot.find(:last, :conditions => ["component_uuid = ?", @resource.uuid], :order => :created_at) end - raise "A version already exists on this resource." if params[:category]==EventCategory::KEY_VERSION && root_snapshot.event(EventCategory::KEY_VERSION) + raise "A version already exists on this resource." if params[:category]==EventCategory::KEY_VERSION && analysis.event(EventCategory::KEY_VERSION) raise "An event '#{params[:name]}' (category '#{params[:category]}') already exists on this resource." if Event.already_exists(@resource.last_analysis.id, params[:name], params[:category]) - # Create events for the root project and every submodule event_to_return = nil name = params[:name] desc = params[:description] category = params[:category] - snapshots = Snapshot.find(:all, :include => ['events', 'project'], :conditions => ["(root_snapshot_id = ? OR id = ?) AND scope = 'PRJ'", root_snapshot.id, root_snapshot.id]) - snapshots.each do |snapshot| - # if this is a 'Version' event, must propagate the version number to the snapshot - if category==EventCategory::KEY_VERSION - snapshot.version = name - snapshot.save! - end - # and then create the event linked to the updated snapshot - event=Event.new( - :name => name, - :description => desc, - :category => category, - :snapshot => snapshot, - :component_uuid => snapshot.project.uuid, - :event_date => snapshot.created_at - ) - event.save! - event_to_return = event if snapshot.component_uuid = @resource.uuid + if category==EventCategory::KEY_VERSION + analysis.version = name + analysis.save! end - - + event=Event.new( + :name => name, + :description => desc, + :category => category, + :snapshot => analysis, + :component_uuid => analysis.component_uuid, + :event_date => analysis.created_at + ) + event.save! + respond_to do |format| - format.json { render :json => jsonp(events_to_json([event_to_return])) } - format.xml { render :xml => events_to_xml([event_to_return]) } + format.json { render :json => jsonp(events_to_json([event])) } + format.xml { render :xml => events_to_xml([event]) } format.text { render :text => text_not_supported } end diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/project_controller.rb b/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/project_controller.rb index 15523f43e32..a85983ebede 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/project_controller.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/project_controller.rb @@ -232,7 +232,7 @@ class ProjectController < ApplicationController sid = params[:snapshot_id] if sid - Snapshot.update_all("status='U'", ["id=? or root_snapshot_id=(?)", sid.to_i, sid.to_i]) + Snapshot.update_all("status='U'", ["id=?", sid.to_i]) flash[:notice] = message('project_history.snapshot_deleted') end @@ -313,7 +313,7 @@ class ProjectController < ApplicationController end end - redirect_to :action => 'history', :id => snapshot.root_project.id + redirect_to :action => 'history', :id => snapshot.project.id end def delete_version @@ -338,7 +338,7 @@ class ProjectController < ApplicationController end flash[:notice] = message('project_history.version_removed', :params => h(old_version_name)) - redirect_to :action => 'history', :id => parent_snapshot.root_project.id + redirect_to :action => 'history', :id => parent_snapshot.project.id end def create_event diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/models/snapshot.rb b/server/sonar-web/src/main/webapp/WEB-INF/app/models/snapshot.rb index f9b3f553a96..d1a33ac45ea 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/app/models/snapshot.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/app/models/snapshot.rb @@ -21,13 +21,16 @@ class Snapshot < ActiveRecord::Base include Resourceable belongs_to :project, :class_name => 'Project', :foreign_key => 'component_uuid',:primary_key => 'uuid' - belongs_to :root_project, :class_name => 'Project', :foreign_key => 'root_component_uuid',:primary_key => 'uuid' has_many :events, :class_name => 'Event', :foreign_key => 'analysis_uuid', :primary_key => 'uuid', :dependent => :destroy, :order => 'event_date DESC' STATUS_UNPROCESSED = 'U' STATUS_PROCESSED = 'P' + def root_project + project + end + def created_at long_to_date(:created_at) end @@ -138,7 +141,7 @@ class Snapshot < ActiveRecord::Base end def component_uuid_for_authorization - root_component_uuid + component_uuid end def period_mode(period_index) -- 2.39.5