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

CREATE TABLE public.production_price_history (id UUID PRIMARY KEY DEFAULT gen_random_uuid(), plan_id UUID NOT NULL REFERENCES public.production_plans(id) ON DELETE CASCADE, old_price DECIMAL NOT NULL DEFAULT 0, new_price DECIMAL NOT NULL DEFAULT 0, change_amount DECIMAL NOT NULL DEFAULT 0, operator_id UUID REFERENCES auth.users(id) ON DELETE SET NULL, operator_name TEXT, change_reason TEXT, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()); CREATE INDEX idx_price_history_plan ON public.production_price_history(plan_id); CREATE INDEX idx_price_history_created ON public.production_price_history(created_at); ALTER TABLE public.production_price_history ENABLE ROW LEVEL SECURITY; CREATE POLICY purchaser_select_price_history ON public.production_price_history FOR SELECT USING (EXISTS (SELECT 1 FROM public.production_plans p WHERE p.id = production_price_history.plan_id AND p.purchaser_id = get_user_master_company_id())); CREATE POLICY textile_select_price_history ON public.production_price_history FOR SELECT USING (EXISTS (SELECT 1 FROM public.production_plans p JOIN public.plan_factories pf ON pf.plan_id = p.id WHERE p.id = production_price_history.plan_id AND pf.factory_id = get_user_master_company_id())); CREATE POLICY purchaser_insert_price_history ON public.production_price_history FOR INSERT WITH CHECK (EXISTS (SELECT 1 FROM public.production_plans p WHERE p.id = production_price_history.plan_id AND p.purchaser_id = get_user_master_company_id()));
