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

Ensure notifications are unique to reminders

Kabiru Mwenja89bd340e93bb
diff --git a/app/models/notification.rb b/app/models/notification.rb
index 2b2fbc5c6e4..99407a2e29d 100644
--- a/app/models/notification.rb
+++ b/app/models/notification.rb
@@ -51,6 +51,8 @@ class Notification < ApplicationRecord
belongs_to :journal
belongs_to :resource, polymorphic: true
+ has_one :reminder, dependent: :nullify
+
include Scopes::Scoped
scopes :unsent_reminders_before,
:mail_reminder_unsent,
diff --git a/db/migrate/20241119131205_create_reminders.rb b/db/migrate/20241119131205_create_reminders.rb
index e74d1601642..87e5e5d6fa4 100644
--- a/db/migrate/20241119131205_create_reminders.rb
+++ b/db/migrate/20241119131205_create_reminders.rb
@@ -11,5 +11,10 @@ class CreateReminders < ActiveRecord::Migration[7.1]
t.timestamps
end
+
+ add_index :reminders, :notification_id,
+ unique: true,
+ where: "notification_id IS NOT NULL",
+ name: "index_reminders_on_notification_id_unique"
end
end
diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb
index e615c033932..b850207b6e3 100644
--- a/spec/models/notification_spec.rb
+++ b/spec/models/notification_spec.rb
@@ -33,6 +33,8 @@ RSpec.describe Notification do
it { is_expected.to belong_to(:resource) }
it { is_expected.to belong_to(:actor).class_name("User") }
it { is_expected.to belong_to(:recipient).class_name("User") }
+
+ it { is_expected.to have_one(:reminder).dependent(:nullify) }
end
describe "Enums" do
diff --git a/spec/models/reminder_spec.rb b/spec/models/reminder_spec.rb
index a65bcfd8857..479a03fd39b 100644
--- a/spec/models/reminder_spec.rb
+++ b/spec/models/reminder_spec.rb
@@ -42,4 +42,8 @@ RSpec.describe Reminder do
.backed_by_column_of_type(:integer)
end
end
+
+ describe "DB Indexes" do
+ it { is_expected.to have_db_index(:notification_id).unique(true) }
+ end
end