DEV: Avoid nil custom fields (#128)

This commit is contained in:
Jarek Radosz 2021-05-05 12:19:00 +02:00 committed by GitHub
parent 8a2fdaf063
commit ce63d80abb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 4 deletions

View File

@ -0,0 +1,24 @@
# frozen_string_literal: true
class RemoveNilCustomFieldsFromSolved < ActiveRecord::Migration[6.0]
def up
execute <<~SQL
DELETE FROM post_custom_fields
WHERE name = 'is_accepted_answer' AND value IS NULL
SQL
execute <<~SQL
DELETE FROM topic_custom_fields
WHERE name = 'accepted_answer_post_id' AND value IS NULL
SQL
execute <<~SQL
DELETE FROM topic_custom_fields
WHERE name = 'solved_auto_close_topic_timer_id' AND value IS NULL
SQL
end
def down
raise ActiveRecord::IrreversibleMigration
end
end

View File

@ -83,7 +83,7 @@ SQL
if accepted_id > 0 if accepted_id > 0
if p2 = Post.find_by(id: accepted_id) if p2 = Post.find_by(id: accepted_id)
p2.custom_fields["is_accepted_answer"] = nil p2.custom_fields.delete("is_accepted_answer")
p2.save! p2.save!
if defined?(UserAction::SOLVED) if defined?(UserAction::SOLVED)
@ -168,13 +168,13 @@ SQL
topic ||= post.topic topic ||= post.topic
DistributedMutex.synchronize("discourse_solved_toggle_answer_#{topic.id}") do DistributedMutex.synchronize("discourse_solved_toggle_answer_#{topic.id}") do
post.custom_fields["is_accepted_answer"] = nil post.custom_fields.delete("is_accepted_answer")
topic.custom_fields["accepted_answer_post_id"] = nil topic.custom_fields.delete("accepted_answer_post_id")
if timer_id = topic.custom_fields[AUTO_CLOSE_TOPIC_TIMER_CUSTOM_FIELD] if timer_id = topic.custom_fields[AUTO_CLOSE_TOPIC_TIMER_CUSTOM_FIELD]
topic_timer = TopicTimer.find_by(id: timer_id) topic_timer = TopicTimer.find_by(id: timer_id)
topic_timer.destroy! if topic_timer topic_timer.destroy! if topic_timer
topic.custom_fields[AUTO_CLOSE_TOPIC_TIMER_CUSTOM_FIELD] = nil topic.custom_fields.delete(AUTO_CLOSE_TOPIC_TIMER_CUSTOM_FIELD)
end end
topic.save! topic.save!