mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-23 15:48:43 -05:00
FIX: unique index on topic_id, post_number
This commit is contained in:
parent
45ca83328d
commit
1b03feacf8
1 changed files with 26 additions and 0 deletions
26
db/migrate/20140715013018_correct_post_number_index.rb
Normal file
26
db/migrate/20140715013018_correct_post_number_index.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
class CorrectPostNumberIndex < ActiveRecord::Migration
|
||||
def change
|
||||
|
||||
begin
|
||||
a = execute <<SQL
|
||||
UPDATE posts SET post_number = post_number + 1
|
||||
WHERE id IN (
|
||||
SELECT p1.id
|
||||
FROM posts p1
|
||||
JOIN
|
||||
(
|
||||
SELECT post_number, topic_id, min(id) min_id
|
||||
FROM posts
|
||||
GROUP BY post_number, topic_id
|
||||
HAVING COUNT(*) > 1
|
||||
) pp ON p1.topic_id = pp.topic_id AND
|
||||
p1.post_number >= pp.post_number AND
|
||||
p1.id <> pp.min_id
|
||||
)
|
||||
SQL
|
||||
end until a.cmdtuples == 0
|
||||
|
||||
remove_index :posts, [:topic_id, :post_number]
|
||||
add_index :posts, [:topic_id, :post_number], unique: true
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue