This commit is contained in:
2026-01-27 01:02:33 +09:00
parent 9d68367a5a
commit 48d3f7708e
3 changed files with 51 additions and 3 deletions
@@ -0,0 +1,47 @@
class ChangeUserPostViewsToCompositePk < ActiveRecord::Migration[8.0]
def up
execute <<~SQL
ALTER TABLE
user_post_views
MODIFY COLUMN id BIGINT NOT NULL
;
SQL
execute <<~SQL
ALTER TABLE
user_post_views
DROP PRIMARY KEY
;
SQL
remove_column :user_post_views, :id
execute <<~SQL
ALTER TABLE
user_post_views
ADD PRIMARY KEY (user_id, post_id)
;
SQL
remove_index :user_post_views, name: 'index_user_post_views_on_user_id'
end
def down
execute <<~SQL
ALTER TABLE
user_post_views
DROP PRIMARY KEY
;
SQL
execute <<~SQL
ALTER TABLE
user_post_views
ADD COLUMN id BIGINT NOT NULL AUTO_INCREMENT FIRST
, ADD PRIMARY KEY (id)
;
SQL
add_index :user_post_views, :user_id, name: 'index_user_post_views_on_user_id'
end
end