DO $$
BEGIN
  IF EXISTS (
    SELECT 1
    FROM invoices
    WHERE provider_invoice_id IS NOT NULL
    GROUP BY provider, provider_invoice_id
    HAVING COUNT(*) > 1
  ) THEN
    RAISE EXCEPTION 'duplicate invoices(provider, provider_invoice_id) values require reconciliation before migration 019';
  END IF;
END $$;

CREATE UNIQUE INDEX IF NOT EXISTS idx_invoices_provider_object_unique
  ON invoices(provider, provider_invoice_id)
  WHERE provider_invoice_id IS NOT NULL;
