A platform for community discussion. Free, open, simple.
FIX: Periodically ensure consistency of categories. (#7663)
Bianca Nenciuf63b8bb79d7c
diff --git a/app/jobs/scheduled/ensure_db_consistency.rb b/app/jobs/scheduled/ensure_db_consistency.rbindex de9f4e8cb67..dd85fa0b537 100644--- a/app/jobs/scheduled/ensure_db_consistency.rb+++ b/app/jobs/scheduled/ensure_db_consistency.rb@@ -21,6 +21,7 @@ module JobsCategoryTagStat.ensure_consistency!User.ensure_consistency!UserAvatar.ensure_consistency!+ Category.ensure_consistency!endendenddiff --git a/app/models/category.rb b/app/models/category.rbindex 096ed96aba3..6dc5530e2b4 100644--- a/app/models/category.rb+++ b/app/models/category.rb@@ -664,6 +664,15 @@ class Category < ActiveRecord::Baseendend+ def self.ensure_consistency!+ Category+ .joins('LEFT JOIN topics ON categories.topic_id = topics.id AND topics.deleted_at IS NULL')+ .where({ topics: { id: nil }})+ .find_each do |category|+ category.create_category_definition+ end+ end+privatedef check_permissions_compatibility(parent_permissions, child_permissions)diff --git a/lib/tasks/import.rake b/lib/tasks/import.rakeindex e54ed4260df..92c4b90a558 100644--- a/lib/tasks/import.rake+++ b/lib/tasks/import.rake@@ -428,7 +428,7 @@ enddef create_category_definitionslog "Creating category definitions"- Category.where(topic_id: nil).each(&:create_category_definition)+ Category.ensure_consistency!enddef log(message)diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rbindex 39bce54d188..5746d396311 100644--- a/spec/models/category_spec.rb+++ b/spec/models/category_spec.rb@@ -890,4 +890,22 @@ describe Category doendend+ describe "#ensure_consistency!" do+ it "creates category topic" do+ category = Fabricate(:category)+ category_destroyed = Fabricate(:category)+ category_trashed = Fabricate(:category)++ category_topic_id = category.topic.id+ category_destroyed.topic.destroy!+ category_trashed.topic.trash!++ Category.ensure_consistency!++ expect(category.reload.topic_id).to eq(category_topic_id)+ expect(category_destroyed.reload.topic).to_not eq(nil)+ expect(category_trashed.reload.topic).to_not eq(nil)+ end+ end+end