Skip to content

Commit

Permalink
[api] fix cleanup of project link information when linked projects go…
Browse files Browse the repository at this point in the history
…t removed
  • Loading branch information
adrianschroeter committed Sep 29, 2014
1 parent b922135 commit c233a60
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/api/app/models/project.rb
Expand Up @@ -45,7 +45,7 @@ class ForbiddenError < APIException
has_many :messages, :as => :db_object, :dependent => :delete_all
has_many :watched_projects, :dependent => :destroy, inverse_of: :project

has_many :linkedprojects, -> { order(:position) }, :class_name => 'LinkedProject', foreign_key: :db_project_id
has_many :linkedprojects, -> { order(:position) }, :class_name => 'LinkedProject', foreign_key: :db_project_id, :dependent => :delete_all

has_many :taggings, :as => :taggable, :dependent => :delete_all
has_many :tags, :through => :taggings
Expand Down Expand Up @@ -81,6 +81,9 @@ def cleanup_before_destroy
CacheLine.cleanup_project(self.name)
@del_repo = Project.find_by_name('deleted').repositories[0]

# find linking projects
cleanup_linking_projects

# find linking repositories
cleanup_linking_repos

Expand Down Expand Up @@ -128,8 +131,19 @@ def find_repos(sym)
end
end

def cleanup_linking_projects
#replace links to this project with links to the "deleted" project
LinkedProject.transaction do
LinkedProject.where(linked_db_project: self).each do |lp|
id = lp.db_project_id
lp.destroy
Rails.cache.delete('xml_project_%d' % id)
end
end
end

def cleanup_linking_repos
#replace links to this projects with links to the "deleted" project
#replace links to this project repositories with links to the "deleted" repository
find_repos(:linking_repositories) do |link_rep|
link_rep.path_elements.includes(:link).each do |pe|
next unless Repository.find(pe.repository_id).db_project_id == self.id
Expand Down
38 changes: 38 additions & 0 deletions src/api/test/unit/project_test.rb
Expand Up @@ -266,6 +266,44 @@ def test_create_maintenance_project_and_maintained_project
assert_equal 'maintenance', maintenance_project.project_type()
end

def test_handle_project_links
User.current = users( :Iggy )

# project A
axml = Xmlhash.parse(
"<project name='home:Iggy:A'>
<title>Iggy's Home Project</title>
<description>dummy</description>
<link project='home:Iggy' />
</project>"
)
projectA = Project.create( :name => "home:Iggy:A" )
projectA.update_from_xml(axml)
projectA.save!
# project B
axml = Xmlhash.parse(
"<project name='home:Iggy:B'>
<title>Iggy's Home Project</title>
<description>dummy</description>
<link project='home:Iggy:A' />
</project>"
)
projectB = Project.create( :name => "home:Iggy:B" )
projectB.update_from_xml(axml)
projectB.save!

# validate xml
xml_string = projectA.to_axml
assert_xml_tag xml_string, :tag => :link, :attributes => { :project => "home:Iggy" }
xml_string = projectB.to_axml
assert_xml_tag xml_string, :tag => :link, :attributes => { :project => "home:Iggy:A" }

projectA.destroy
projectB.reload
xml_string = projectB.to_axml
assert_no_xml_tag xml_string, :tag => :link
end


#helper
def put_flags(flags)
Expand Down

0 comments on commit c233a60

Please sign in to comment.