diff --git a/db/migrate/20191209095548_ensures_unique_accepted_answer_post_id.rb b/db/migrate/20191209095548_ensures_unique_accepted_answer_post_id.rb new file mode 100644 index 0000000..1ae1bf9 --- /dev/null +++ b/db/migrate/20191209095548_ensures_unique_accepted_answer_post_id.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +class EnsuresUniqueAcceptedAnswerPostId < ActiveRecord::Migration[5.2] + def change + execute <<~SQL + DELETE FROM topic_custom_fields AS tcf1 + USING topic_custom_fields AS tcf2 + WHERE tcf1.id > tcf2.id AND + tcf1.topic_id = tcf2.topic_id AND + tcf1.name = tcf2.name AND + tcf1.name = 'accepted_answer_post_id' + SQL + + add_index :topic_custom_fields, + :topic_id, + name: :idx_topic_custom_fields_accepted_answer, + unique: true, + where: "name = 'accepted_answer_post_id'" + end +end