Project

General

Profile

Patch #36416 ยป 0001-Cleanup-more-dependent-objects-on-project-delete.patch

Holger Just, 2022-01-25 18:53

View differences:

app/models/project.rb
30 30
  IDENTIFIER_MAX_LENGTH = 100
31 31

  
32 32
  # Specific overridden Activities
33
  has_many :time_entry_activities
33
  has_many :time_entry_activities, :dependent => :destroy
34 34
  has_many :memberships, :class_name => 'Member', :inverse_of => :project
35 35
  # Memberships of active users only
36 36
  has_many :members,
......
43 43
  belongs_to :default_version, :class_name => 'Version'
44 44
  belongs_to :default_assigned_to, :class_name => 'Principal'
45 45
  has_many :time_entries, :dependent => :destroy
46
  has_many :queries, :dependent => :delete_all
46
  has_many :queries, :dependent => :destroy
47 47
  has_many :documents, :dependent => :destroy
48 48
  has_many :news, lambda {includes(:author)}, :dependent => :destroy
49 49
  has_many :issue_categories, lambda {order(:name)}, :dependent => :delete_all
db/migrate/20220125182055_delete_orphaned_activities_and_queries_roles.rb
1
class DeleteOrphanedActivitiesAndQueriesRoles < ActiveRecord::Migration[6.1]
2
  def self.up
3
    TimeEntryActivity.left_outer_joins(:project).where(projects:{id: nil}).where.not(project_id: nil).delete_all
4

  
5
    queries_roles = "#{Query.table_name_prefix}queries_roles#{Query.table_name_suffix}"
6
    queries = Query.table_name
7
    ActiveRecord::Base.connection.execute "DELETE #{queries_roles} FROM #{queries_roles} LEFT OUTER JOIN #{queries} ON #{queries_roles}.query_id = #{queries}.id WHERE #{queries}.id IS NULL"
8
  end
9

  
10
  def self.down
11
    # no-op
12
  end
13
end
test/unit/project_test.rb
233 233
    # some boards
234 234
    assert @ecookbook.boards.any?
235 235

  
236
    # generate some dependent objects
237
    overridden_activity = TimeEntryActivity.new({:name => "Project", :project => @ecookbook})
238
    assert overridden_activity.save!
239

  
240
    query = IssueQuery.generate!(:project => @ecookbook, :visibility => Query::VISIBILITY_ROLES, :roles => Role.where(:id => [1, 3]).to_a)
241

  
236 242
    @ecookbook.destroy
237 243
    # make sure that the project non longer exists
238 244
    assert_raise(ActiveRecord::RecordNotFound) {Project.find(@ecookbook.id)}
......
240 246
    assert_not Member.where(:project_id => @ecookbook.id).exists?
241 247
    assert_not Board.where(:project_id => @ecookbook.id).exists?
242 248
    assert_not Issue.where(:project_id => @ecookbook.id).exists?
249
    assert_not Enumeration.where(:project_id => @ecookbook.id).exists?
250

  
251
    assert_not Query.where(:project_id => @ecookbook.id).exists?
252
    assert_nil ActiveRecord::Base.connection.select_value("SELECT 1 FROM queries_roles WHERE query_id = #{query.id}")
243 253
  end
244 254

  
245 255
  def test_destroy_should_destroy_subtasks
    (1-1/1)