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

HistoricActiveRecordRelation#pluck: map existing columns and warn if column is not journalized

Sebastian Fiedlschuster41470cac7fae
diff --git a/app/models/journable/historic_active_record_relation.rb b/app/models/journable/historic_active_record_relation.rb
index edfebd0dc6e..446adb57153 100644
--- a/app/models/journable/historic_active_record_relation.rb
+++ b/app/models/journable/historic_active_record_relation.rb
@@ -74,7 +74,24 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation
# WorkPackage.where(assigned_to_id: 123).at_timestamp(1.year.ago).pluck(:id)
#
def pluck(*column_names)
- column_names.map! { |column_name| column_name == :id ? 'journals.journable_id' : column_name }
+ column_names.map! do |column_name|
+ case column_name
+ when :id, 'id'
+ 'journals.journable_id'
+ when :created_at, 'created_at'
+ 'journables.created_at'
+ when :updated_at, 'updated_at'
+ 'journals.updated_at'
+ else
+ if model.column_names_missing_in_journal.include?(column_name.to_s)
+ Rails.logger.warn "Cannot pluck column `#{column_name}` because this attribute is not journalized," \
+ "i.e. it is missing in the #{journal_class.table_name} table."
+ "null as #{column_name}"
+ else
+ column_name
+ end
+ end
+ end
arel
super
end
diff --git a/spec/models/journable/historic_active_record_relation_spec.rb b/spec/models/journable/historic_active_record_relation_spec.rb
index 33a5403e799..39c8b0372e8 100644
--- a/spec/models/journable/historic_active_record_relation_spec.rb
+++ b/spec/models/journable/historic_active_record_relation_spec.rb
@@ -82,6 +82,58 @@ describe Journable::HistoricActiveRecordRelation do
subject { historic_relation }
+ describe "#pluck" do
+ describe "id" do
+ subject { historic_relation.pluck(:id) }
+
+ it "returns the id of the work package" do
+ expect(subject).to eq [work_package.id]
+ end
+ end
+
+ describe "description" do
+ subject { historic_relation.pluck(:description) }
+
+ it "returns the description of the work package" do
+ expect(subject).to eq ["The work package as it has been on Wednesday"]
+ end
+ end
+
+ describe "created_at" do
+ subject { historic_relation.pluck(:created_at) }
+
+ it "returns the created_at of the work package" do
+ expect(subject).to eq [monday]
+ end
+ end
+
+ describe "updated_at" do
+ subject { historic_relation.pluck(:updated_at) }
+
+ it "returns the updated_at of the work package" do
+ expect(subject).to eq [wednesday]
+ end
+ end
+
+ describe "position" do
+ subject { historic_relation.pluck(:position) }
+
+ it "returns null because the column is not journalized" do
+ expect(subject).to eq [nil]
+ end
+
+ it "does not raise an error" do
+ expect { subject }.not_to raise_error
+ end
+
+ it "prints a warning" do
+ expect(Rails.logger).to receive(:warn).with(/position/)
+
+ subject
+ end
+ end
+ end
+
describe "#where" do
describe "project_id in array (Arel::Nodes::HomogeneousIn)" do
let(:relation) { WorkPackage.where(project_id: [project.id, 1, 2, 3]) }