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
