-- v0.403 R1: Product reviews (buyer reviews on purchased products) -- UNIQUE(product_id, buyer_id) - one review per buyer per product DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'products') AND EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'orders') THEN IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'product_reviews') THEN CREATE TABLE product_reviews ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), product_id UUID NOT NULL REFERENCES products(id) ON DELETE CASCADE, buyer_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, order_id UUID NOT NULL REFERENCES orders(id) ON DELETE CASCADE, rating INTEGER NOT NULL CHECK (rating >= 1 AND rating <= 5), comment TEXT, created_at TIMESTAMPTZ DEFAULT NOW(), UNIQUE(product_id, buyer_id) ); CREATE INDEX idx_product_reviews_product_id ON product_reviews(product_id); CREATE INDEX idx_product_reviews_buyer_id ON product_reviews(buyer_id); END IF; END IF; END $$;