class GenerateInstanceKeys < ActiveRecord::Migration[5.1]
  def change
    BotInstance.where(key: "")
    .or(BotInstance.where(key: nil))
    .each do |instance|
      instance.update(key: SecureRandom.base64(32))
    end

    reversible do |dir|
      change_table :bot_instances do |t|
        dir.up   { t.change :key, :string, :null => false }
        dir.down { t.change :key, :string, :null => true }
      end
    end

    add_index :bot_instances, :key, :unique => true
  end
end

class GenerateInstanceKeys < ActiveRecord::Migration[5.1]
  def change
    BotInstance.where(key: "")
    .or(BotInstance.where(key: nil))
    .each do |instance|
      instance.update(key: SecureRandom.base64(32))
    end

    reversible do |dir|
      change_table :bot_instances do |t|
        dir.up   { t.change :key, :string, :null => false }
        dir.down { t.change :key, :string, :null => true }
      end
    end

    add_index :bot_instances, :key, :unique => true, :length => 40
  end
end

Set index length to 40
Set index length to 40

Since we run an emoji-compatible database, a single char can be up to
four bytes. Maria (and MySQL) enforce a 767-bit index length limit, so
we need to manually pull this down.
