CREATE TABLE device_customer_transfers ( transfer_id uuid PRIMARY KEY, source_tenant_id uuid NOT NULL REFERENCES tenants, source_asset_id uuid NOT NULL, source_agent_id uuid NOT NULL, target_tenant_id uuid NOT NULL REFERENCES tenants, requested_by text NOT NULL, target_asset_id uuid, target_agent_id uuid, csr_sha256 text, created_at timestamptz NOT NULL DEFAULT now(), completed_at timestamptz, cancelled_at timestamptz, CHECK (source_tenant_id <> target_tenant_id), CHECK ((target_asset_id IS NULL) = (target_agent_id IS NULL)), CHECK ((csr_sha256 IS NULL) = (target_agent_id IS NULL)), CHECK (completed_at IS NULL OR target_agent_id IS NOT NULL), CHECK (cancelled_at IS NULL OR target_agent_id IS NULL) ); CREATE UNIQUE INDEX device_customer_transfers_source ON device_customer_transfers (source_tenant_id,source_agent_id) WHERE cancelled_at IS NULL; ALTER TABLE device_customer_transfers ENABLE ROW LEVEL SECURITY; CREATE POLICY tenant_scope ON device_customer_transfers USING (source_tenant_id::text = current_setting('app.tenant_id', true)); GRANT SELECT, INSERT ON device_customer_transfers TO rmm_app; GRANT UPDATE (target_asset_id,target_agent_id,csr_sha256,completed_at,cancelled_at) ON device_customer_transfers TO rmm_app;