ALTER TABLE "affiliate_payouts" ADD COLUMN IF NOT EXISTS "settlement_rail" text;
--> statement-breakpoint
ALTER TABLE "affiliate_payouts" ADD COLUMN IF NOT EXISTS "settlement_reference" text;
--> statement-breakpoint
ALTER TABLE "affiliate_payouts" ADD COLUMN IF NOT EXISTS "settled_at" timestamp with time zone;
--> statement-breakpoint
ALTER TABLE "affiliate_payouts" ADD COLUMN IF NOT EXISTS "settled_by_user_id" uuid;
--> statement-breakpoint
DO $$ BEGIN
  ALTER TABLE "affiliate_payouts" ADD CONSTRAINT "affiliate_payouts_settled_by_user_id_users_id_fk" FOREIGN KEY ("settled_by_user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION WHEN duplicate_object THEN NULL;
END $$;
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "affiliate_payouts_settlement_evidence_uq" ON "affiliate_payouts" USING btree ("settlement_rail", "settlement_reference");
