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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. CREATE FOREIGN TABLE public.issue_es
  2. (
  3. id bigint NOT NULL,
  4. repo_id bigint,
  5. index bigint,
  6. poster_id bigint,
  7. original_author character varying(255),
  8. original_author_id bigint,
  9. name character varying(255) ,
  10. content text,
  11. comment text,
  12. milestone_id bigint,
  13. priority integer,
  14. is_closed boolean,
  15. is_pull boolean,
  16. pr_id bigint,
  17. num_comments integer,
  18. ref character varying(255),
  19. deadline_unix bigint,
  20. created_unix bigint,
  21. updated_unix bigint,
  22. closed_unix bigint,
  23. is_locked boolean NOT NULL,
  24. amount bigint,
  25. is_transformed boolean NOT NULL
  26. )SERVER multicorn_es
  27. OPTIONS
  28. (
  29. host '192.168.207.94',
  30. port '9200',
  31. index 'user_es-index',
  32. rowid_column 'id'
  33. )
  34. ;
  35. CREATE OR REPLACE FUNCTION public.insert_issue_data() RETURNS trigger AS
  36. $def$
  37. BEGIN
  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)
  60. VALUES (
  61. NEW.id,
  62. NEW.repo_id,
  63. NEW.index,
  64. NEW.poster_id,
  65. NEW.original_author,
  66. NEW.original_author_id,
  67. NEW.name,
  68. NEW.content,
  69. NEW.milestone_id,
  70. NEW.priority,
  71. NEW.is_closed,
  72. NEW.is_pull,
  73. NEW.num_comments,
  74. NEW.ref,
  75. NEW.deadline_unix,
  76. NEW.created_unix,
  77. NEW.updated_unix,
  78. NEW.closed_unix,
  79. NEW.is_locked,
  80. NEW.amount,
  81. NEW.is_transformed
  82. );
  83. RETURN NEW;
  84. END;
  85. $def$
  86. LANGUAGE plpgsql;
  87. DROP TRIGGER IF EXISTS es_insert_issue on public.issue;
  88. CREATE TRIGGER es_insert_issue
  89. AFTER INSERT ON public.issue
  90. FOR EACH ROW EXECUTE PROCEDURE insert_issue_data();
  91. CREATE OR REPLACE FUNCTION public.udpate_issue_comment() RETURNS trigger AS
  92. $def$
  93. BEGIN
  94. if (TG_OP = 'DELETE') then
  95. 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;
  96. elsif (TG_OP = 'INSERT') then
  97. 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;
  98. elsif (TG_OP = 'UPDATE') then
  99. 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;
  100. end if;
  101. return null;
  102. END;
  103. $def$
  104. LANGUAGE plpgsql;
  105. DROP TRIGGER IF EXISTS es_udpate_issue_comment on public.comment;
  106. CREATE TRIGGER es_udpate_issue_comment
  107. AFTER INSERT OR DELETE OR UPDATE ON public.comment
  108. FOR EACH ROW EXECUTE PROCEDURE udpate_issue_comment();
  109. CREATE OR REPLACE FUNCTION public.update_issue_content() RETURNS trigger AS
  110. $def$
  111. declare
  112. BEGIN
  113. UPDATE public.issue_es SET content=NEW.content where id=NEW.id;
  114. return new;
  115. END
  116. $def$
  117. LANGUAGE plpgsql;
  118. DROP TRIGGER IF EXISTS es_update_issue_content on public.issue;
  119. CREATE TRIGGER es_update_issue_content
  120. AFTER UPDATE OF "content" ON public.issue
  121. FOR EACH ROW EXECUTE PROCEDURE update_issue_content();
  122. CREATE OR REPLACE FUNCTION public.update_issue_name() RETURNS trigger AS
  123. $def$
  124. declare
  125. BEGIN
  126. UPDATE public.issue_es SET name=NEW.name where id=NEW.id;
  127. return new;
  128. END
  129. $def$
  130. LANGUAGE plpgsql;
  131. DROP TRIGGER IF EXISTS es_update_issue_name on public.issue;
  132. CREATE TRIGGER es_update_issue_name
  133. AFTER UPDATE OF "name" ON public.issue
  134. FOR EACH ROW EXECUTE PROCEDURE update_issue_name();
  135. CREATE OR REPLACE FUNCTION public.update_issue_is_closed() RETURNS trigger AS
  136. $def$
  137. declare
  138. BEGIN
  139. UPDATE public.issue_es SET is_closed=NEW.is_closed where id=NEW.id;
  140. return new;
  141. END
  142. $def$
  143. LANGUAGE plpgsql;
  144. DROP TRIGGER IF EXISTS es_update_issue_is_closed on public.issue;
  145. CREATE TRIGGER es_update_issue_is_closed
  146. AFTER UPDATE OF "is_closed" ON public.issue
  147. FOR EACH ROW EXECUTE PROCEDURE update_issue_is_closed();
  148. CREATE OR REPLACE FUNCTION public.update_issue_num_comments() RETURNS trigger AS
  149. $def$
  150. declare
  151. BEGIN
  152. UPDATE public.issue_es SET num_comments=NEW.num_comments where id=NEW.id;
  153. return new;
  154. END
  155. $def$
  156. LANGUAGE plpgsql;
  157. DROP TRIGGER IF EXISTS es_update_issue_num_comments on public.issue;
  158. CREATE TRIGGER es_update_issue_num_comments
  159. AFTER UPDATE OF "num_comments" ON public.issue
  160. FOR EACH ROW EXECUTE PROCEDURE update_issue_num_comments();
  161. CREATE OR REPLACE FUNCTION public.delete_issue() RETURNS trigger AS
  162. $def$
  163. declare
  164. BEGIN
  165. DELETE FROM public.issue_es where id=OLD.id;
  166. return new;
  167. END
  168. $def$
  169. LANGUAGE plpgsql;
  170. DROP TRIGGER IF EXISTS es_delete_issue on public.issue;
  171. CREATE TRIGGER es_delete_issue
  172. AFTER DELETE ON public.issue
  173. FOR EACH ROW EXECUTE PROCEDURE delete_issue();