Skip to content

Commit ed168d2

Browse files
authored
FIX: Skip deleted or empty content (#211)
The backfill job which targets locales defined by `SiteSetting.automatic_translation_target_languages` is erroring out on ``` Job exception: Couldn't find Post with 'id'=1710794 [WHERE "posts"."deleted_at" IS NULL] ``` This commit makes sure we skip posts and topics that have been deleted or have empty content (e.g. cooked null)
1 parent 4a2faf9 commit ed168d2

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

app/jobs/scheduled/automatic_translation_backfill.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ def execute(args = nil)
1818
end
1919
end
2020

21-
def fetch_untranslated_model_ids(model = Post, limit = 100, target_locales = backfill_locales)
21+
def fetch_untranslated_model_ids(
22+
model,
23+
content_column,
24+
limit,
25+
target_locales = backfill_locales
26+
)
2227
m = model.name.downcase
2328
DB.query_single(<<~SQL, target_locales: target_locales, limit: limit)
2429
SELECT m.id
@@ -39,6 +44,8 @@ def fetch_untranslated_model_ids(model = Post, limit = 100, target_locales = bac
3944
ARRAY[]::text[]
4045
))
4146
)
47+
AND m.deleted_at IS NULL
48+
AND m.#{content_column} != ''
4249
ORDER BY m.id DESC
4350
LIMIT :limit
4451
SQL
@@ -92,9 +99,9 @@ def translate_records(type, record_ids)
9299
def process_batch
93100
models_translated = [Post, Topic].size
94101
translations_per_model = [translations_per_run / models_translated, 1].max
95-
topic_ids = fetch_untranslated_model_ids(Topic, translations_per_model)
102+
topic_ids = fetch_untranslated_model_ids(Topic, "title", translations_per_model)
96103
translations_per_model = translations_per_run - topic_ids.size
97-
post_ids = fetch_untranslated_model_ids(Post, translations_per_model)
104+
post_ids = fetch_untranslated_model_ids(Post, "cooked", translations_per_model)
98105
return if topic_ids.empty? && post_ids.empty?
99106

100107
translate_records(Topic, topic_ids)

spec/jobs/automatic_translation_backfill_spec.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,21 @@ def expect_google_translate(text)
177177
end
178178

179179
it "returns correct post ids needing translation in descending id" do
180-
result = described_class.new.fetch_untranslated_model_ids(Post, 50, %w[de es])
180+
result = described_class.new.fetch_untranslated_model_ids(Post, "cooked", 50, %w[de es])
181181
expect(result).to include(post_7.id, post_6.id, post_3.id, post_2.id, post_1.id)
182182
end
183+
184+
it "does not return posts that are deleted" do
185+
post_1.trash!
186+
result = described_class.new.fetch_untranslated_model_ids(Post, "cooked", 50, %w[de es])
187+
expect(result).not_to include(post_1.id)
188+
end
189+
190+
it "does not return posts that are empty" do
191+
post_1.cooked = ""
192+
post_1.save!(validate: false)
193+
result = described_class.new.fetch_untranslated_model_ids(Post, "cooked", 50, %w[de es])
194+
expect(result).not_to include(post_1.id)
195+
end
183196
end
184197
end

0 commit comments

Comments
 (0)