From 85175b7fc166e9b357716960bdcd31d1145f0355 Mon Sep 17 00:00:00 2001 From: Nat Date: Fri, 14 Feb 2025 14:51:24 +0800 Subject: [PATCH] FIX: Skip deleted or empty content --- .../scheduled/automatic_translation_backfill.rb | 13 ++++++++++--- spec/jobs/automatic_translation_backfill_spec.rb | 15 ++++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/app/jobs/scheduled/automatic_translation_backfill.rb b/app/jobs/scheduled/automatic_translation_backfill.rb index cb412d60..538a0040 100644 --- a/app/jobs/scheduled/automatic_translation_backfill.rb +++ b/app/jobs/scheduled/automatic_translation_backfill.rb @@ -18,7 +18,12 @@ def execute(args = nil) end end - def fetch_untranslated_model_ids(model = Post, limit = 100, target_locales = backfill_locales) + def fetch_untranslated_model_ids( + model, + content_column, + limit, + target_locales = backfill_locales + ) m = model.name.downcase DB.query_single(<<~SQL, target_locales: target_locales, limit: limit) SELECT m.id @@ -39,6 +44,8 @@ def fetch_untranslated_model_ids(model = Post, limit = 100, target_locales = bac ARRAY[]::text[] )) ) + AND m.deleted_at IS NULL + AND m.#{content_column} != '' ORDER BY m.id DESC LIMIT :limit SQL @@ -92,9 +99,9 @@ def translate_records(type, record_ids) def process_batch models_translated = [Post, Topic].size translations_per_model = [translations_per_run / models_translated, 1].max - topic_ids = fetch_untranslated_model_ids(Topic, translations_per_model) + topic_ids = fetch_untranslated_model_ids(Topic, "title", translations_per_model) translations_per_model = translations_per_run - topic_ids.size - post_ids = fetch_untranslated_model_ids(Post, translations_per_model) + post_ids = fetch_untranslated_model_ids(Post, "cooked", translations_per_model) return if topic_ids.empty? && post_ids.empty? translate_records(Topic, topic_ids) diff --git a/spec/jobs/automatic_translation_backfill_spec.rb b/spec/jobs/automatic_translation_backfill_spec.rb index 25ccbb77..83b1115b 100644 --- a/spec/jobs/automatic_translation_backfill_spec.rb +++ b/spec/jobs/automatic_translation_backfill_spec.rb @@ -177,8 +177,21 @@ def expect_google_translate(text) end it "returns correct post ids needing translation in descending id" do - result = described_class.new.fetch_untranslated_model_ids(Post, 50, %w[de es]) + result = described_class.new.fetch_untranslated_model_ids(Post, "cooked", 50, %w[de es]) expect(result).to include(post_7.id, post_6.id, post_3.id, post_2.id, post_1.id) end + + it "does not return posts that are deleted" do + post_1.trash! + result = described_class.new.fetch_untranslated_model_ids(Post, "cooked", 50, %w[de es]) + expect(result).not_to include(post_1.id) + end + + it "does not return posts that are empty" do + post_1.cooked = "" + post_1.save!(validate: false) + result = described_class.new.fetch_untranslated_model_ids(Post, "cooked", 50, %w[de es]) + expect(result).not_to include(post_1.id) + end end end