Fixed that time entries custom values are not deleted when deleting a project or an issue (#15709).

git-svn-id: http://svn.redmine.org/redmine/trunk@12421 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2013-12-18 18:39:09 +00:00
parent d4297ca3ff
commit 5c5ba0c61a
5 changed files with 29 additions and 2 deletions

View File

@ -38,7 +38,7 @@ class Issue < ActiveRecord::Base
},
:readonly => true
has_many :time_entries, :dependent => :delete_all
has_many :time_entries, :dependent => :destroy
has_and_belongs_to_many :changesets, :order => "#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC"
has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all

View File

@ -39,7 +39,7 @@ class Project < ActiveRecord::Base
has_many :issues, :dependent => :destroy, :include => [:status, :tracker]
has_many :issue_changes, :through => :issues, :source => :journals
has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC"
has_many :time_entries, :dependent => :delete_all
has_many :time_entries, :dependent => :destroy
has_many :queries, :class_name => 'IssueQuery', :dependent => :delete_all
has_many :documents, :dependent => :destroy
has_many :news, :dependent => :destroy, :include => :author

View File

@ -0,0 +1,9 @@
class DeleteOrphanTimeEntriesCustomValues < ActiveRecord::Migration
def up
CustomValue.where("customized_type = ? AND NOT EXISTS (SELECT 1 FROM #{TimeEntry.table_name} t WHERE t.id = customized_id)", "TimeEntry").delete_all
end
def down
# nop
end
end

View File

@ -1375,6 +1375,15 @@ class IssueTest < ActiveSupport::TestCase
assert_nil TimeEntry.find_by_issue_id(1)
end
def test_destroy_should_delete_time_entries_custom_values
issue = Issue.generate!
time_entry = TimeEntry.generate!(:issue => issue, :custom_field_values => {10 => '1'})
assert_difference 'CustomValue.where(:customized_type => "TimeEntry").count', -1 do
assert issue.destroy
end
end
def test_destroying_a_deleted_issue_should_not_raise_an_error
issue = Issue.find(1)
Issue.find(1).destroy

View File

@ -263,6 +263,15 @@ class ProjectTest < ActiveSupport::TestCase
assert_equal 0, CustomValue.where(:customized_type => ['Project', 'Issue', 'TimeEntry', 'Version']).count
end
def test_destroy_should_delete_time_entries_custom_values
project = Project.generate!
time_entry = TimeEntry.generate!(:project => project, :custom_field_values => {10 => '1'})
assert_difference 'CustomValue.where(:customized_type => "TimeEntry").count', -1 do
assert project.destroy
end
end
def test_move_an_orphan_project_to_a_root_project
sub = Project.find(2)
sub.set_parent! @ecookbook