class CreateMunsterTables < ActiveRecord::Migration[7.0]
  def change
    create_table :received_webhooks do |t|
      t.string :handler_event_id, null: false
      t.string :handler_module_name, null: false
      t.string :status, default: "received", null: false

      # We don't assume, that we can always parse received body as JSON. Body could be invalid or partly missing,
      # we can argue how we can handle that for different integrations, but we still should be able to save this data
      # if it's required. Hence, we don't use :jsonb, but :binary type column here.
      t.binary :body, null: false

      t.timestamps
    end

    # For deduplication at ingress. UNIQUE indices in Postgres are case-sensitive
    # which is what we want, as these are externally-generated IDs
    add_index :received_webhooks, [:handler_module_name, :handler_event_id], unique: true, name: "webhook_dedup_idx"
    # For backfill processing (to know how many skipped etc. payloads we have)
    add_index :received_webhooks, [:status]
  end
end
