From 39a230ffcbeaf7abe2049f9a543119327a108610 Mon Sep 17 00:00:00 2001 From: Daniel Uber Date: Tue, 26 Oct 2021 11:56:14 -0400 Subject: [PATCH] Add reaction to articles when seeding the database (#15193) * Add one reaction to each article when seeding the database * Add reaction to article when created and sync reactions count. There's still a gap where the redis-cached reaction count could be present but not cleared during seeder runs (redis is disconnected for caching and rails cache is set to the null store). Since we are able to enqueue sidekiq jobs redis is live - but it's a bad idea to use sidekiq's connection to flush keys in redis. * Make the reaction count update script a no-op * Remove unused data update script and limit user id queries Pull all user ids into an array before you start creating articles, and sample from the array rather than repeatedly asking for the first user from a random ordered db result. * fail if we can't create reaction thanks @jgaskins Co-authored-by: Jamie Gaskins * Revert "fail if we can't create reaction" This reverts commit c35f6d47d2e8956a240133b9e6ad0d144f1b4722. There is a uniqueness constraint on (user, reactable, category) that could be triggered when seeding due to random selection. It's better to skip creation when a validation error occurs than to break the seed completely. There are expected to be 0-9 reactions per article, we don't require a set number on each article, and likely won't notice any specific problem if the random number is one lower than it would have been because some reactions were skipped due to uniqueness violations. Co-authored-by: Jamie Gaskins --- db/seeds.rb | 14 +++++++++++- ...850_update_public_reaction_counts_again.rb | 22 ------------------- ...02046_remove_unused_data_update_scripts.rb | 1 + 3 files changed, 14 insertions(+), 23 deletions(-) delete mode 100644 lib/data_update_scripts/20200526181850_update_public_reaction_counts_again.rb diff --git a/db/seeds.rb b/db/seeds.rb index 087de5922..76f0710c0 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -169,6 +169,9 @@ end num_articles = 25 * SEEDS_MULTIPLIER seeder.create_if_none(Article, num_articles) do + user_ids = User.all.pluck(:id) + public_categories = %w[like unicorn] + num_articles.times do |i| tags = [] tags << "discuss" if (i % 3).zero? @@ -187,12 +190,21 @@ seeder.create_if_none(Article, num_articles) do #{Faker::Hipster.paragraph(sentence_count: 2)} MARKDOWN - Article.create!( + article = Article.create!( body_markdown: markdown, featured: true, show_comments: true, user_id: User.order(Arel.sql("RANDOM()")).first.id, ) + + Random.random_number(10).times do |t| + article.reactions.create( + user_id: user_ids.sample, + category: public_categories.sample, + ) + end + + article.sync_reactions_count end end diff --git a/lib/data_update_scripts/20200526181850_update_public_reaction_counts_again.rb b/lib/data_update_scripts/20200526181850_update_public_reaction_counts_again.rb deleted file mode 100644 index 7bccf2b60..000000000 --- a/lib/data_update_scripts/20200526181850_update_public_reaction_counts_again.rb +++ /dev/null @@ -1,22 +0,0 @@ -module DataUpdateScripts - class UpdatePublicReactionCountsAgain - def run - article_count = Article.count - comment_count = Comment.count - ActiveRecord::Base.connection.execute( - "UPDATE articles SET public_reactions_count = positive_reactions_count, previous_public_reactions_count = " \ - "previous_positive_reactions_count WHERE id <= #{article_count / 2}", - ) - ActiveRecord::Base.connection.execute( - "UPDATE articles SET public_reactions_count = positive_reactions_count, previous_public_reactions_count = " \ - "previous_positive_reactions_count WHERE id > #{article_count / 2}", - ) - ActiveRecord::Base.connection.execute( - "UPDATE comments SET public_reactions_count = positive_reactions_count WHERE id <= #{comment_count / 2}", - ) - ActiveRecord::Base.connection.execute( - "UPDATE comments SET public_reactions_count = positive_reactions_count WHERE id > #{comment_count / 2}", - ) - end - end -end diff --git a/lib/data_update_scripts/20210503202046_remove_unused_data_update_scripts.rb b/lib/data_update_scripts/20210503202046_remove_unused_data_update_scripts.rb index 8745ee9db..dd6e1eaf1 100644 --- a/lib/data_update_scripts/20210503202046_remove_unused_data_update_scripts.rb +++ b/lib/data_update_scripts/20210503202046_remove_unused_data_update_scripts.rb @@ -13,6 +13,7 @@ module DataUpdateScripts 20200415200651_index_reading_list_reactions 20200518173504_update_public_reactions_count_from_positive_reactions_count 20200519142908_re_index_feed_content_and_users_to_elasticsearch + 20200526181850_update_public_reaction_counts_again 20200729120730_remove_orphaned_ahoy_events 20200803142830_reindex_listing_search_column 20200805171911_clean_up_language_settings