feat: 投稿とタグのリレーション・テーブルについて論理削除と履歴を追加(#84) (#148)

#84 マイグレ修正

Merge remote-tracking branch 'origin/main' into feature/084

#84 構文エラー修正

#84

#84

Co-authored-by: miteruzo <miteruzo@naver.com>
Reviewed-on: #148
This commit was merged in pull request #148.
This commit is contained in:
2025-12-13 16:34:45 +09:00
parent 3b32d4d8ac
commit 9a656a9e6e
8 changed files with 173 additions and 56 deletions
@@ -0,0 +1,36 @@
class AddDiscardToPostTags < ActiveRecord::Migration[8.0]
def up
execute <<~SQL
DELETE
pt1
FROM
post_tags pt1
INNER JOIN
post_tags pt2
ON
pt1.post_id = pt2.post_id
AND pt1.tag_id = pt2.tag_id
AND pt1.id > pt2.id
;
SQL
add_column :post_tags, :discarded_at, :datetime
add_index :post_tags, :discarded_at
add_column :post_tags, :is_active, :boolean,
as: 'discarded_at IS NULL', stored: true
add_column :post_tags, :active_unique_key, :string,
as: "CASE WHEN discarded_at IS NULL THEN CONCAT(post_id, ':', tag_id) ELSE NULL END",
stored: true
add_index :post_tags, :active_unique_key, unique: true, name: 'idx_post_tags_active_unique'
add_index :post_tags, [:post_id, :discarded_at]
add_index :post_tags, [:tag_id, :discarded_at]
end
def down
raise ActiveRecord::IrreversibleMigration, '戻せません.'
end
end