You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue_foreigntable_for_es.sql 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. DROP FOREIGN TABLE public.issue_es;
  2. CREATE FOREIGN TABLE public.issue_es
  3. (
  4. id bigint NOT NULL,
  5. repo_id bigint,
  6. index bigint,
  7. poster_id bigint,
  8. original_author character varying(255),
  9. original_author_id bigint,
  10. name character varying(255) ,
  11. content text,
  12. comment text,
  13. milestone_id bigint,
  14. priority integer,
  15. is_closed boolean,
  16. is_pull boolean,
  17. pr_id bigint,
  18. num_comments integer,
  19. ref character varying(255),
  20. deadline_unix bigint,
  21. created_unix bigint,
  22. updated_unix bigint,
  23. closed_unix bigint,
  24. is_locked boolean NOT NULL,
  25. amount bigint,
  26. is_transformed boolean NOT NULL
  27. )SERVER multicorn_es
  28. OPTIONS
  29. (
  30. host '192.168.207.94',
  31. port '9200',
  32. index 'issue-es-index',
  33. rowid_column 'id',
  34. default_sort '_id'
  35. )
  36. ;
  37. delete from public.issue_es;
  38. INSERT INTO public.issue_es(
  39. id,
  40. repo_id,
  41. index,
  42. poster_id,
  43. original_author,
  44. original_author_id,
  45. name,
  46. content,
  47. milestone_id,
  48. priority,
  49. is_closed,
  50. is_pull,
  51. num_comments,
  52. ref,
  53. deadline_unix,
  54. created_unix,
  55. updated_unix,
  56. closed_unix,
  57. is_locked,
  58. amount,
  59. is_transformed,comment)
  60. SELECT
  61. b.id,
  62. b.repo_id,
  63. b.index,
  64. b.poster_id,
  65. b.original_author,
  66. b.original_author_id,
  67. b.name,
  68. b.content,
  69. b.milestone_id,
  70. b.priority,
  71. b.is_closed,
  72. b.is_pull,
  73. b.num_comments,
  74. b.ref,
  75. b.deadline_unix,
  76. b.created_unix,
  77. b.updated_unix,
  78. b.closed_unix,
  79. b.is_locked,
  80. b.amount,
  81. b.is_transformed,
  82. (select array_to_string(array_agg(content order by created_unix desc),',') from public.comment a where a.issue_id=b.id)
  83. FROM public.issue b,public.repository c where b.repo_id=c.id and c.is_private=false;
  84. CREATE OR REPLACE FUNCTION public.insert_issue_data() RETURNS trigger AS
  85. $def$
  86. DECLARE
  87. privateValue boolean=false;
  88. BEGIN
  89. select into privateValue is_private from public.repository where id=NEW.repo_id;
  90. if not privateValue then
  91. INSERT INTO public.issue_es(
  92. id,
  93. repo_id,
  94. index,
  95. poster_id,
  96. original_author,
  97. original_author_id,
  98. name,
  99. content,
  100. milestone_id,
  101. priority,
  102. is_closed,
  103. is_pull,
  104. num_comments,
  105. ref,
  106. deadline_unix,
  107. created_unix,
  108. updated_unix,
  109. closed_unix,
  110. is_locked,
  111. amount,
  112. is_transformed)
  113. VALUES (
  114. NEW.id,
  115. NEW.repo_id,
  116. NEW.index,
  117. NEW.poster_id,
  118. NEW.original_author,
  119. NEW.original_author_id,
  120. NEW.name,
  121. NEW.content,
  122. NEW.milestone_id,
  123. NEW.priority,
  124. NEW.is_closed,
  125. NEW.is_pull,
  126. NEW.num_comments,
  127. NEW.ref,
  128. NEW.deadline_unix,
  129. NEW.created_unix,
  130. NEW.updated_unix,
  131. NEW.closed_unix,
  132. NEW.is_locked,
  133. NEW.amount,
  134. NEW.is_transformed
  135. );
  136. end if;
  137. RETURN NEW;
  138. END;
  139. $def$
  140. LANGUAGE plpgsql;
  141. DROP TRIGGER IF EXISTS es_insert_issue on public.issue;
  142. CREATE TRIGGER es_insert_issue
  143. AFTER INSERT ON public.issue
  144. FOR EACH ROW EXECUTE PROCEDURE insert_issue_data();
  145. ALTER TABLE public.issue ENABLE ALWAYS TRIGGER es_insert_issue;
  146. CREATE OR REPLACE FUNCTION public.udpate_issue_comment() RETURNS trigger AS
  147. $def$
  148. BEGIN
  149. if (TG_OP = 'DELETE') then
  150. update public.issue_es SET comment=(select array_to_string(array_agg(content order by created_unix desc),',') from public.comment where issue_id=OLD.issue_id) where id=OLD.issue_id;
  151. elsif (TG_OP = 'UPDATE') then
  152. update public.issue_es SET comment=(select array_to_string(array_agg(content order by created_unix desc),',') from public.comment where issue_id=NEW.issue_id) where id=NEW.issue_id;
  153. end if;
  154. return null;
  155. END;
  156. $def$
  157. LANGUAGE plpgsql;
  158. DROP TRIGGER IF EXISTS es_udpate_issue_comment on public.comment;
  159. CREATE TRIGGER es_udpate_issue_comment
  160. AFTER DELETE OR UPDATE ON public.comment
  161. FOR EACH ROW EXECUTE PROCEDURE udpate_issue_comment();
  162. ALTER TABLE public.comment ENABLE ALWAYS TRIGGER es_udpate_issue_comment;
  163. CREATE OR REPLACE FUNCTION public.update_issue() RETURNS trigger AS
  164. $def$
  165. declare
  166. BEGIN
  167. UPDATE public.issue_es
  168. SET content=NEW.content,
  169. name=NEW.name,
  170. is_closed=NEW.is_closed,
  171. num_comments=NEW.num_comments,
  172. comment=(select array_to_string(array_agg(content order by created_unix desc),',') from public.comment where issue_id=NEW.id)
  173. where id=NEW.id;
  174. return new;
  175. END
  176. $def$
  177. LANGUAGE plpgsql;
  178. DROP TRIGGER IF EXISTS es_update_issue on public.issue;
  179. CREATE TRIGGER es_update_issue
  180. AFTER UPDATE ON public.issue
  181. FOR EACH ROW EXECUTE PROCEDURE update_issue();
  182. ALTER TABLE public.issue ENABLE ALWAYS TRIGGER es_update_issue;
  183. CREATE OR REPLACE FUNCTION public.delete_issue() RETURNS trigger AS
  184. $def$
  185. declare
  186. BEGIN
  187. DELETE FROM public.issue_es where id=OLD.id;
  188. return new;
  189. END
  190. $def$
  191. LANGUAGE plpgsql;
  192. DROP TRIGGER IF EXISTS es_delete_issue on public.issue;
  193. CREATE TRIGGER es_delete_issue
  194. AFTER DELETE ON public.issue
  195. FOR EACH ROW EXECUTE PROCEDURE delete_issue();
  196. ALTER TABLE public.issue ENABLE ALWAYS TRIGGER es_delete_issue;