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 <jamie@forem.com>

* 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 <jamie@forem.com>
This commit is contained in:
Daniel Uber 2021-10-26 11:56:14 -04:00 committed by GitHub
parent 0dbf7d0832
commit 39a230ffcb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 23 deletions

View file

@ -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

View file

@ -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

View file

@ -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