class CreateAdminAccounts < ActiveRecord::Migration[7.2]
  def up
    create_table :admin_accounts, id: :uuid, default: -> { "gen_random_uuid()" } do |t|
      t.string :username, null: false
      t.string :password_digest, null: false
      t.timestamps
    end

    add_index :admin_accounts, :username, unique: true

    password_digest = BCrypt::Password.create("admin")
    execute <<~SQL.squish
      INSERT INTO admin_accounts (id, username, password_digest, created_at, updated_at)
      VALUES (gen_random_uuid(), 'admin', #{connection.quote(password_digest)}, NOW(), NOW())
    SQL
  end

  def down
    drop_table :admin_accounts
  end
end
