Skip to content

Commit

Permalink
Close government / update ministers transactionally
Browse files Browse the repository at this point in the history
Currently, it's possible for this method to close the government, and
then run into trouble later on while closing ministerial appointments.
If one of these `save!` calls failed, we'd be left in an inconsistent
state.

Wrapping the update and save! calls in a transaction will mean that they
either all succeed, or are all rolled back, avoiding the chance of
getting into this bad state.

I also suspect (but am not 100% sure) that doing this transactionally
will significantly speed up the code, because we won't have to open /
close a transaction for every role appointment we save.
  • Loading branch information
richardTowers committed Jun 21, 2024
1 parent 3b7d222 commit ba37433
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions app/controllers/admin/governments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ def prepare_to_close
def close
government = Government.find(params[:id])

government.update!(end_date: Time.zone.today) unless government.end_date
ActiveRecord::Base.transaction do
government.update!(end_date: Time.zone.today) unless government.end_date

current_active_ministerial_appointments.each do |appointment|
appointment.ended_at = government.end_date
appointment.save!(validate: false)
current_active_ministerial_appointments.each do |appointment|
appointment.ended_at = government.end_date
appointment.save!(validate: false)
end
end

redirect_to edit_admin_government_path(government), notice: "Government closed"
Expand Down

0 comments on commit ba37433

Please sign in to comment.