Bypassing validiation for data script (#16407)

* Bypassing validiation for data script

Prior to this commit, we attempted to run a script against all articles
using validation.  In the case of DEV, we have lots of articles that
would no longer validate.  This change now updates the column value
without running validation.

Relates to #16075

* Bump for travis
This commit is contained in:
Jeremy Friesen 2022-02-03 13:29:14 -05:00 committed by GitHub
parent c372d0a8f7
commit c17fa11875
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 14 deletions

View file

@ -1,11 +0,0 @@
module DataUpdateScripts
class BackfillColumnMainImageFromFrontmatter
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!(main_image_from_frontmatter: true) if parsed.front_matter&.key?("cover_image")
end
end
end
end

View file

@ -0,0 +1,16 @@
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

@ -1,9 +1,9 @@
require "rails_helper"
require Rails.root.join(
"lib/data_update_scripts/20220120052641_backfill_column_main_image_from_frontmatter.rb",
"lib/data_update_scripts/20220203165540_update_article_cached_attribute_around_main_image.rb",
)
describe DataUpdateScripts::BackfillColumnMainImageFromFrontmatter do
RSpec.describe DataUpdateScripts::UpdateArticleCachedAttributeAroundMainImage 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
@ -14,11 +14,13 @@ describe DataUpdateScripts::BackfillColumnMainImageFromFrontmatter do
before do
article_without_image
article_main_image
article_markdown_image
# Need to do the following for the script to even run.
article_markdown_image.update_column(:main_image_from_frontmatter, false)
end
it "set main_image_from_frontmatter to true only for articles with cover_image in body_markdown" do
described_class.new.run
expect(article_without_image.reload.main_image_from_frontmatter).to be false
expect(article_main_image.reload.main_image_from_frontmatter).to be false
expect(article_markdown_image.reload.main_image_from_frontmatter).to be true