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

[#46841] Filtering with two timestamps for custom field values wrongfully requires a work package to match at both timestamps

Dombi Attila05d5f71649b6
diff --git a/app/models/journable/historic_active_record_relation.rb b/app/models/journable/historic_active_record_relation.rb
index fa2411e7f18..f25517f5e30 100644
--- a/app/models/journable/historic_active_record_relation.rb
+++ b/app/models/journable/historic_active_record_relation.rb
@@ -105,6 +105,7 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation
relation = self
relation = switch_to_journals_database_table(relation)
+ relation = substitute_join_tables_in_where_clause(relation)
relation = substitute_database_table_in_where_clause(relation)
relation = add_timestamp_condition(relation)
relation = add_join_on_journables_table_with_created_at_column(relation)
@@ -160,7 +161,7 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation
# - `updated_at`
#
# When asking for `WorkPackage.at_timestamp(...).where(id: 123)`, we are expecting `id` to refer
- # to the id of the work pacakge, not of the journalized table entry.
+ # to the id of the work package, not of the journalized table entry.
#
# Also, the `created_at` and `updated_at` columns are not included in the journalized table.
# We gather the `updated_at` from the `journals` mapping table, and the `created_at` from the
@@ -202,6 +203,38 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation
end
end
+ # Additional table joins can appear in the where clause, such as the custom_values table join.
+ # We need to substitute the table name ("custom_values") with the journalized table name
+ # ("customized_journals") in order to retrieve historic data from the journalized table.
+
+ def substitute_join_tables_in_where_clause(relation)
+ relation.where_clause.instance_variable_get(:@predicates).each do |predicate|
+ substitute_custom_values_join_in_predicate(predicate)
+ end
+ relation
+ end
+
+ # For simplicity's sake we replace the "custom_values" join only when the predicate is a String.
+ # This is the way we are receiving the predicate from the `Queries::WorkPackages::Filter::CustomFieldFilter`
+ # The joins are defined in the `Queries::WorkPackages::Filter::CustomFieldContext#where_subselect_joins`
+ # method. If we ever change that method to use Arel, we will need to implement the substitution
+ # for Arel objects as well.
+ def substitute_custom_values_join_in_predicate(predicate)
+ if predicate.is_a? String
+ predicate.gsub! /JOIN (?<!_)#{CustomValue.table_name}/, "JOIN #{Journal::CustomizableJournal.table_name}"
+ predicate.gsub! "JOIN \"#{CustomValue.table_name}\"", "JOIN \"#{Journal::CustomizableJournal.table_name}\""
+
+ customized_type = /custom_values.customized_type = 'WorkPackage'/
+ customized_id = /custom_values.customized_id = work_packages.id/
+
+ # The customizable_journals table has no direct relation to the work_packages table,
+ # but it has to the journals table. We join it to the journals table instead.
+ journal_id = "customizable_journals.journal_id = journals.id"
+
+ predicate.gsub! /#{customized_type}.*AND #{customized_id}/m, journal_id
+ end
+ end
+
# Add a timestamp condition: Select the work package journals that are the
# current ones at the given timestamp.
#
@@ -324,8 +357,9 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation
# Replace table names in sql strings, e.g.
#
- # "work_package.id" => "journals.journable_id"
+ # "work_package.id" => "journals.journable_id"
# "work_package.subject" => "work_package_journals.subject"
+ # "custom_values.*" => "customizable_journals.*"
#
def gsub_table_names_in_sql_string!(sql_string)
sql_string.gsub! /(?<!_)#{model.table_name}\.updated_at/, "journals.updated_at"
@@ -336,6 +370,8 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation
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}\"."
+ sql_string.gsub! /(?<!_)#{CustomValue.table_name}\./, "#{Journal::CustomizableJournal.table_name}."
+ sql_string.gsub! "\"#{CustomValue.table_name}\".", "\"#{Journal::CustomizableJournal.table_name}\"."
end
class NotImplementedError < StandardError; end
diff --git a/app/models/query/results.rb b/app/models/query/results.rb
index 99f613b461c..6c6b821da04 100644
--- a/app/models/query/results.rb
+++ b/app/models/query/results.rb
@@ -51,7 +51,7 @@ class ::Query::Results
def sorted_work_packages_matching_the_filters_today
sorted_work_packages
.visible
- .merge(filtered_work_packages)
+ .merge(filtered_work_packages_matching_the_filters_today)
end
def sorted_work_packages_matching_the_filters_at_any_of_the_given_timestamps
@@ -86,6 +86,10 @@ class ::Query::Results
.where(query.statement)
end
+ def filtered_work_packages_matching_the_filters_today
+ filtered_work_packages.merge(filter_merges)
+ end
+
def sorted_work_packages
work_package_scope
.joins(sort_criteria_joins)
@@ -105,7 +109,6 @@ class ::Query::Results
def work_package_scope
WorkPackage
- .merge(filter_merges)
.includes(all_includes)
.references(:projects)
end
diff --git a/spec/models/journable/historic_active_record_relation_spec.rb b/spec/models/journable/historic_active_record_relation_spec.rb
index b5206913e9e..399a997260d 100644
--- a/spec/models/journable/historic_active_record_relation_spec.rb
+++ b/spec/models/journable/historic_active_record_relation_spec.rb
@@ -37,10 +37,13 @@ RSpec.describe Journable::HistoricActiveRecordRelation do
let(:wednesday) { "2022-08-03".to_datetime }
let(:thursday) { "2022-08-04".to_datetime }
let(:friday) { "2022-08-05".to_datetime }
-
+ let(:default_work_package_attributes) do
+ { description: "The work package as it is since Friday", estimated_hours: 10, project: }
+ end
+ let(:work_package_attributes) { {} }
let(:project) { create(:project) }
let!(:work_package) do
- new_work_package = create(:work_package, description: "The work package as it is since Friday", estimated_hours: 10, project:)
+ new_work_package = create(:work_package, default_work_package_attributes.merge(work_package_attributes))
new_work_package.update_columns created_at: monday
new_work_package
end
@@ -235,6 +238,71 @@ RSpec.describe Journable::HistoricActiveRecordRelation do
expect(subject).to include work_package
end
end
+
+ describe "when searching for custom fields" do
+ let(:custom_field) do
+ create(:text_wp_custom_field,
+ name: 'Text CF',
+ types: project.types,
+ projects: [project])
+ end
+ let!(:monday_cf_journal) do
+ create(:journal_customizable_journal, journal: monday_journal, custom_field:, value: 'Monday_CV')
+ end
+ let!(:wednesday_cf_journal) do
+ create(:journal_customizable_journal, journal: wednesday_journal, custom_field:, value: 'Wednesday_CV')
+ end
+ let!(:friday_cf_journal) do
+ create(:journal_customizable_journal, journal: friday_journal, custom_field:, value: 'Friday_CV')
+ end
+ let(:work_package_attributes) { { custom_values: { custom_field.id => 'Friday_CV' } } }
+ let(:filter) do
+ Queries::WorkPackages::Filter::CustomFieldFilter.create!(
+ name: custom_field.column_name,
+ context: build_stubbed(:query, project:),
+ operator: '~',
+ values:
+ )
+ end
+ let(:relation) { WorkPackage.where(filter.where) }
+
+ context 'with the current value at the current time' do
+ let(:values) { %w(Friday_CV) }
+ let(:historic_relation) { relation.at_timestamp(Timestamp.new("PT0S")) }
+
+ it "returns the requested work package" do
+ expect(subject).to include work_package
+ end
+ end
+
+ context 'with the matching historic value' do
+ let(:values) { %w(Wednesday_CV) }
+
+ it "transforms the expression to join the customizable_journals" do
+ subject.to_sql.squish.tap do |subject_sql|
+ expect(subject_sql)
+ .to include <<~SQL.squish
+ JOIN customizable_journals ON
+ customizable_journals.journal_id = journals.id
+ AND customizable_journals.custom_field_id = #{custom_field.id}
+ SQL
+ expect(subject_sql).to include "customizable_journals.value ILIKE '%Wednesday_CV%'"
+ end
+ end
+
+ it "returns the requested work package" do
+ expect(subject).to include work_package
+ end
+ end
+
+ context 'with a different historic value' do
+ let(:values) { %w(Monday_CV) }
+
+ it "does not return the requested work package" do
+ expect(subject).not_to include work_package
+ end
+ end
+ end
end
describe "foo OR bar (Arel::Nodes::Grouping)" do