FIX: Periodically ensure consistency of categories. (#7663)

This commit is contained in:
Bianca Nenciu 2019-06-06 12:30:52 +03:00 committed by Régis Hanol
parent c131903e56
commit f63b8bb79d
4 changed files with 29 additions and 1 deletions

View File

@ -21,6 +21,7 @@ module Jobs
CategoryTagStat.ensure_consistency! CategoryTagStat.ensure_consistency!
User.ensure_consistency! User.ensure_consistency!
UserAvatar.ensure_consistency! UserAvatar.ensure_consistency!
Category.ensure_consistency!
end end
end end
end end

View File

@ -664,6 +664,15 @@ class Category < ActiveRecord::Base
end end
end end
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
private private
def check_permissions_compatibility(parent_permissions, child_permissions) def check_permissions_compatibility(parent_permissions, child_permissions)

View File

@ -428,7 +428,7 @@ end
def create_category_definitions def create_category_definitions
log "Creating category definitions" log "Creating category definitions"
Category.where(topic_id: nil).each(&:create_category_definition) Category.ensure_consistency!
end end
def log(message) def log(message)

View File

@ -890,4 +890,22 @@ describe Category do
end end
end end
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 end