미디어 업로드와 태그 표시 수정
This commit is contained in:
21
db/migrations/055_add_post_tag_sort_order.sql
Normal file
21
db/migrations/055_add_post_tag_sort_order.sql
Normal file
@@ -0,0 +1,21 @@
|
||||
ALTER TABLE post_tags
|
||||
ADD COLUMN IF NOT EXISTS sort_order INTEGER NOT NULL DEFAULT 0;
|
||||
|
||||
WITH ordered_post_tags AS (
|
||||
SELECT
|
||||
post_id,
|
||||
tag_id,
|
||||
(ROW_NUMBER() OVER (
|
||||
PARTITION BY post_id
|
||||
ORDER BY created_at ASC, tag_id ASC
|
||||
) - 1) * 10 AS next_sort_order
|
||||
FROM post_tags
|
||||
)
|
||||
UPDATE post_tags
|
||||
SET sort_order = ordered_post_tags.next_sort_order
|
||||
FROM ordered_post_tags
|
||||
WHERE post_tags.post_id = ordered_post_tags.post_id
|
||||
AND post_tags.tag_id = ordered_post_tags.tag_id;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS post_tags_post_id_sort_order_idx
|
||||
ON post_tags (post_id, sort_order ASC, created_at ASC);
|
||||
Reference in New Issue
Block a user