Skip to content

Commit

Permalink
refactor license user seed script to use batches
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Dec 13, 2023
1 parent fe2b514 commit c911884
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions db/scripts/seed_license_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@
# heroku run:detached -e SLEEP_DURATION=0.5 --tail \
# rails runner db/scripts/seed_license_users.rb

BATCH_SIZE = ENV.fetch('BATCH_SIZE') { 1_000 }.to_i
SLEEP_DURATION = ENV.fetch('SLEEP_DURATION') { 1 }.to_f

licenses = License.where_assoc_not_exists(:license_users)
.where_assoc_exists(:user)
.includes(:user)
licenses_with_users = License.where_assoc_not_exists(:license_users)
.where_assoc_exists(:user)
.eager_load(
:account,
:user,
)

Rails.logger.info "Seeding #{licenses.count} license_users"
Rails.logger.info "Seeding license_users for #{licenses_with_users.count} total licenses"

licenses.find_each do |license|
Rails.logger.info "Seeding license_user for license=#{license.id} <> user=#{license.user_id}"
licenses_with_users.find_in_batches(batch_size: BATCH_SIZE) do |licenses|
Rails.logger.info "Seeding batch of #{licenses.count} licenses"

license.users << license.user
licenses.each do |license|
Rails.logger.info "Seeding license_user for license=#{license.id} <> user=#{license.user_id}"

license.users << license.user
end

sleep SLEEP_DURATION
end
Expand Down

0 comments on commit c911884

Please sign in to comment.