From bce3b1ae0d3629daabb95835776bafe942fc8e6c Mon Sep 17 00:00:00 2001 From: Echo Date: Sat, 15 Nov 2025 18:11:00 -0500 Subject: [PATCH] wrap key rotation in a transaction (#626) --- app/controllers/users_controller.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 0a6bb95..ddaeabc 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -37,11 +37,13 @@ class UsersController < ApplicationController end def rotate_api_key - @user.api_keys.destroy_all + @user.api_keys.transaction do + @user.api_keys.destroy_all - new_api_key = @user.api_keys.create!(name: "Hackatime key") + new_api_key = @user.api_keys.create!(name: "Hackatime key") - render json: { token: new_api_key.token }, status: :ok + render json: { token: new_api_key.token }, status: :ok + end rescue => e Rails.logger.error("error rotate #{e.class.name} #{e.message}") render json: { error: "cant rotate" }, status: :unprocessable_entity