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

Create ApplicationRecord.skip_optimistic_locking

Dombi Attila9bc195ffc8d8
diff --git a/app/models/application_record.rb b/app/models/application_record.rb
index 74eef7872d4..39d5baa8251 100644
--- a/app/models/application_record.rb
+++ b/app/models/application_record.rb
@@ -48,4 +48,16 @@ class ApplicationRecord < ActiveRecord::Base
&.first # first result row
&.first # max column
end
+
+ def self.skip_optimistic_locking(&)
+ # TODO: The activerecord-import gem does not respect the ActiveRecord::Base.lock_optimistically
+ # flag, so a direct cleaning of the locking_column is necessary.
+ # Once the gem is updated we can use the ActiveRecord::Base.lock_optimistically = false, instead of
+ # removing the locking_column. See: https://github.com/zdennis/activerecord-import/pull/822
+ original_locking_column = locking_column
+ self.locking_column = nil
+ yield
+ ensure
+ self.locking_column = original_locking_column
+ end
end
diff --git a/modules/meeting/app/models/structured_meeting.rb b/modules/meeting/app/models/structured_meeting.rb
index acdaeeaeea6..5aab08db8a3 100644
--- a/modules/meeting/app/models/structured_meeting.rb
+++ b/modules/meeting/app/models/structured_meeting.rb
@@ -41,36 +41,28 @@ class StructuredMeeting < Meeting
top
end
- # Disable the locking_column in order to avoid causing `StaleObjectError`
- # TODO: The activerecord-import gem does not respect the ActiveRecord::Base.lock_optimistically
- # flag, so a direct cleaning of the locking_column is necessary.
- # Once the gem is updated we can use the ActiveRecord::Base.lock_optimistically = false, instead of
- # removing the locking_column. See: https://github.com/zdennis/activerecord-import/pull/822
- original_locking_column = MeetingAgendaItem.locking_column
- MeetingAgendaItem.locking_column = nil
-
- MeetingAgendaItem.import(
- changed_items,
- on_duplicate_key_update: {
- conflict_target: [:id],
- columns: %i[meeting_id
- author_id
- title
- notes
- position
- duration_in_minutes
- start_time
- end_time
- created_at
- updated_at
- work_package_id
- item_type
- lock_version]
- }
- )
-
- # Re-enable the locking_column
- MeetingAgendaItem.locking_column = original_locking_column
+ # Disable optimistic locking in order to avoid causing `StaleObjectError`.
+ MeetingAgendaItem.skip_optimistic_locking do
+ MeetingAgendaItem.import(
+ changed_items,
+ on_duplicate_key_update: {
+ conflict_target: [:id],
+ columns: %i[meeting_id
+ author_id
+ title
+ notes
+ position
+ duration_in_minutes
+ start_time
+ end_time
+ created_at
+ updated_at
+ work_package_id
+ item_type
+ lock_version]
+ }
+ )
+ end
end
end