Skip to content

Commit

Permalink
transaction_changes
Browse files Browse the repository at this point in the history
  • Loading branch information
goodviber committed Jan 6, 2025
1 parent a9bccc1 commit 6a4befb
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/dfe/analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
require 'dfe/analytics/big_query_api'
require 'dfe/analytics/big_query_legacy_api'
require 'dfe/analytics/azure_federated_auth'
require 'dfe/analytics/transaction_changes'

module DfE
module Analytics
Expand Down
99 changes: 99 additions & 0 deletions lib/dfe/analytics/transaction_changes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# frozen_string_literal: true

require 'active_record'

# This module provides functionality to track changes to ActiveRecord attributes
# within a database transaction. It ensures that changes are tracked and reset
# appropriately during commit and rollback operations.

module DfE
module Analytics
module TransactionChanges
def _run_commit_callbacks
super
ensure
@transaction_changed_attributes = nil
end

def _run_rollback_callbacks
super
ensure
@transaction_changed_attributes = nil
end

def transaction_changed_attributes
@transaction_changed_attributes ||= HashWithIndifferentAccess.new
end

def _write_attribute(attr_name, value)
_store_transaction_changed_attributes(attr_name) do
super(attr_name, value)
end
end

if ActiveRecord.version >= Gem::Version.new('6.1.0.alpha')
def write_attribute(attr_name, value)
_store_transaction_changed_attributes(attr_name) do
super(attr_name, value)
end
end
end

def attribute_will_change!(attr_name)
unless transaction_changed_attributes.key?(attr_name)
value = _read_attribute_for_transaction(attr_name)
value = _deserialize_transaction_change_value(attr_name, value)
transaction_changed_attributes[attr_name] = value
end
super
end

private

def init_internals
super
@transaction_changed_attributes = nil
end

def _deserialize_transaction_change_value(attr_name, value)
attribute = @attributes[attr_name]
return value unless attribute.type.is_a?(::ActiveRecord::Type::Serialized)

attribute.type.deserialize(value)
end

def _store_transaction_changed_attributes(attr_name)
attr_name = attr_name.to_s
old_value = _read_attribute_for_transaction(attr_name)
ret = yield
new_value = _read_attribute_for_transaction(attr_name)
if !transaction_changed_attributes.key?(attr_name) && new_value != old_value
transaction_changed_attributes[attr_name] = _deserialize_transaction_change_value(attr_name, old_value)
elsif transaction_changed_attributes.key?(attr_name)
new_value = _deserialize_transaction_change_value(attr_name, new_value)

stored_value = transaction_changed_attributes[attr_name]

transaction_changed_attributes.delete(attr_name) if new_value == stored_value
end
ret
end

def _read_attribute_for_transaction(attr_name)
attribute = @attributes[attr_name]
# Avoid causing an earlier memoized type cast of mutable serialized user values,
# since could prevent mutations of that user value from affecting the attribute value
# that would affect it without using this library.
if attribute.type.is_a?(::ActiveRecord::Type::Serialized)
if attribute.came_from_user?
attribute.type.serialize(attribute.value_before_type_cast)
else
attribute.value_before_type_cast
end
else
_read_attribute(attr_name)
end
end
end
end
end

0 comments on commit 6a4befb

Please sign in to comment.