Skip to content
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

post_process method outside batch consumption DB transaction #201

Closed
wants to merge 1 commit into from

Conversation

harsha-flipp
Copy link
Collaborator

@harsha-flipp harsha-flipp commented Aug 24, 2023

Pull Request Template

Description

After batch consumption transaction is complete(BULK INSERTs), previously nothing is returned. Now we are passing the active record objects created back to consume_batch method so these records objects can be used by post_process hook method. Consumers can override this to do an action on the Active Record Models.

Note that these active record models are used by ActiveRecord-Import gem to bulk insert data. So record IDs are populated only if there is association involved.

Usecase: After saving records to Database, I queue a sidekiq job with record IDs.
Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

I ran the unit tests locally and this code change adds an empty post_process method in consumer. So it should not hurt any one upgrading to this version. For a meaningful use, they will have to override post_process in their consumers.

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Added an RSpec to note that the records are passed to post_process method
  • Couldnot add an RSpec to try both the branches of compacted and uncompacted messages. But I tried it locally by setting the parameter on consumer class.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added a line in the CHANGELOG describing this change, under the UNRELEASED heading
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@harsha-flipp harsha-flipp changed the title Ccol 2132 post_process method outside batch consumption DB transaction Aug 24, 2023

def post_process(records)
# ActiveRecord objects that have been used to save to the database.
# They contain primary keys only if associations are involved.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wording is a bit awkward... maybe

This method gets passed the ActiveRecord objects that were saved to the database.
They contain primary keys only if associations were also saved via bulk_import_id.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have been used to I am sorry I should not flex my grammar. I will use your words.

I wanted to give the intent that these are not the active records returned after save. These are the records we created and passing to save the record.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But... aren't they the same thing?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I misunderstood that rails adds IDs back to the objects for every save.

I tested in rails console and attribute ID is saved to the object only whenobject.save! is called. For other scenarios, object.save doesnot really change the object attribute ID.

def uncompacted_update(messages)
BatchSlicer.
slice(messages).
each(&method(:update_database))
each(&method(:update_database)).flatten
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should use map instead of each here since we're using the return value.

expect(batch_consumer).to receive(:post_process).once.with(records)
expect(batch_consumer).to receive(:update_database) { records }
batch_consumer.send(:consume_batch, payload, metadata)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we're probably missing some more tests - e.g. with and without max_db_batch_size, and with and without deletions. Probably can just add another expect onto existing tests showing that they return the expected values.

@harsha-flipp
Copy link
Collaborator Author

Lionel tackled with these changes in his PR. I am closing this.

@harsha-flipp harsha-flipp deleted the CCOL-2132 branch December 19, 2023 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants