ALTER TABLE public.bill_approvals
ADD COLUMN IF NOT EXISTS bill_id UUID;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1
    FROM information_schema.table_constraints
    WHERE constraint_schema = 'public'
      AND table_name = 'bill_approvals'
      AND constraint_name = 'bill_approvals_bill_id_fkey'
  ) THEN
    ALTER TABLE public.bill_approvals
    ADD CONSTRAINT bill_approvals_bill_id_fkey
    FOREIGN KEY (bill_id)
    REFERENCES public.bills(id)
    ON DELETE CASCADE;
  END IF;
END $$;

CREATE INDEX IF NOT EXISTS idx_bill_approvals_bill_id
ON public.bill_approvals (bill_id);