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

at_timestamp: handle join clauses with `work_packages.id`

Sebastian Fiedlschuster9bfad21dc3de
diff --git a/app/models/journable/historic_active_record_relation.rb b/app/models/journable/historic_active_record_relation.rb
index 4ba73f53439..e16b01430ed 100644
--- a/app/models/journable/historic_active_record_relation.rb
+++ b/app/models/journable/historic_active_record_relation.rb
@@ -123,8 +123,7 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation
def substitute_database_table_in_where_clause(relation)
relation.where_clause.instance_variable_get(:@predicates).each do |predicate|
if predicate.kind_of? String
- predicate.gsub! "#{model.table_name}.", "#{model.journal_class.table_name}."
- predicate.gsub! "#{model.journal_class.table_name}.id", "journals.journable_id"
+ gsub_table_names_in_sql_string!(predicate)
elsif predicate.left.relation == arel_table
if predicate.right.respond_to? :name and predicate.right.name == "id"
predicate.right.instance_variable_set(:@name, "journable_id")
@@ -188,8 +187,7 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation
def modify_order_clauses(arel)
arel.instance_variable_get(:@ast).instance_variable_get(:@orders).each do |order_clause|
if order_clause.kind_of? Arel::Nodes::SqlLiteral
- order_clause.gsub! "#{model.table_name}.", "#{model.journal_class.table_name}."
- order_clause.gsub! "#{model.journal_class.table_name}.id", "journals.journable_id"
+ gsub_table_names_in_sql_string!(order_clause)
elsif order_clause.expr.relation == model.arel_table
if order_clause.expr.name == "id"
order_clause.expr.name = "journable_id"
@@ -225,7 +223,9 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation
def modify_joins(arel)
arel.instance_variable_get(:@ast).instance_variable_get(:@cores).each do |core|
core.instance_variable_get(:@source).right.each do |node|
- if node.kind_of?(Arel::Nodes::Join) and node.right.kind_of?(Arel::Nodes::On)
+ if node.kind_of? Arel::Nodes::StringJoin
+ 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
@@ -239,6 +239,18 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation
end
arel
end
+
+ # Replace table names in sql strings, e.g.
+ #
+ # "work_package.id" => "journals.journable_id"
+ # "work_package.subject" => "work_package_journals.subject"
+ #
+ def gsub_table_names_in_sql_string!(sql_string)
+ sql_string.gsub! /(?<!_)#{model.table_name}\.id/, "journals.journable_id"
+ sql_string.gsub! "\"#{model.table_name}\".\"id\"", "\"journals\".\"journable_id\""
+ sql_string.gsub! /(?<!_)#{model.table_name}\./, "#{model.journal_class.table_name}."
+ sql_string.gsub! "\"#{model.table_name}\".", "\"#{model.journal_class.table_name}\"."
+ end
end
# rubocop:enable Style/ClassCheck
diff --git a/spec/models/journable/timestamps_spec.rb b/spec/models/journable/timestamps_spec.rb
index 93b4724be19..fa857285246 100644
--- a/spec/models/journable/timestamps_spec.rb
+++ b/spec/models/journable/timestamps_spec.rb
@@ -535,6 +535,19 @@ describe Journable::Timestamps do
expect(subject).to include work_package
end
end
+
+ describe "when the sql where statement includes work_package.id" do
+ # This is used in the manual-sorting feature.
+ subject { WorkPackage.where("(\"work_packages\".\"id\" IN (#{work_package.id}))").at_timestamp(wednesday) }
+
+ it "transforms the expression to query the correct table" do
+ expect(subject.to_sql).to include "\"journals\".\"journable_id\" IN (#{work_package.id})"
+ end
+
+ it "returns the requested work package" do
+ expect(subject).to include work_package
+ end
+ end
end
describe "when chaining an order clause" do
@@ -598,6 +611,37 @@ describe Journable::Timestamps do
end
end
end
+
+ describe "when chaining a join" do
+ describe "when joining using active record" do
+ before { work_package.time_entries << create(:time_entry) }
+
+ subject { WorkPackage.joins(:time_entries).at_timestamp(wednesday) }
+
+ it "transforms the table name" do
+ expect(subject.to_sql).to include \
+ "JOIN \"time_entries\" ON \"time_entries\".\"work_package_id\" = \"journals\".\"journable_id\""
+ end
+
+ it "returns the requested work package" do
+ expect(subject).to include work_package
+ end
+ end
+
+ describe "when joining using a manual sql expression" do
+ # This is used in the manual-sorting feature.
+ subject { WorkPackage.joins("LEFT OUTER JOIN ordered_work_packages ON ordered_work_packages.work_package_id = work_packages.id").at_timestamp(wednesday) }
+
+ it "transforms the table name" do
+ expect(subject.to_sql).to include \
+ "LEFT OUTER JOIN ordered_work_packages ON ordered_work_packages.work_package_id = journals.journable_id"
+ end
+
+ it "returns the requested work package" do
+ expect(subject).to include work_package
+ end
+ end
+ end
end
describe "#at_timestamp" do