ALTER TABLE agent_installations ADD COLUMN customer_transfer_pending boolean NOT NULL DEFAULT false, ADD COLUMN customer_transfer_activated_at timestamptz; CREATE UNIQUE INDEX device_customer_transfers_target ON device_customer_transfers (target_tenant_id,target_agent_id) WHERE target_agent_id IS NOT NULL; -- The target has tenant-scoped access to its inventory. Only this function -- can use that evidence to close the exact transfer in the source tenant. CREATE FUNCTION finish_device_customer_transfer(target_tenant uuid, target_agent uuid) RETURNS boolean LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, public, pg_temp AS $$ DECLARE move public.device_customer_transfers%ROWTYPE; cutover timestamptz; BEGIN IF current_setting('app.tenant_id', true) IS DISTINCT FROM target_tenant::text THEN RETURN false; END IF; SELECT t.* INTO move FROM public.device_customer_transfers t JOIN public.agent_installations a ON (a.tenant_id,a.asset_id,a.agent_id) = (t.target_tenant_id,t.target_asset_id,t.target_agent_id) WHERE t.target_tenant_id=target_tenant AND t.target_agent_id=target_agent AND t.completed_at IS NULL AND t.cancelled_at IS NULL AND a.customer_transfer_pending AND a.revoked_at IS NULL AND a.last_authenticated_at IS NOT NULL AND EXISTS ( SELECT FROM public.observations o WHERE (o.tenant_id,o.agent_id)=(a.tenant_id,a.agent_id) AND o.received_at>=a.created_at AND o.quality='valid' AND o.source_id IN ('linux.base','windows.base') AND NULLIF(btrim(o.data->>'hostname'),'') IS NOT NULL ) FOR UPDATE OF a,t; IF NOT FOUND THEN RETURN false; END IF; -- Serialize with source-side job planning and device controls. Pending -- transfers already prevent new work from being submitted to the source. PERFORM set_config('app.tenant_id',move.source_tenant_id::text,true); PERFORM FROM public.agent_installations WHERE (tenant_id,asset_id,agent_id)=(move.source_tenant_id,move.source_asset_id,move.source_agent_id) FOR UPDATE; IF EXISTS ( SELECT FROM public.jobs WHERE tenant_id=move.source_tenant_id AND agent_id=move.source_agent_id AND state NOT IN ('succeeded','failed','cancelled','expired','closed_unknown') ) OR EXISTS ( SELECT FROM public.approval_requests r JOIN public.operation_plans p USING(tenant_id,plan_id) WHERE r.tenant_id=move.source_tenant_id AND r.approval_state='pending_approval' AND p.expires_at>now() AND convert_from(p.payload,'UTF8')::jsonb->'jobs' @> jsonb_build_array(jsonb_build_object('payload',jsonb_build_object('agent_id',move.source_agent_id))) ) THEN PERFORM set_config('app.tenant_id',target_tenant::text,true); RETURN false; END IF; cutover := clock_timestamp(); UPDATE public.agent_installations SET revoked_at=cutover, connection_generation=connection_generation+1, previous_certificate_sha256=NULL, previous_certificate_until=NULL WHERE (tenant_id,asset_id,agent_id)=(move.source_tenant_id,move.source_asset_id,move.source_agent_id) AND revoked_at IS NULL; UPDATE public.device_customer_transfers SET completed_at=cutover WHERE transfer_id=move.transfer_id; INSERT INTO public.audit_events (tenant_id,event_id,principal,event_type,data) VALUES (move.source_tenant_id,gen_random_uuid(),move.requested_by,'device_customer_transfer_completed', jsonb_build_object('completion_origin','automatic_after_target_inventory','decision', jsonb_build_object('transfer_id',move.transfer_id,'asset_id',move.source_asset_id, 'agent_id',move.source_agent_id,'target_tenant_id',move.target_tenant_id, 'target_asset_id',move.target_asset_id,'target_agent_id',move.target_agent_id))); PERFORM set_config('app.tenant_id',target_tenant::text,true); UPDATE public.agent_installations SET customer_transfer_pending=false, customer_transfer_activated_at=cutover, last_contact_at=COALESCE(last_contact_at,cutover) WHERE (tenant_id,asset_id,agent_id)=(move.target_tenant_id,move.target_asset_id,move.target_agent_id); RETURN true; END; $$; REVOKE ALL ON FUNCTION finish_device_customer_transfer(uuid,uuid) FROM PUBLIC; GRANT EXECUTE ON FUNCTION finish_device_customer_transfer(uuid,uuid) TO rmm_app; -- Existing enrollments can already have proved their new identity under the -- old workflow. Finish only transfers with that evidence and preserve history. UPDATE agent_installations a SET customer_transfer_pending=true FROM device_customer_transfers t WHERE (a.tenant_id,a.asset_id,a.agent_id)=(t.target_tenant_id,t.target_asset_id,t.target_agent_id) AND t.completed_at IS NULL AND t.cancelled_at IS NULL; DO $$ DECLARE destination record; BEGIN FOR destination IN SELECT target_tenant_id,target_agent_id FROM device_customer_transfers WHERE target_agent_id IS NOT NULL AND completed_at IS NULL AND cancelled_at IS NULL ORDER BY created_at,transfer_id LOOP PERFORM set_config('app.tenant_id',destination.target_tenant_id::text,true); PERFORM finish_device_customer_transfer(destination.target_tenant_id,destination.target_agent_id); END LOOP; END; $$;