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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Journable::Timestamps: columns missing in the journal-data table are interpreted as null
Sebastian Fiedlschuster236a8e45cd03
diff --git a/app/models/journable/historic_active_record_relation.rb b/app/models/journable/historic_active_record_relation.rbindex 62dd27d9db2..edfebd0dc6e 100644--- a/app/models/journable/historic_active_record_relation.rb+++ b/app/models/journable/historic_active_record_relation.rb@@ -190,11 +190,7 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation#def select_columns_from_the_appropriate_tables(relation)if relation.select_values.count == 0- relation = relation.select("'#{timestamp}' as timestamp,- #{model.journal_class.table_name}.*,- journals.journable_id as id,- journables.created_at as created_at,- journals.updated_at as updated_at".gsub("\n", ""))+ relation = relation.select(column_select_definitions.join(", "))elsif relation.select_values.count == 1 andrelation.select_values.first.respond_to? :relation andrelation.select_values.first.relation.name == model.journal_class.table_name and@@ -207,6 +203,19 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relationrelationend+ def column_select_definitions+ [+ "#{model.journal_class.table_name}.*",+ "journals.journable_id as id",+ "journables.created_at as created_at",+ "journals.updated_at as updated_at",+ "'#{timestamp}' as timestamp"+ ] + \+ model.column_names_missing_in_journal.collect do |missing_column_name|+ "null as #{missing_column_name}"+ end+ end+# Modify order clauses to use the work-pacakge-journals table.#def modify_order_clauses(arel)diff --git a/app/models/journable/timestamps.rb b/app/models/journable/timestamps.rbindex 4797208e64e..e5c0b264626 100644--- a/app/models/journable/timestamps.rb+++ b/app/models/journable/timestamps.rb@@ -60,6 +60,14 @@ module Journable::Timestampsdef at_timestamp(timestamp)Journable::HistoricActiveRecordRelation.new(all, timestamp:)end++ # There may be database column for the journable class that are not+ # present in the journal-data table, e.g. the 'lock_version' column+ # of the work_packages table.+ #+ def column_names_missing_in_journal+ column_names - journal_class.column_names - ["created_at", "updated_at"]+ endend# Instantiates a journable with historic data from the given timestap.@@ -73,10 +81,12 @@ module Journable::Timestamps"id" => id,"created_at" => created_at,"updated_at" => journal.updated_at,- "timestamp" => timestamp,- "position" => (position if respond_to? :position)+ "timestamp" => timestamp})+ self.class.column_names_missing_in_journal.each do |missing_column_name|+ attributes[missing_column_name] = nil+ endjournable = self.class.instantiate(attributes)journable.readonly!journablediff --git a/spec/models/journable/historic_active_record_relation_spec.rb b/spec/models/journable/historic_active_record_relation_spec.rbindex dd673c57208..ae8d73db0cc 100644--- a/spec/models/journable/historic_active_record_relation_spec.rb+++ b/spec/models/journable/historic_active_record_relation_spec.rb@@ -380,4 +380,27 @@ describe Journable::HistoricActiveRecordRelation doendendend++ describe "#first" do+ describe "for columns that don't exist in the journal-data table" do+ let(:column_name) { :lock_version }+ subject { historic_relation.first.send(column_name) }++ specify "the column name does exist in the journable table" do+ expect(WorkPackage.column_names).to include column_name.to_s+ end++ specify "the column name does not exist in the journal-data table" do+ expect(Journal::WorkPackageJournal.column_names).not_to include column_name.to_s+ end++ it "has the attribute with null value" do+ expect(historic_relation.first.attributes_before_type_cast[column_name]).to be nil+ end++ it "has the typecasted value matching the journable class's data type" do+ expect(subject).to eq 0+ end+ end+ endenddiff --git a/spec/models/journable/timestamps_spec.rb b/spec/models/journable/timestamps_spec.rbindex 2540af51a8e..d3002ef3521 100644--- a/spec/models/journable/timestamps_spec.rb+++ b/spec/models/journable/timestamps_spec.rb@@ -547,6 +547,26 @@ describe Journable::Timestamps doexpect(subject.description).to eq "The work package as it has been on Monday"expect(journable.description).to eq "The work package as it is since Friday"end++ describe "for columns that don't exist in the journal-data table" do+ let(:column_name) { :lock_version }++ specify "the column name does exist in the journable table" do+ expect(WorkPackage.column_names).to include column_name.to_s+ end++ specify "the column name does not exist in the journal-data table" do+ expect(Journal::WorkPackageJournal.column_names).not_to include column_name.to_s+ end++ it "has the attribute with null value" do+ expect(subject.attributes_before_type_cast[column_name]).to be nil+ end++ it "has the typecasted value matching the journable class's data type" do+ expect(subject.send(column_name)).to eq 0+ end+ endendend