summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2013-12-18 18:39:09 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2013-12-18 18:39:09 +0000
commit5c5ba0c61af44bdbd8931c131192963179cca9ac (patch)
treed950222bd2a006e1b1ce1d14b31e607f95240d6e
parentd4297ca3ff309a63737e2d12adb4ea1ec0085828 (diff)
downloadredmine-5c5ba0c61af44bdbd8931c131192963179cca9ac.tar.gz
redmine-5c5ba0c61af44bdbd8931c131192963179cca9ac.zip
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
-rw-r--r--app/models/issue.rb2
-rw-r--r--app/models/project.rb2
-rw-r--r--db/migrate/20131218183023_delete_orphan_time_entries_custom_values.rb9
-rw-r--r--test/unit/issue_test.rb9
-rw-r--r--test/unit/project_test.rb9
5 files changed, 29 insertions, 2 deletions
diff --git a/app/models/issue.rb b/app/models/issue.rb
index 0101abf00..c09b591a1 100644
--- a/app/models/issue.rb
+++ b/app/models/issue.rb
@@ -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
diff --git a/app/models/project.rb b/app/models/project.rb
index d4f29453a..4167d9a3a 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -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
diff --git a/db/migrate/20131218183023_delete_orphan_time_entries_custom_values.rb b/db/migrate/20131218183023_delete_orphan_time_entries_custom_values.rb
new file mode 100644
index 000000000..f4e593559
--- /dev/null
+++ b/db/migrate/20131218183023_delete_orphan_time_entries_custom_values.rb
@@ -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
diff --git a/test/unit/issue_test.rb b/test/unit/issue_test.rb
index 4e82aa007..0e252cc2e 100644
--- a/test/unit/issue_test.rb
+++ b/test/unit/issue_test.rb
@@ -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
diff --git a/test/unit/project_test.rb b/test/unit/project_test.rb
index 88e99c439..945f775e8 100644
--- a/test/unit/project_test.rb
+++ b/test/unit/project_test.rb
@@ -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