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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. id,
  62. repo_id,
  63. index,
  64. poster_id,
  65. original_author,
  66. original_author_id,
  67. name,
  68. content,
  69. milestone_id,
  70. priority,
  71. is_closed,
  72. is_pull,
  73. num_comments,
  74. ref,
  75. deadline_unix,
  76. created_unix,
  77. updated_unix,
  78. closed_unix,
  79. is_locked,
  80. amount,
  81. 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;
  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. CREATE OR REPLACE FUNCTION public.udpate_issue_comment() RETURNS trigger AS
  146. $def$
  147. BEGIN
  148. if (TG_OP = 'DELETE') then
  149. 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;
  150. elsif (TG_OP = 'UPDATE') 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=NEW.issue_id) where id=NEW.issue_id;
  152. end if;
  153. return null;
  154. END;
  155. $def$
  156. LANGUAGE plpgsql;
  157. DROP TRIGGER IF EXISTS es_udpate_issue_comment on public.comment;
  158. CREATE TRIGGER es_udpate_issue_comment
  159. AFTER DELETE OR UPDATE ON public.comment
  160. FOR EACH ROW EXECUTE PROCEDURE udpate_issue_comment();
  161. CREATE OR REPLACE FUNCTION public.update_issue() RETURNS trigger AS
  162. $def$
  163. declare
  164. BEGIN
  165. UPDATE public.issue_es
  166. SET content=NEW.content,
  167. name=NEW.name,
  168. is_closed=NEW.is_closed,
  169. num_comments=NEW.num_comments,
  170. comment=(select array_to_string(array_agg(content order by created_unix desc),',') from public.comment where issue_id=NEW.id)
  171. where id=NEW.id;
  172. return new;
  173. END
  174. $def$
  175. LANGUAGE plpgsql;
  176. DROP TRIGGER IF EXISTS es_update_issue on public.issue;
  177. CREATE TRIGGER es_update_issue
  178. AFTER UPDATE ON public.issue
  179. FOR EACH ROW EXECUTE PROCEDURE update_issue();
  180. CREATE OR REPLACE FUNCTION public.delete_issue() RETURNS trigger AS
  181. $def$
  182. declare
  183. BEGIN
  184. DELETE FROM public.issue_es where id=OLD.id;
  185. return new;
  186. END
  187. $def$
  188. LANGUAGE plpgsql;
  189. DROP TRIGGER IF EXISTS es_delete_issue on public.issue;
  190. CREATE TRIGGER es_delete_issue
  191. AFTER DELETE ON public.issue
  192. FOR EACH ROW EXECUTE PROCEDURE delete_issue();