迁移调整

dev_forge
cxt 5 years ago
parent 32e0b8865a
commit 6938639d0d

@ -1,16 +1,16 @@
class AddUniqIndexToPollVotes < ActiveRecord::Migration[5.2] class AddUniqIndexToPollVotes < ActiveRecord::Migration[5.2]
def change def change
remove_index :poll_votes, column: [:poll_question_id, :user_id] # remove_index :poll_votes, column: [:poll_question_id, :user_id]
#
change_column_default :poll_votes, :poll_question_id, from: nil, to: -1 # change_column_default :poll_votes, :poll_question_id, from: nil, to: -1
PollVote.where(poll_answer_id: nil).update_all(poll_answer_id: -1) # PollVote.where(poll_answer_id: nil).update_all(poll_answer_id: -1)
#
sql = %Q(delete from poll_votes where (poll_question_id, user_id, poll_answer_id) in # sql = %Q(delete from poll_votes where (poll_question_id, user_id, poll_answer_id) in
(select * from (select poll_question_id, user_id, poll_answer_id from poll_votes group by poll_question_id, user_id, poll_answer_id having count(*) > 1) a) # (select * from (select poll_question_id, user_id, poll_answer_id from poll_votes group by poll_question_id, user_id, poll_answer_id having count(*) > 1) a)
and id not in (select * from (select min(id) from poll_votes group by poll_question_id, user_id, poll_answer_id having count(*) > 1 order by id) b)) # and id not in (select * from (select min(id) from poll_votes group by poll_question_id, user_id, poll_answer_id having count(*) > 1 order by id) b))
ActiveRecord::Base.connection.execute sql # ActiveRecord::Base.connection.execute sql
#
add_index :poll_votes, [:poll_question_id, :user_id, :poll_answer_id], name: 'poll_answer_index', unique: true # add_index :poll_votes, [:poll_question_id, :user_id, :poll_answer_id], name: 'poll_answer_index', unique: true
end end
end end

@ -1,15 +1,15 @@
class AddIndexToPollAnswer < ActiveRecord::Migration[5.2] class AddIndexToPollAnswer < ActiveRecord::Migration[5.2]
def change def change
# remove_index :poll_votes, column: [:poll_question_id, :user_id] remove_index :poll_votes, column: [:poll_question_id, :user_id]
#
# change_column_default :poll_votes, :poll_question_id, from: nil, to: -1 change_column_default :poll_votes, :poll_question_id, from: nil, to: -1
# PollVote.where(poll_answer_id: nil).update_all(poll_answer_id: -1) PollVote.where(poll_answer_id: nil).update_all(poll_answer_id: -1)
#
# sql = %Q(delete from poll_votes where (poll_question_id, user_id, poll_answer_id) in sql = %Q(delete from poll_votes where (poll_question_id, user_id, poll_answer_id) in
# (select * from (select poll_question_id, user_id, poll_answer_id from poll_votes group by poll_question_id, user_id, poll_answer_id having count(*) > 1) a) (select * from (select poll_question_id, user_id, poll_answer_id from poll_votes group by poll_question_id, user_id, poll_answer_id having count(*) > 1) a)
# and id not in (select * from (select min(id) from poll_votes group by poll_question_id, user_id, poll_answer_id having count(*) > 1 order by id) b)) and id not in (select * from (select min(id) from poll_votes group by poll_question_id, user_id, poll_answer_id having count(*) > 1 order by id) b))
# ActiveRecord::Base.connection.execute sql ActiveRecord::Base.connection.execute sql
#
# add_index :poll_votes, [:poll_question_id, :user_id, :poll_answer_id], name: 'poll_answer_index', unique: true add_index :poll_votes, [:poll_question_id, :user_id, :poll_answer_id], name: 'poll_answer_index', unique: true
end end
end end

Loading…
Cancel
Save