class ChangeUserIpsToCompositePk < ActiveRecord::Migration[8.0]
  def up
    execute <<~SQL
      ALTER TABLE
        user_ips
          MODIFY COLUMN id BIGINT NOT NULL
      ;
    SQL

    execute <<~SQL
      ALTER TABLE
        user_ips
          DROP PRIMARY KEY
      ;
    SQL

    remove_column :user_ips, :id

    execute <<~SQL
      ALTER TABLE
        user_ips
          ADD PRIMARY KEY (user_id, ip_address_id)
      ;
    SQL

    remove_index :user_ips, name: 'index_user_ips_on_user_id'
  end

  def down
    execute <<~SQL
      ALTER TABLE
        user_ips
          DROP PRIMARY KEY
      ;
    SQL

    execute <<~SQL
      ALTER TABLE
        user_ips
          ADD COLUMN id BIGINT NOT NULL AUTO_INCREMENT FIRST
        , ADD PRIMARY KEY (id)
      ;
    SQL

    add_index :user_ips, :user_id, name: 'index_user_ips_on_user_id'
  end
end
