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

CCOL-2440: Add consumer configuration to save associated keys prior to saving primary record #217

Merged
merged 4 commits into from
May 9, 2024

Conversation

lionelpereira
Copy link
Collaborator

@lionelpereira lionelpereira commented May 7, 2024

Pull Request Template

Description

Normal bulk consumption logic:

Widget:
has_one : detail
  1. Record class records (Widget) are saved
  2. Record class primary keys are filled from DB
  3. Associated records (Details) are updated with foreign key from record class

Backfill consumption logic (this change)

FidgetWidget
belongs_to: widget
belong_to: fidget

Notes:

  • Record class cannot be upserted first as foreign_keys widget_id, fidget_id are required
  1. Associated records (Widget & Fidget) are created and linked to record class (FidgetWidget)
  2. Associated records are saved
  3. Primary keys (widget_id, fidget_id) of associated records are filled from DB
  4. Foreign keys of record class are filled with newly saved primary keys of associated records
  • 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?

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

  • Test A
  • Test B

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

@dorner
Copy link
Member

dorner commented May 8, 2024

@lionelpereira can you provide some more information in this PR as to what the use case is here and why it isn't covered by existing functionality?

@lionelpereira
Copy link
Collaborator Author

@lionelpereira can you provide some more information in this PR as to what the use case is here and why it isn't covered by existing functionality?

@dorner Updated the PR description

@lionelpereira lionelpereira requested a review from dorner May 8, 2024 17:34
@@ -476,6 +477,8 @@ def self.configure_producer_or_consumer(kafka_config)
# @return [Block]
setting :bulk_import_id_generator, nil

setting :backfill_associations, false
Copy link
Member

Choose a reason for hiding this comment

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

YARD comment please.

I'm not sure I like the naming of this... You're actually backfilling the main record with the association IDs. Maybe something as simple as save_associations_first?

associations = {}
record_list.associations.each do |assoc|
col = @bulk_import_id_column if assoc.klass.column_names.include?(@bulk_import_id_column)
associations[[assoc.name, assoc.klass, col, assoc.foreign_key]] = []
Copy link
Member

Choose a reason for hiding this comment

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

Feels like you should be able to just use [assoc, col] and reference what you need out of the association further down?

Copy link
Member

Choose a reason for hiding this comment

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

Also you don't need to do this up-front - just change line 93 to associations = Hash.new([]) which will put an empty array as the default for any key instead of nil.

# Associate this associated batch record's record with the primary record to
# retrieve foreign_key after associated records have been saved and primary
# keys have been filled
primary_batch_record.record.send(:"#{assoc}=", batch_record.record)
Copy link
Member

Choose a reason for hiding this comment

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

Pretty sure you can do primary_batch_record.record[assoc] = batch_record.record

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 was getting a NoMethodError when I tried this

case klass.to_s
when 'Widget', 'Fidget'
%w(id)
when 'WidgetFidgets'
Copy link
Member

Choose a reason for hiding this comment

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

Why is this the only one that's plural?

record_list.associations.each do |assoc|
col = @bulk_import_id_column if assoc.klass.column_names.include?(@bulk_import_id_column)
associations[[assoc, col]] = []
end
Copy link
Member

Choose a reason for hiding this comment

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

You should be able to remove lines 95-98.

Copy link
Collaborator Author

@lionelpereira lionelpereira May 9, 2024

Choose a reason for hiding this comment

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

I would still need these lines to determine the bulk_import_column instead of checking for each batch record

Copy link
Member

Choose a reason for hiding this comment

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

Ah - didn't make the connection between this and line 100. -_- Maybe rename associations to something else - it's mirroring record.associations but isn't actually recording the same thing.

In that case there's no need to use Hash.new([]).

@@ -84,15 +87,60 @@ def import_associations(record_list)
end
end

# rubocop:disable Metrics/AbcSize
Copy link
Member

Choose a reason for hiding this comment

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

Let's not disable this - if it's complaining, the function is too big. Let's break it up.

@lionelpereira lionelpereira requested a review from dorner May 9, 2024 14:06
Copy link
Member

@dorner dorner left a comment

Choose a reason for hiding this comment

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

I think this works!

@dorner dorner merged commit 98e478f into master May 9, 2024
5 of 6 checks passed
@dorner dorner deleted the CCOL-2440 branch May 9, 2024 14:10
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