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

add non null constraints on data_id/type polymorphic foreign key

ulferts046db16aee99
diff --git a/db/migrate/20230306083203_non_null_data_reference_on_journals.rb b/db/migrate/20230306083203_non_null_data_reference_on_journals.rb
new file mode 100644
index 00000000000..2854d2ae3c4
--- /dev/null
+++ b/db/migrate/20230306083203_non_null_data_reference_on_journals.rb
@@ -0,0 +1,6 @@
+class NonNullDataReferenceOnJournals < ActiveRecord::Migration[7.0]
+ def change
+ change_column_null :journals, :data_id, false
+ change_column_null :journals, :data_type, false
+ end
+end
diff --git a/spec/models/journal_spec.rb b/spec/models/journal_spec.rb
index 86750ad2bf3..c137a8817aa 100644
--- a/spec/models/journal_spec.rb
+++ b/spec/models/journal_spec.rb
@@ -55,4 +55,18 @@ describe Journal do
.to change(Notification, :count).from(1).to(0)
end
end
+
+ describe '#create' do
+ context 'without a data foreign key' do
+ subject { create(:work_package_journal, data: nil) }
+
+ it 'raises an error and does not create a database record' do
+ expect { subject }
+ .to raise_error(ActiveRecord::NotNullViolation)
+
+ expect(described_class.count)
+ .to eq 0
+ end
+ end
+ end
end
diff --git a/spec/services/users/replace_mentions_service_integration_spec.rb b/spec/services/users/replace_mentions_service_integration_spec.rb
index 531888361dd..f328e32ab16 100644
--- a/spec/services/users/replace_mentions_service_integration_spec.rb
+++ b/spec/services/users/replace_mentions_service_integration_spec.rb
@@ -342,7 +342,9 @@ describe Users::ReplaceMentionsService, 'integration' do
end
context 'for journal notes' do
- it_behaves_like 'rewritten mention', :journal, :notes
+ it_behaves_like 'rewritten mention', :journal, :notes do
+ let(:additional_properties) { { data_id: 5, data_type: 'Foobar' } }
+ end
end
context 'for comment comments' do
@@ -357,7 +359,12 @@ describe Users::ReplaceMentionsService, 'integration' do
context 'for customizable_journal value' do
it_behaves_like 'rewritten mention', :journal_customizable_journal, :value do
- let(:additional_properties) { { journal: create(:journal), custom_field: create(:text_wp_custom_field) } }
+ let(:additional_properties) do
+ {
+ journal: create(:journal, data_id: 5, data_type: 'Foobar'),
+ custom_field: create(:text_wp_custom_field)
+ }
+ end
end
end