From 2d95bd31699216644389b820bdbbcc605959a5ea Mon Sep 17 00:00:00 2001 From: jorg-vr Date: Tue, 23 May 2023 15:00:07 +0200 Subject: [PATCH 1/2] Revert "Use `create_or_find_by` to reduce activity status deadlocks" --- app/models/activity.rb | 4 ++-- app/models/activity_status.rb | 4 +++- app/models/series_membership.rb | 4 ++-- test/models/activity_status_test.rb | 5 +---- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/app/models/activity.rb b/app/models/activity.rb index 12ae3ee780..e9b7e6e428 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -448,9 +448,9 @@ def activity_status_for(user, series = nil) def activity_status_for!(user, series = nil) first_try = true begin - ActivityStatus.create_or_find_by(activity: self, series: series, user: user) + ActivityStatus.find_or_create_by(activity: self, series: series, user: user) rescue StandardError - # https://github.com/dodona-edu/dodona/pull/4547 + # https://github.com/dodona-edu/dodona/issues/1877 raise unless first_try first_try = false diff --git a/app/models/activity_status.rb b/app/models/activity_status.rb index 6e18883040..825abf8422 100644 --- a/app/models/activity_status.rb +++ b/app/models/activity_status.rb @@ -31,10 +31,12 @@ class ActivityStatus < ApplicationRecord belongs_to :series, optional: true belongs_to :user + validates :series_id_non_nil, uniqueness: { scope: %i[user_id activity_id] }, on: :create + scope :in_series, ->(series) { where(series: series) } scope :for_user, ->(user) { where(user: user) } - before_create :initialise_series_id_non_nil + before_validation :initialise_series_id_non_nil, if: -> { new_record? } before_create :initialise_values_for_content_page, if: -> { activity.content_page? } before_create :initialise_values_for_exercise, if: -> { activity.exercise? } diff --git a/app/models/series_membership.rb b/app/models/series_membership.rb index ff8a5a8328..d0ee6fb097 100644 --- a/app/models/series_membership.rb +++ b/app/models/series_membership.rb @@ -32,11 +32,11 @@ def add_activity_statuses_delayed def add_activity_statuses if activity.is_a? Exercise activity.submissions.where(course: series.course).distinct(:user_id).find_each do |submission| - ActivityStatus.create_or_find_by(series: series, activity: activity, user: submission.user) + ActivityStatus.create(series: series, activity: activity, user: submission.user) end else activity.activity_read_states.where(course: series.course).distinct(:user_id).find_each do |ars| - ActivityStatus.create_or_find_by(series: series, activity: activity, user: ars.user) + ActivityStatus.create(series: series, activity: activity, user: ars.user) end end end diff --git a/test/models/activity_status_test.rb b/test/models/activity_status_test.rb index 2716e02b00..269b277c0c 100644 --- a/test/models/activity_status_test.rb +++ b/test/models/activity_status_test.rb @@ -44,10 +44,7 @@ class ActivityStatusTest < ActiveSupport::TestCase activity = exercises(:python_exercise) user = users(:student) ActivityStatus.create(user: user, activity: activity, series: nil) - assert_equal 1, ActivityStatus.count - assert_raises ActiveRecord::RecordNotUnique do - ActivityStatus.create(user: user, activity: activity, series: nil) - end + ActivityStatus.create(user: user, activity: activity, series: nil) assert_equal 1, ActivityStatus.count end From e37a9f12bc98f5e9efbddf8d877454c7839100c4 Mon Sep 17 00:00:00 2001 From: jorg-vr Date: Tue, 23 May 2023 15:07:11 +0200 Subject: [PATCH 2/2] Bump version --- config/initializers/00_version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/initializers/00_version.rb b/config/initializers/00_version.rb index 8fe2c88ae8..5653856f77 100644 --- a/config/initializers/00_version.rb +++ b/config/initializers/00_version.rb @@ -3,7 +3,7 @@ class Application module Version MAJOR = 6 MINOR = 8 - PATCH = 5 + PATCH = 6 STRING = [MAJOR, MINOR, PATCH].compact.join('.') end