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 6.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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,pr_id)
  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. (select id from public.pull_request d where b.id=d.issue_id and b.is_pull=true)
  84. FROM public.issue b,public.repository c where b.repo_id=c.id and c.is_private=false;
  85. CREATE OR REPLACE FUNCTION public.insert_issue_data() RETURNS trigger AS
  86. $def$
  87. DECLARE
  88. privateValue boolean=false;
  89. BEGIN
  90. select into privateValue is_private from public.repository where id=NEW.repo_id;
  91. if not privateValue then
  92. INSERT INTO public.issue_es(
  93. id,
  94. repo_id,
  95. index,
  96. poster_id,
  97. original_author,
  98. original_author_id,
  99. name,
  100. content,
  101. milestone_id,
  102. priority,
  103. is_closed,
  104. is_pull,
  105. num_comments,
  106. ref,
  107. deadline_unix,
  108. created_unix,
  109. updated_unix,
  110. closed_unix,
  111. is_locked,
  112. amount,
  113. is_transformed)
  114. VALUES (
  115. NEW.id,
  116. NEW.repo_id,
  117. NEW.index,
  118. NEW.poster_id,
  119. NEW.original_author,
  120. NEW.original_author_id,
  121. NEW.name,
  122. NEW.content,
  123. NEW.milestone_id,
  124. NEW.priority,
  125. NEW.is_closed,
  126. NEW.is_pull,
  127. NEW.num_comments,
  128. NEW.ref,
  129. NEW.deadline_unix,
  130. NEW.created_unix,
  131. NEW.updated_unix,
  132. NEW.closed_unix,
  133. NEW.is_locked,
  134. NEW.amount,
  135. NEW.is_transformed
  136. );
  137. end if;
  138. RETURN NEW;
  139. END;
  140. $def$
  141. LANGUAGE plpgsql;
  142. DROP TRIGGER IF EXISTS es_insert_issue on public.issue;
  143. CREATE TRIGGER es_insert_issue
  144. AFTER INSERT ON public.issue
  145. FOR EACH ROW EXECUTE PROCEDURE insert_issue_data();
  146. ALTER TABLE public.issue ENABLE ALWAYS TRIGGER es_insert_issue;
  147. CREATE OR REPLACE FUNCTION public.udpate_issue_comment() RETURNS trigger AS
  148. $def$
  149. BEGIN
  150. if (TG_OP = 'DELETE') then
  151. 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;
  152. elsif (TG_OP = 'UPDATE') then
  153. 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;
  154. end if;
  155. return null;
  156. END;
  157. $def$
  158. LANGUAGE plpgsql;
  159. DROP TRIGGER IF EXISTS es_udpate_issue_comment on public.comment;
  160. CREATE TRIGGER es_udpate_issue_comment
  161. AFTER DELETE OR UPDATE ON public.comment
  162. FOR EACH ROW EXECUTE PROCEDURE udpate_issue_comment();
  163. ALTER TABLE public.comment ENABLE ALWAYS TRIGGER es_udpate_issue_comment;
  164. CREATE OR REPLACE FUNCTION public.update_issue() RETURNS trigger AS
  165. $def$
  166. declare
  167. BEGIN
  168. UPDATE public.issue_es
  169. SET content=NEW.content,
  170. name=NEW.name,
  171. is_closed=NEW.is_closed,
  172. num_comments=NEW.num_comments,
  173. comment=(select array_to_string(array_agg(content order by created_unix desc),',') from public.comment where issue_id=NEW.id)
  174. where id=NEW.id;
  175. return new;
  176. END
  177. $def$
  178. LANGUAGE plpgsql;
  179. DROP TRIGGER IF EXISTS es_update_issue on public.issue;
  180. CREATE TRIGGER es_update_issue
  181. AFTER UPDATE ON public.issue
  182. FOR EACH ROW EXECUTE PROCEDURE update_issue();
  183. ALTER TABLE public.issue ENABLE ALWAYS TRIGGER es_update_issue;
  184. CREATE OR REPLACE FUNCTION public.delete_issue() RETURNS trigger AS
  185. $def$
  186. declare
  187. BEGIN
  188. DELETE FROM public.issue_es where id=OLD.id;
  189. return new;
  190. END
  191. $def$
  192. LANGUAGE plpgsql;
  193. DROP TRIGGER IF EXISTS es_delete_issue on public.issue;
  194. CREATE TRIGGER es_delete_issue
  195. AFTER DELETE ON public.issue
  196. FOR EACH ROW EXECUTE PROCEDURE delete_issue();
  197. ALTER TABLE public.issue ENABLE ALWAYS TRIGGER es_delete_issue;