mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: fix 'posts:reorder' rake task
Running the reorder rake task was triggering the following error: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "post_timings_unique" I re-worked the queries and refactored to use the same couple of queries for all similar tables/columns.
This commit is contained in:
parent
4c03a944f6
commit
712ab33ff8
@ -310,92 +310,77 @@ end
|
|||||||
desc 'Reorders all posts based on their creation_date'
|
desc 'Reorders all posts based on their creation_date'
|
||||||
task 'posts:reorder_posts', [:topic_id] => [:environment] do |_, args|
|
task 'posts:reorder_posts', [:topic_id] => [:environment] do |_, args|
|
||||||
Post.transaction do
|
Post.transaction do
|
||||||
# update sort_order and flip post_number to prevent
|
|
||||||
# unique constraint violations when updating post_number
|
builder = DB.build <<~SQL
|
||||||
builder = DB.build(<<~SQL)
|
|
||||||
WITH ordered_posts AS (
|
WITH ordered_posts AS (
|
||||||
SELECT
|
SELECT
|
||||||
id,
|
id,
|
||||||
ROW_NUMBER()
|
ROW_NUMBER() OVER (
|
||||||
OVER (
|
PARTITION BY
|
||||||
PARTITION BY topic_id
|
topic_id
|
||||||
ORDER BY created_at, post_number ) AS new_post_number
|
ORDER BY
|
||||||
FROM posts
|
created_at,
|
||||||
|
post_number
|
||||||
|
) AS new_post_number
|
||||||
|
FROM
|
||||||
|
posts
|
||||||
/*where*/
|
/*where*/
|
||||||
)
|
)
|
||||||
UPDATE posts AS p
|
UPDATE
|
||||||
SET sort_order = o.new_post_number,
|
posts AS p
|
||||||
|
SET
|
||||||
|
sort_order = o.new_post_number,
|
||||||
post_number = p.post_number * -1
|
post_number = p.post_number * -1
|
||||||
FROM ordered_posts AS o
|
FROM
|
||||||
WHERE p.id = o.id AND
|
ordered_posts AS o
|
||||||
|
WHERE
|
||||||
|
p.id = o.id AND
|
||||||
p.post_number <> o.new_post_number
|
p.post_number <> o.new_post_number
|
||||||
SQL
|
SQL
|
||||||
|
|
||||||
builder.where("topic_id = :topic_id") if args[:topic_id]
|
builder.where("topic_id = :topic_id") if args[:topic_id]
|
||||||
builder.exec(topic_id: args[:topic_id])
|
builder.exec(topic_id: args[:topic_id])
|
||||||
|
|
||||||
DB.exec(<<~SQL)
|
[
|
||||||
UPDATE notifications AS x
|
["notifications", "post_number"],
|
||||||
SET post_number = p.sort_order
|
["post_timings", "post_number"],
|
||||||
FROM posts AS p
|
["posts", "reply_to_post_number"],
|
||||||
WHERE x.topic_id = p.topic_id AND
|
["topic_users", "last_read_post_number"],
|
||||||
x.post_number = ABS(p.post_number) AND
|
["topic_users", "highest_seen_post_number"],
|
||||||
p.post_number < 0
|
["topic_users", "last_emailed_post_number"],
|
||||||
|
].each do |table, column|
|
||||||
|
DB.exec <<~SQL
|
||||||
|
UPDATE
|
||||||
|
#{table} AS x
|
||||||
|
SET
|
||||||
|
#{column} = p.sort_order * -1
|
||||||
|
FROM
|
||||||
|
posts AS p
|
||||||
|
WHERE
|
||||||
|
p.post_number < 0 AND
|
||||||
|
x.topic_id = p.topic_id AND
|
||||||
|
x.#{column} = ABS(p.post_number)
|
||||||
SQL
|
SQL
|
||||||
|
|
||||||
DB.exec(<<~SQL)
|
DB.exec <<~SQL
|
||||||
UPDATE post_timings AS x
|
UPDATE
|
||||||
SET post_number = x.post_number * -1
|
#{table}
|
||||||
FROM posts AS p
|
SET
|
||||||
WHERE x.topic_id = p.topic_id AND
|
#{column} = #{column} * -1
|
||||||
x.post_number = ABS(p.post_number) AND
|
WHERE
|
||||||
p.post_number < 0;
|
#{column} < 0
|
||||||
|
SQL
|
||||||
|
end
|
||||||
|
|
||||||
UPDATE post_timings AS t
|
DB.exec <<~SQL
|
||||||
SET post_number = p.sort_order
|
UPDATE
|
||||||
FROM posts AS p
|
posts
|
||||||
WHERE t.topic_id = p.topic_id AND
|
SET
|
||||||
t.post_number = p.post_number AND
|
post_number = sort_order
|
||||||
p.post_number < 0;
|
WHERE
|
||||||
|
post_number < 0
|
||||||
SQL
|
SQL
|
||||||
|
|
||||||
DB.exec(<<~SQL)
|
|
||||||
UPDATE posts AS x
|
|
||||||
SET reply_to_post_number = p.sort_order
|
|
||||||
FROM posts AS p
|
|
||||||
WHERE x.topic_id = p.topic_id AND
|
|
||||||
x.reply_to_post_number = ABS(p.post_number) AND
|
|
||||||
p.post_number < 0;
|
|
||||||
SQL
|
|
||||||
|
|
||||||
DB.exec(<<~SQL)
|
|
||||||
UPDATE topic_users AS x
|
|
||||||
SET last_read_post_number = p.sort_order
|
|
||||||
FROM posts AS p
|
|
||||||
WHERE x.topic_id = p.topic_id AND
|
|
||||||
x.last_read_post_number = ABS(p.post_number) AND
|
|
||||||
p.post_number < 0;
|
|
||||||
|
|
||||||
UPDATE topic_users AS x
|
|
||||||
SET highest_seen_post_number = p.sort_order
|
|
||||||
FROM posts AS p
|
|
||||||
WHERE x.topic_id = p.topic_id AND
|
|
||||||
x.highest_seen_post_number = ABS(p.post_number) AND
|
|
||||||
p.post_number < 0;
|
|
||||||
|
|
||||||
UPDATE topic_users AS x
|
|
||||||
SET last_emailed_post_number = p.sort_order
|
|
||||||
FROM posts AS p
|
|
||||||
WHERE x.topic_id = p.topic_id AND
|
|
||||||
x.last_emailed_post_number = ABS(p.post_number) AND
|
|
||||||
p.post_number < 0;
|
|
||||||
SQL
|
|
||||||
|
|
||||||
# finally update the post_number
|
|
||||||
DB.exec(<<~SQL)
|
|
||||||
UPDATE posts
|
|
||||||
SET post_number = sort_order
|
|
||||||
WHERE post_number < 0
|
|
||||||
SQL
|
|
||||||
end
|
end
|
||||||
|
|
||||||
puts "", "Done.", ""
|
puts "", "Done.", ""
|
||||||
|
Loading…
Reference in New Issue
Block a user