Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Fix wrong join with historic relation

Klaus Zanders4056a6c6bcba
diff --git a/app/models/journable/historic_active_record_relation.rb b/app/models/journable/historic_active_record_relation.rb
index 970fdd28976..55769fb827a 100644
--- a/app/models/journable/historic_active_record_relation.rb
+++ b/app/models/journable/historic_active_record_relation.rb
@@ -352,12 +352,7 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation
gsub_table_names_in_sql_string!(node.left)
elsif node.kind_of?(Arel::Nodes::Join) and node.right.kind_of?(Arel::Nodes::On)
[node.right.expr.left, node.right.expr.right].each do |attribute|
- if attribute.respond_to? :relation and
- (attribute.relation == journal_class.arel_table) and
- (attribute.name == "id")
- attribute.relation = Journal.arel_table
- attribute.name = "journable_id"
- end
+ modify_conditions(attribute)
end
end
end
@@ -439,6 +434,13 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation
# expr, which is a NodeExpression itself
[node.expr.left, node.expr.right].each { |child| modify_conditions(child) }
elsif node.kind_of?(Arel::Attributes::Attribute)
+ # With the replacement of the table ivar we might already have switched the base relation ... so if the ID is used
+ # in a join, we should also replace it with the journable
+ if node.relation == model.journal_class.arel_table && node.name == "id"
+ node.relation = Journal.arel_table
+ node.name = "journable_id"
+ end
+
# We find an attribute, figure out if it is the model's table
if node.relation == model.arel_table
if node.name == "id"
diff --git a/app/models/work_package.rb b/app/models/work_package.rb
index 57dd294c6d4..a1ff7016931 100644
--- a/app/models/work_package.rb
+++ b/app/models/work_package.rb
@@ -59,7 +59,7 @@ class WorkPackage < ApplicationRecord
belongs_to :priority, class_name: "IssuePriority"
belongs_to :category, class_name: "Category", optional: true
- has_many :time_entries, dependent: :delete_all, inverse_of: :entity
+ has_many :time_entries, dependent: :delete_all, as: :entity
has_many :file_links, dependent: :delete_all, class_name: "Storages::FileLink", as: :container
diff --git a/app/models/work_package/time_entries_cleaner.rb b/app/models/work_package/time_entries_cleaner.rb
index 654818a3ea8..59c8a044d0d 100644
--- a/app/models/work_package/time_entries_cleaner.rb
+++ b/app/models/work_package/time_entries_cleaner.rb
@@ -56,7 +56,7 @@ module WorkPackage::TimeEntriesCleaner
end
def update_time_entries(work_packages, action)
- TimeEntry.where(["work_package_id IN (?)", work_packages.map(&:id)]).update_all(action)
+ TimeEntry.where(["entity_type = ? AND entity_id IN (?)", "WorkPackage", work_packages.map(&:id)]).update_all(action)
end
def reassign_time_entries_before_destruction_of(work_packages, user, ids)
@@ -73,7 +73,7 @@ module WorkPackage::TimeEntriesCleaner
false
else
- condition = "work_package_id = #{reassign_to.id}, project_id = #{reassign_to.project_id}"
+ condition = "entity_type = 'WorkPackage', entity_id = #{reassign_to.id}, project_id = #{reassign_to.project_id}"
WorkPackage.update_time_entries(work_packages, condition)
end
end
diff --git a/modules/meeting/app/models/meeting.rb b/modules/meeting/app/models/meeting.rb
index 109e4b9255e..8afabfef1c6 100644
--- a/modules/meeting/app/models/meeting.rb
+++ b/modules/meeting/app/models/meeting.rb
@@ -42,6 +42,8 @@ class Meeting < ApplicationRecord
belongs_to :recurring_meeting, optional: true
has_one :scheduled_meeting, inverse_of: :meeting
+ has_many :time_entries, dependent: :delete_all, inverse_of: :entity, as: :entity
+
# Legacy association to minutes, agendas, contents
# to be removed in 17.0
has_one :agenda, dependent: :destroy, class_name: "MeetingAgenda"
diff --git a/spec/models/journable/historic_active_record_relation_spec.rb b/spec/models/journable/historic_active_record_relation_spec.rb
index 502628326a6..ed42f57311a 100644
--- a/spec/models/journable/historic_active_record_relation_spec.rb
+++ b/spec/models/journable/historic_active_record_relation_spec.rb
@@ -560,8 +560,11 @@ RSpec.describe Journable::HistoricActiveRecordRelation do
before { work_package.time_entries << create(:time_entry) }
it "transforms the table name" do
- expect(subject.to_sql).to include \
- "JOIN \"time_entries\" ON \"time_entries\".\"work_package_id\" = \"journals\".\"journable_id\""
+ expect(subject.to_sql).to include <<~SQL.squish
+ INNER JOIN "time_entries"
+ ON "time_entries"."entity_type" = 'WorkPackage'
+ AND "time_entries"."entity_id" = "journals"."journable_id"
+ SQL
end
it "returns the requested work package" do
@@ -572,7 +575,7 @@ RSpec.describe Journable::HistoricActiveRecordRelation do
describe "using a manual sql expression" do
# This is used in the manual-sorting feature.
let(:relation) do
- WorkPackage \
+ WorkPackage
.joins("LEFT OUTER JOIN ordered_work_packages ON ordered_work_packages.work_package_id = work_packages.id")
end
diff --git a/spec/models/journable/timestamps_spec.rb b/spec/models/journable/timestamps_spec.rb
index 10370ba2d15..c10768bf433 100644
--- a/spec/models/journable/timestamps_spec.rb
+++ b/spec/models/journable/timestamps_spec.rb
@@ -274,10 +274,16 @@ RSpec.describe Journable::Timestamps do
subject { relation.at_timestamp(friday) }
it "joins the journable table rather than the journal-data table" do
- expect(subject.to_sql).not_to include \
- "INNER JOIN \"time_entries\" ON \"time_entries\".\"work_package_id\" = \"work_package_journals\".\"id\""
- expect(subject.to_sql).to include \
- "INNER JOIN \"time_entries\" ON \"time_entries\".\"work_package_id\" = \"journals\".\"journable_id\""
+ expect(subject.to_sql).not_to include <<~SQL.squish
+ INNER JOIN "time_entries"
+ ON "time_entries"."entity_type" = 'WorkPackage'
+ AND "time_entries"."entity_id" = "work_package_journals"."id"
+ SQL
+ expect(subject.to_sql).to include <<~SQL.squish
+ INNER JOIN "time_entries"
+ ON "time_entries"."entity_type" = 'WorkPackage'
+ AND "time_entries"."entity_id" = "journals"."journable_id"
+ SQL
end
it "returns the matching records in their historic states" do
@@ -502,8 +508,8 @@ RSpec.describe Journable::Timestamps do
describe "when plucking the id as sub query (workaround)" do
subject do
- sub_query_ids = WorkPackage.at_timestamp(monday) \
- .where(description: "The work package as it has been on Monday") \
+ sub_query_ids = WorkPackage.at_timestamp(monday)
+ .where(description: "The work package as it has been on Monday")
.pluck(:id)
WorkPackage.where(id: sub_query_ids)
end