Short-circuiting parsing any articles to update (#16413)

* Short-circuiting parsing any articles to update

Setting a field that is to help track from where we received an image.

Related to #16075 and #16407.

See [Slack Thread][1] for all the details

[1]:https://forem-team.slack.com/archives/CSY5KKK9U/p1643916711077719

* Fix

* Apply suggestions from code review

Co-authored-by: Jamie Gaskins <jgaskins@hey.com>

Co-authored-by: Jamie Gaskins <jgaskins@hey.com>
This commit is contained in:
Jeremy Friesen 2022-02-04 11:13:01 -05:00 committed by GitHub
parent 765df66084
commit 75ae759465
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 19 deletions

View file

@ -1,16 +0,0 @@
module DataUpdateScripts
class UpdateArticleCachedAttributeAroundMainImage
# @note This is a re-runniing of an earlier version of
# `lib/data_update_scripts/20220120052641_backfill_column_main_image_from_frontmatter.rb`
# @see https://github.com/forem/forem/blob/9a11beec50f93dee1bbd58331028236205026084/lib/data_update_scripts/20220120052641_backfill_column_main_image_from_frontmatter.rb
# for original version.
def run
Article.where(main_image_from_frontmatter: false).find_each do |article|
fixed_body_markdown = MarkdownProcessor::Fixer::FixAll.call(article.body_markdown || "")
parsed = FrontMatterParser::Parser.new(:md).call(fixed_body_markdown)
article.update_column(:main_image_from_frontmatter, true) if parsed.front_matter&.key?("cover_image")
end
end
end
end

View file

@ -0,0 +1,21 @@
module DataUpdateScripts
class UpdateArticleFlagWithoutParsingArticle
def run
# Query cribbed from https://dev.to/admin/blazer/queries/545-articles-containing-a-given-string-in-the-markdown?substring=cover_image%3A+
Article.where(main_image_from_frontmatter: false).find_each do |article|
has_cover_image = false
begin
fixed_body_markdown = MarkdownProcessor::Fixer::FixAll.call(article.body_markdown || "")
parsed = FrontMatterParser::Parser.new(:md).call(fixed_body_markdown)
has_cover_image = parsed.front_matter.key?("cover_image")
rescue StandardError
# Piping this to /dev/null, because we can't assume this article is processible in our
# current application state
ForemStatsClient.increment "dus.update_article_flag_without_parsing_article.errors"
end
article.update_column(:main_image_from_frontmatter, true) if has_cover_image
end
end
end
end

View file

@ -1,9 +1,9 @@
require "rails_helper"
require Rails.root.join(
"lib/data_update_scripts/20220203165540_update_article_cached_attribute_around_main_image.rb",
"lib/data_update_scripts/20220203194510_update_article_flag_without_parsing_article.rb",
)
RSpec.describe DataUpdateScripts::UpdateArticleCachedAttributeAroundMainImage do
RSpec.describe DataUpdateScripts::UpdateArticleFlagWithoutParsingArticle do
let(:article_without_image) { create(:article, with_main_image: false) }
let(:article_main_image) { create(:article, with_main_image: true) }
let(:article_markdown_image) do
@ -15,7 +15,7 @@ RSpec.describe DataUpdateScripts::UpdateArticleCachedAttributeAroundMainImage do
article_without_image
article_main_image
# Need to do the following for the script to even run.
article_markdown_image.update_column(:main_image_from_frontmatter, false)
article_markdown_image.update_columns(main_image_from_frontmatter: false, main_image: "https://awesome.com/image.url")
end
it "set main_image_from_frontmatter to true only for articles with cover_image in body_markdown" do