migrate_brand_recommend.sql 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. -- ============================================================
  2. -- 品牌药品 + 品牌推荐规则 迁移
  3. -- 执行: psql -h <host> -U <user> -d pharmacopoeia -f database/migrate_brand_recommend.sql
  4. -- ============================================================
  5. BEGIN;
  6. -- 品牌药品表
  7. CREATE TABLE IF NOT EXISTS public.brands (
  8. id SERIAL PRIMARY KEY,
  9. name varchar(200) NOT NULL,
  10. function_indication text,
  11. usage_dosage text,
  12. contraindication text,
  13. ingredients text,
  14. properties text,
  15. specification text,
  16. adverse_reactions text,
  17. precautions text,
  18. execution_standard text,
  19. storage text,
  20. jump_url varchar(500),
  21. description varchar(500),
  22. sort_order integer NOT NULL DEFAULT 0,
  23. is_active boolean NOT NULL DEFAULT true,
  24. created_at timestamptz DEFAULT now(),
  25. updated_at timestamptz DEFAULT now()
  26. );
  27. CREATE UNIQUE INDEX IF NOT EXISTS idx_brand_name ON public.brands(name);
  28. -- 品牌推荐规则表(关联品牌)
  29. CREATE TABLE IF NOT EXISTS public.brand_recommend_rules (
  30. id SERIAL PRIMARY KEY,
  31. keyword varchar(100) NOT NULL,
  32. brand_id integer NOT NULL REFERENCES public.brands(id) ON DELETE CASCADE,
  33. tier integer NOT NULL DEFAULT 1,
  34. is_active boolean NOT NULL DEFAULT true,
  35. created_at timestamptz DEFAULT now(),
  36. updated_at timestamptz DEFAULT now()
  37. );
  38. CREATE INDEX IF NOT EXISTS idx_br_keyword ON public.brand_recommend_rules(keyword);
  39. CREATE INDEX IF NOT EXISTS idx_br_brand ON public.brand_recommend_rules(brand_id);
  40. CREATE INDEX IF NOT EXISTS idx_br_tier ON public.brand_recommend_rules(tier);
  41. CREATE INDEX IF NOT EXISTS idx_br_active ON public.brand_recommend_rules(is_active);
  42. -- messages 表加 brand_recommendations 快照字段(历史对话持久化)
  43. ALTER TABLE public.messages ADD COLUMN IF NOT EXISTS brand_recommendations jsonb;
  44. COMMIT;