Skip to content

Commit

Permalink
Add an option to store record changes
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed Nov 21, 2022
1 parent d813d4c commit 161e73f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/inventory_refresh/inventory_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class InventoryCollection

attr_accessor :attributes_blacklist, :attributes_whitelist

attr_reader :model_class, :strategy, :custom_save_block, :parent, :internal_attributes, :delete_method, :dependency_attributes, :manager_ref, :create_only, :association, :complete, :update_only, :transitive_dependency_attributes, :check_changed, :arel, :inventory_object_attributes, :name, :saver_strategy, :targeted_scope, :default_values, :targeted_arel, :targeted, :manager_ref_allowed_nil, :use_ar_object, :created_records, :updated_records, :deleted_records, :retention_strategy, :custom_reconnect_block, :batch_extra_attributes, :references_storage, :unconnected_edges, :assert_graph_integrity
attr_reader :model_class, :strategy, :custom_save_block, :parent, :internal_attributes, :delete_method, :dependency_attributes, :manager_ref, :create_only, :association, :complete, :update_only, :transitive_dependency_attributes, :check_changed, :arel, :inventory_object_attributes, :name, :saver_strategy, :targeted_scope, :default_values, :targeted_arel, :targeted, :manager_ref_allowed_nil, :use_ar_object, :created_records, :updated_records, :deleted_records, :retention_strategy, :custom_reconnect_block, :batch_extra_attributes, :references_storage, :unconnected_edges, :assert_graph_integrity, :store_record_changes, :record_changes

delegate :<<,
:build,
Expand Down Expand Up @@ -149,7 +149,8 @@ def initialize(properties = {})
properties[:update_only],
properties[:use_ar_object],
properties[:targeted],
properties[:assert_graph_integrity])
properties[:assert_graph_integrity],
properties[:store_record_changes])

init_strategies(properties[:strategy],
properties[:saver_strategy],
Expand Down Expand Up @@ -214,6 +215,11 @@ def store_deleted_records(records)
@deleted_records.concat(records_identities(records))
end

def store_record_changes(records)
records = [records] unless records.respond_to?(:map)
@record_changes.concat(records.map { |r| {record_identity(r)[:id] => record.changes} })
end

# @return [Array<Symbol>] all columns that are part of the best fit unique index
def unique_index_columns
return @unique_index_columns if @unique_index_columns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,11 @@ def init_ic_relations(dependency_attributes, parent_inventory_collections = nil)
# for the batch saver strategy.
# @param targeted [Boolean] True if the collection is targeted, in that case it will be leveraging :manager_uuids
# :parent_inventory_collections and :targeted_arel to save a subgraph of a data.
# @param store_record_changes [Boolean] By default false. If true changes to the InventoryObject will be stored for use
# by other inventory collections
def init_flags(complete, create_only, check_changed,
update_only, use_ar_object, targeted,
assert_graph_integrity)
assert_graph_integrity, store_record_changes)
@complete = complete.nil? ? true : complete
@create_only = create_only.nil? ? false : create_only
@check_changed = check_changed.nil? ? true : check_changed
Expand All @@ -188,6 +190,7 @@ def init_flags(complete, create_only, check_changed,
@use_ar_object = use_ar_object || false
@targeted = !!targeted
@assert_graph_integrity = assert_graph_integrity.nil? ? true : assert_graph_integrity
@store_record_changes = store_record_changes.nil? ? false : store_record_changes
end

# @param attributes_blacklist [Array] Attributes we do not want to include into saving. We cannot blacklist an
Expand Down Expand Up @@ -390,6 +393,7 @@ def init_changed_records_stats
@created_records = []
@updated_records = []
@deleted_records = []
@record_changes = []
end

# Processes passed saver strategy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ def targeted?
targeted
end

# @return [Boolean] if true will store changes to the InventoryObject
def store_record_changes?
store_record_changes
end

# True if processing of this InventoryCollection object would lead to no operations. Then we use this marker to
# stop processing of the InventoryCollector object very soon, to avoid a lot of unnecessary Db queries, etc.
#
Expand Down
2 changes: 2 additions & 0 deletions lib/inventory_refresh/save_collection/saver/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class Default < InventoryRefresh::SaveCollection::Saver::Base
# key value
def update_record!(record, hash, inventory_object)
record.assign_attributes(hash.except(:id))

if !inventory_collection.check_changed? || record.changed?
inventory_collection.store_record_changes(record) if inventory_collection.store_record_changes?
record.save
inventory_collection.store_updated_records(record)
end
Expand Down

0 comments on commit 161e73f

Please sign in to comment.