From c0f8e2eee0df3317f16bd6d2d3345879f2888292 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Thu, 12 Dec 2019 12:00:28 +0100 Subject: [PATCH] FIX: applies a unique index for accepted_answer_post_id custom field (#83) --- ..._ensures_unique_accepted_answer_post_id.rb | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 db/migrate/20191209095548_ensures_unique_accepted_answer_post_id.rb 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