-- ============================================
-- 此文件由 Meoo Cloud 自动生成，请勿修改
-- This file is auto-generated by Meoo Cloud, DO NOT MODIFY
-- ============================================


-- 修复 plan_factories 的 RLS 策略
-- 删除旧的策略
DROP POLICY IF EXISTS factory_select_plan_factories ON public.plan_factories;
DROP POLICY IF EXISTS purchaser_select_plan_factories ON public.plan_factories;
DROP POLICY IF EXISTS purchaser_insert_plan_factories ON public.plan_factories;
DROP POLICY IF EXISTS purchaser_delete_plan_factories ON public.plan_factories;

-- 创建新的策略：允许查看（用于仓库同步和分享链接）
CREATE POLICY users_select_plan_factories ON public.plan_factories
FOR SELECT USING (true);

-- 插入和删除保持严格限制
CREATE POLICY users_insert_plan_factories ON public.plan_factories
FOR INSERT WITH CHECK (
 EXISTS (
 SELECT 1 FROM public.production_plans
 WHERE id = plan_id AND purchaser_id = public.get_user_master_company_id()
 )
);

CREATE POLICY users_delete_plan_factories ON public.plan_factories
FOR DELETE USING (
 EXISTS (
 SELECT 1 FROM public.production_plans
 WHERE id = plan_id AND purchaser_id = public.get_user_master_company_id()
 )
);

