-
Notifications
You must be signed in to change notification settings - Fork 897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Store created updated and deleted records #15603
Merged
agrare
merged 7 commits into
ManageIQ:master
from
Ladas:store_created_updated_and_deleted_records
Jul 19, 2017
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bfcdaca
Allow IC to store what was created/updated/deleted
Ladas 4bc5a94
Unify default and concurrent_safe savers and store counts
Ladas d08f793
Store counts for concurrent safe batch saver
Ladas 71a20a3
Test that we correctly store what was created/updated/deleted in IC
Ladas 01cbe8e
Move store_created_records code after records creation
Ladas 5011628
Enforce that identity of the create/updated/deleted record is present
Ladas 42f74aa
Correct storing of deleted and updated records for a batch strategy
Ladas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,55 +3,6 @@ module Saver | |
class ConcurrentSafe < ManagerRefresh::SaveCollection::Saver::Base | ||
private | ||
|
||
def save!(inventory_collection, association) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 for consolidating the save! methods now |
||
attributes_index = {} | ||
inventory_objects_index = {} | ||
inventory_collection.each do |inventory_object| | ||
attributes = inventory_object.attributes(inventory_collection) | ||
index = inventory_object.manager_uuid | ||
|
||
attributes_index[index] = attributes | ||
inventory_objects_index[index] = inventory_object | ||
end | ||
|
||
inventory_collection_size = inventory_collection.size | ||
deleted_counter = 0 | ||
created_counter = 0 | ||
_log.info("*************** PROCESSING #{inventory_collection} of size #{inventory_collection_size} *************") | ||
# Records that are in the DB, we will be updating or deleting them. | ||
ActiveRecord::Base.transaction do | ||
association.find_each do |record| | ||
next unless assert_distinct_relation(record) | ||
|
||
index = inventory_collection.object_index_with_keys(unique_index_keys, record) | ||
|
||
inventory_object = inventory_objects_index.delete(index) | ||
hash = attributes_index.delete(index) | ||
|
||
if inventory_object.nil? | ||
# Record was found in the DB but not sent for saving, that means it doesn't exist anymore and we should | ||
# delete it from the DB. | ||
deleted_counter += 1 if delete_record!(inventory_collection, record) | ||
else | ||
# Record was found in the DB and sent for saving, we will be updating the DB. | ||
update_record!(inventory_collection, record, hash, inventory_object) | ||
end | ||
end | ||
end | ||
|
||
# Records that were not found in the DB but sent for saving, we will be creating these in the DB. | ||
if inventory_collection.create_allowed? | ||
ActiveRecord::Base.transaction do | ||
inventory_objects_index.each do |index, inventory_object| | ||
create_record!(inventory_collection, attributes_index.delete(index), inventory_object) | ||
created_counter += 1 | ||
end | ||
end | ||
end | ||
_log.info("*************** PROCESSED #{inventory_collection}, created=#{created_counter}, "\ | ||
"updated=#{inventory_collection_size - created_counter}, deleted=#{deleted_counter} *************") | ||
end | ||
|
||
def update_record!(inventory_collection, record, hash, inventory_object) | ||
assign_attributes_for_update!(hash, inventory_collection, time_now) | ||
record.assign_attributes(hash.except(:id, :type)) | ||
|
@@ -64,14 +15,13 @@ def update_record!(inventory_collection, record, hash, inventory_object) | |
end | ||
|
||
update_query.update_all(hash) | ||
inventory_collection.store_updated_records(record) | ||
end | ||
|
||
inventory_object.id = record.id | ||
end | ||
|
||
def create_record!(inventory_collection, hash, inventory_object) | ||
return unless assert_referential_integrity(hash, inventory_object) | ||
|
||
all_attribute_keys = hash.keys | ||
hash = inventory_collection.model_class.new(hash).attributes.symbolize_keys | ||
assign_attributes_for_create!(hash, inventory_collection, time_now) | ||
|
@@ -80,6 +30,7 @@ def create_record!(inventory_collection, hash, inventory_object) | |
build_insert_query(inventory_collection, all_attribute_keys, [hash]) | ||
) | ||
inventory_object.id = result_id | ||
inventory_collection.store_created_records(inventory_object) | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks overly complicated, it seems like the caller has a better idea of how to get the
id
out of what its storing than this. Would it make more sense to pass in theid
for the created/updated/deleted records?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm, I would rather have it here, since I can later easily add more params, like manager_ref