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

Merge https://github.com/opf/openproject/pull/11243 into https://github.com/opf/openproject/pull/11678

Sebastian Fiedlschustercbd1c9a44b18
diff --git a/app/models/journable/historic_active_record_relation.rb b/app/models/journable/historic_active_record_relation.rb
index f269c198ab9..e16b01430ed 100644
--- a/app/models/journable/historic_active_record_relation.rb
+++ b/app/models/journable/historic_active_record_relation.rb
@@ -123,7 +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}."
+ 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")
@@ -142,7 +142,7 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation
def add_timestamp_condition(relation)
relation \
.joins("INNER JOIN \"journals\" ON \"journals\".\"data_type\" = '#{model.journal_class.name}'
- AND \"journals\".\"data_id\" = \"#{model.journal_class.table_name}\".\"id\"") \
+ AND \"journals\".\"data_id\" = \"#{model.journal_class.table_name}\".\"id\"".gsub("\n", "")) \
.merge(Journal.at_timestamp(timestamp))
end
@@ -152,7 +152,7 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation
def add_join_on_journables_table_with_created_at_column(relation)
relation \
.joins("INNER JOIN (SELECT id, created_at FROM \"#{model.table_name}\") AS journables
- ON \"journables\".\"id\" = \"journals\".\"journable_id\"")
+ ON \"journables\".\"id\" = \"journals\".\"journable_id\"".gsub("\n", ""))
end
# Gather the columns we need in our model from the different tables in the sql query:
@@ -169,7 +169,7 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation
#{model.journal_class.table_name}.*,
journals.journable_id as id,
journables.created_at as created_at,
- journals.created_at as updated_at")
+ journals.created_at as updated_at".gsub("\n", ""))
elsif relation.select_values.count == 1 and
relation.select_values.first.respond_to? :relation and
relation.select_values.first.relation.name == model.journal_class.table_name and
@@ -187,9 +187,14 @@ 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}."
+ gsub_table_names_in_sql_string!(order_clause)
elsif order_clause.expr.relation == model.arel_table
- order_clause.expr.relation = model.journal_class.arel_table
+ if order_clause.expr.name == "id"
+ order_clause.expr.name = "journable_id"
+ order_clause.expr.relation = Journal.arel_table
+ else
+ order_clause.expr.relation = model.journal_class.arel_table
+ end
end
end
arel
@@ -218,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
@@ -232,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 ab578be9d0e..67c3332a957 100644
--- a/spec/models/journable/timestamps_spec.rb
+++ b/spec/models/journable/timestamps_spec.rb
@@ -522,6 +522,32 @@ describe Journable::Timestamps do
it "returns the requested work package" do
expect(subject).to include work_package
end
+
+ describe "when the sql where statement includes work_package.id" do
+ # This is used, for example, in 'follows' relations.
+ 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
+
+ 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
@@ -546,6 +572,79 @@ describe Journable::Timestamps do
expect(subject).to include work_package
end
end
+
+ describe "when chaining a manual order clause using work_packages.id" do
+ # This is used in the manual-sorting feature.
+ subject { WorkPackage.order("work_packages.id DESC").at_timestamp(wednesday) }
+
+ it "transforms the table name" do
+ expect(subject.to_sql).to include "journals.journable_id DESC"
+ end
+
+ it "returns the requested work package" do
+ expect(subject).to include work_package
+ end
+ end
+
+ describe "when chaining an order clause with work_packages.id" do
+ subject { WorkPackage.order(id: :desc).at_timestamp(wednesday) }
+
+ it "transforms the table name" do
+ expect(subject.to_sql).to include "\"journals\".\"journable_id\" DESC"
+ end
+
+ it "returns the requested work package" do
+ expect(subject).to include work_package
+ end
+ end
+
+ describe "when chaining several order clauses" do
+ subject { WorkPackage.order(subject: :asc, id: :desc).at_timestamp(wednesday) }
+
+ it "transforms the table name" do
+ expect(subject.to_sql).to include "\"work_package_journals\".\"subject\" ASC"
+ expect(subject.to_sql).to include "\"journals\".\"journable_id\" DESC"
+ end
+
+ it "returns the requested work package" do
+ expect(subject).to include work_package
+ 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 do
+ WorkPackage \
+ .joins("LEFT OUTER JOIN ordered_work_packages ON ordered_work_packages.work_package_id = work_packages.id") \
+ .at_timestamp(wednesday)
+ end
+
+ 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