# typed: false
class CreateDomainFaUserAvatars < ActiveRecord::Migration[7.0]
  def change
    create_table :domain_fa_user_avatars do |t|
      t.references :user, null: false, index: { unique: true }
      t.integer :state, null: false
      t.jsonb :state_detail
      # url of the file
      t.string :file_url_str
      t.references :log_entry, index: false
      # direct access to log_entry.file if request succeeded
      t.binary :file_sha256
      t.datetime :downloaded_file_at
      t.timestamps
    end

    add_foreign_key :domain_fa_user_avatars,
                    :domain_fa_users,
                    column: :user_id,
                    primary_key: :id
    add_foreign_key :domain_fa_user_avatars,
                    :http_log_entries,
                    column: :log_entry_id,
                    primary_key: :id
    add_foreign_key :domain_fa_user_avatars,
                    :blob_entries,
                    column: :file_sha256,
                    primary_key: :sha256

    create_versions_table :domain_fa_user_avatars
  end
end
