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

Journable::Timestamps: columns missing in the journal-data table are interpreted as null

Sebastian Fiedlschustere9c6bce775a3
diff --git a/app/models/journable/historic_active_record_relation.rb b/app/models/journable/historic_active_record_relation.rb
index f1f83e0fae3..9eb70dd9b04 100644
--- a/app/models/journable/historic_active_record_relation.rb
+++ b/app/models/journable/historic_active_record_relation.rb
@@ -195,11 +195,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.created_at as updated_at".gsub("\n", ""))
+ relation = relation.select(column_select_definitions.join(", "))
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
@@ -212,6 +208,19 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation
relation
end
+ def column_select_definitions
+ [
+ "#{model.journal_class.table_name}.*",
+ "journals.journable_id as id",
+ "journables.created_at as created_at",
+ "journals.created_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.rb
index 4797208e64e..e5c0b264626 100644
--- a/app/models/journable/timestamps.rb
+++ b/app/models/journable/timestamps.rb
@@ -60,6 +60,14 @@ module Journable::Timestamps
def 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"]
+ end
end
# 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
+ end
journable = self.class.instantiate(attributes)
journable.readonly!
journable
diff --git a/spec/models/journable/historic_active_record_relation_spec.rb b/spec/models/journable/historic_active_record_relation_spec.rb
index 44f0696c30c..ddbaf9428ae 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 do
end
end
end
+
+ 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
+ end
end
diff --git a/spec/models/journable/timestamps_spec.rb b/spec/models/journable/timestamps_spec.rb
index 2540af51a8e..d3002ef3521 100644
--- a/spec/models/journable/timestamps_spec.rb
+++ b/spec/models/journable/timestamps_spec.rb
@@ -547,6 +547,26 @@ describe Journable::Timestamps do
expect(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
+ end
end
end