diff --git a/ee/app/models/gitlab_subscription.rb b/ee/app/models/gitlab_subscription.rb index 08ef366f03815164612e1b75942b146a1557b3cd..8e4702930e8f10a0e009044558874892fd4669ac 100644 --- a/ee/app/models/gitlab_subscription.rb +++ b/ee/app/models/gitlab_subscription.rb @@ -12,7 +12,7 @@ class GitlabSubscription < ApplicationRecord attribute :start_date, default: -> { Date.today } before_update :set_max_seats_used_changed_at - before_update :log_previous_state_for_update + before_update :log_previous_state_for_update, if: :tracked_attributes_changed? before_update :reset_seat_statistics before_update :publish_subscription_renewed_event @@ -200,4 +200,8 @@ def new_term? def reset_seat_statistics? new_term? || (max_seats_used_changed_at.present? && max_seats_used_changed_at.to_date < start_date) end + + def tracked_attributes_changed? + changed.intersection(GitlabSubscriptionHistory::TRACKED_ATTRIBUTES).any? + end end diff --git a/ee/spec/models/gitlab_subscription_spec.rb b/ee/spec/models/gitlab_subscription_spec.rb index 68f78972bd6b190feee17590daf2b99383e0c789..8baf88c24fa9b13b7c0944528e0d7cf3af0b6c77 100644 --- a/ee/spec/models/gitlab_subscription_spec.rb +++ b/ee/spec/models/gitlab_subscription_spec.rb @@ -596,16 +596,26 @@ ) end - it 'logs previous state to gitlab subscription history' do - gitlab_subscription.update!(max_seats_used: 32) + context 'when a tracked attribute is updated' do + it 'logs previous state to gitlab subscription history' do + gitlab_subscription.update!(max_seats_used: 32) + + expect(GitlabSubscriptionHistory.count).to eq(1) + expect(GitlabSubscriptionHistory.last.attributes).to include( + 'gitlab_subscription_id' => gitlab_subscription.id, + 'change_type' => 'gitlab_subscription_updated', + 'max_seats_used' => 42, + 'seats' => 13 + ) + end + end - expect(GitlabSubscriptionHistory.count).to eq(1) - expect(GitlabSubscriptionHistory.last.attributes).to include( - 'gitlab_subscription_id' => gitlab_subscription.id, - 'change_type' => 'gitlab_subscription_updated', - 'max_seats_used' => 42, - 'seats' => 13 - ) + context 'when tracked attributes are not updated' do + it 'does not log previous state to gitlab subscription history' do + expect do + gitlab_subscription.update!(last_seat_refresh_at: Time.current) + end.to not_change(GitlabSubscriptionHistory, :count) + end end context 'when max_seats_used has changed' do