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.

repo_foreigntable_for_es.sql 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. drop FOREIGN table if exists public.repository_es;
  2. CREATE FOREIGN TABLE public.repository_es (
  3. id bigint NOT NULL,
  4. owner_id bigint,
  5. owner_name character varying(255),
  6. lower_name character varying(255) NOT NULL,
  7. name character varying(255) NOT NULL,
  8. description text,
  9. website character varying(2048),
  10. original_service_type integer,
  11. original_url character varying(2048),
  12. default_branch character varying(255),
  13. num_watches integer,
  14. num_stars integer,
  15. num_forks integer,
  16. num_issues integer,
  17. num_closed_issues integer,
  18. num_pulls integer,
  19. num_closed_pulls integer,
  20. num_milestones integer DEFAULT 0 NOT NULL,
  21. num_closed_milestones integer DEFAULT 0 NOT NULL,
  22. is_private boolean,
  23. is_empty boolean,
  24. is_archived boolean,
  25. is_mirror boolean,
  26. status integer DEFAULT 0 NOT NULL,
  27. is_fork boolean DEFAULT false NOT NULL,
  28. fork_id bigint,
  29. is_template boolean DEFAULT false NOT NULL,
  30. template_id bigint,
  31. size bigint DEFAULT 0 NOT NULL,
  32. is_fsck_enabled boolean DEFAULT true NOT NULL,
  33. close_issues_via_commit_in_any_branch boolean DEFAULT false NOT NULL,
  34. topics text,
  35. avatar character varying(64),
  36. created_unix bigint,
  37. updated_unix bigint,
  38. contract_address character varying(255),
  39. block_chain_status integer DEFAULT 0 NOT NULL,
  40. balance character varying(255) DEFAULT '0'::character varying NOT NULL,
  41. clone_cnt bigint DEFAULT 0 NOT NULL,
  42. license character varying(100),
  43. download_cnt bigint DEFAULT 0 NOT NULL,
  44. num_commit bigint DEFAULT 0 NOT NULL,
  45. git_clone_cnt bigint DEFAULT 0 NOT NULL,
  46. lang character varying(2048)
  47. ) SERVER multicorn_es
  48. OPTIONS
  49. (
  50. host '192.168.207.94',
  51. port '9200',
  52. index 'repository-es-index',
  53. rowid_column 'id',
  54. default_sort '_id'
  55. )
  56. ;
  57. delete from public.repository_es;
  58. INSERT INTO public.repository_es (id,
  59. owner_id,
  60. owner_name,
  61. lower_name,
  62. name,
  63. description,
  64. website,
  65. original_service_type,
  66. original_url,
  67. default_branch,
  68. num_watches,
  69. num_stars,
  70. num_forks,
  71. num_issues,
  72. num_closed_issues,
  73. num_pulls,
  74. num_closed_pulls,
  75. num_milestones,
  76. num_closed_milestones,
  77. is_private,
  78. is_empty,
  79. is_archived,
  80. is_mirror,
  81. status,
  82. is_fork,
  83. fork_id,
  84. is_template,
  85. template_id,
  86. size,
  87. is_fsck_enabled,
  88. close_issues_via_commit_in_any_branch,
  89. topics,
  90. avatar,
  91. created_unix,
  92. updated_unix,
  93. contract_address,
  94. block_chain_status,
  95. balance,
  96. clone_cnt,
  97. num_commit,
  98. git_clone_cnt)
  99. SELECT
  100. id,
  101. owner_id,
  102. owner_name,
  103. lower_name,
  104. name,
  105. description,
  106. website,
  107. original_service_type,
  108. original_url,
  109. default_branch,
  110. num_watches,
  111. num_stars,
  112. num_forks,
  113. num_issues,
  114. num_closed_issues,
  115. num_pulls,
  116. num_closed_pulls,
  117. num_milestones,
  118. num_closed_milestones,
  119. is_private,
  120. is_empty,
  121. is_archived,
  122. is_mirror,
  123. status,
  124. is_fork,
  125. fork_id,
  126. is_template,
  127. template_id,
  128. size,
  129. is_fsck_enabled,
  130. close_issues_via_commit_in_any_branch,
  131. topics,
  132. avatar,
  133. created_unix,
  134. updated_unix,
  135. contract_address,
  136. block_chain_status,
  137. balance,
  138. clone_cnt,
  139. num_commit,
  140. git_clone_cnt
  141. FROM public.repository;
  142. CREATE OR REPLACE FUNCTION public.insert_repository_data() RETURNS trigger AS
  143. $def$
  144. BEGIN
  145. INSERT INTO public.repository_es (id,
  146. owner_id,
  147. owner_name,
  148. lower_name,
  149. name,
  150. description,
  151. website,
  152. original_service_type,
  153. original_url,
  154. default_branch,
  155. num_watches,
  156. num_stars,
  157. num_forks,
  158. num_issues,
  159. num_closed_issues,
  160. num_pulls,
  161. num_closed_pulls,
  162. num_milestones,
  163. num_closed_milestones,
  164. is_private,
  165. is_empty,
  166. is_archived,
  167. is_mirror,
  168. status,
  169. is_fork,
  170. fork_id,
  171. is_template,
  172. template_id,
  173. size,
  174. is_fsck_enabled,
  175. close_issues_via_commit_in_any_branch,
  176. topics,
  177. avatar,
  178. created_unix,
  179. updated_unix,
  180. contract_address,
  181. block_chain_status,
  182. balance,
  183. clone_cnt,
  184. num_commit,
  185. git_clone_cnt) VALUES
  186. (NEW.id,
  187. NEW.owner_id,
  188. NEW.owner_name,
  189. NEW.lower_name,
  190. NEW.name,
  191. NEW.description,
  192. NEW.website,
  193. NEW.original_service_type,
  194. NEW.original_url,
  195. NEW.default_branch,
  196. NEW.num_watches,
  197. NEW.num_stars,
  198. NEW.num_forks,
  199. NEW.num_issues,
  200. NEW.num_closed_issues,
  201. NEW.num_pulls,
  202. NEW.num_closed_pulls,
  203. NEW.num_milestones,
  204. NEW.num_closed_milestones,
  205. NEW.is_private,
  206. NEW.is_empty,
  207. NEW.is_archived,
  208. NEW.is_mirror,
  209. NEW.status,
  210. NEW.is_fork,
  211. NEW.fork_id,
  212. NEW.is_template,
  213. NEW.template_id,
  214. NEW.size,
  215. NEW.is_fsck_enabled,
  216. NEW.close_issues_via_commit_in_any_branch,
  217. NEW.topics,
  218. NEW.avatar,
  219. NEW.created_unix,
  220. NEW.updated_unix,
  221. NEW.contract_address,
  222. NEW.block_chain_status,
  223. NEW.balance,
  224. NEW.clone_cnt,
  225. NEW.num_commit,
  226. NEW.git_clone_cnt);
  227. RETURN NEW;
  228. END;
  229. $def$
  230. LANGUAGE plpgsql;
  231. DROP TRIGGER IF EXISTS es_insert_repository on public.repository;
  232. CREATE TRIGGER es_insert_repository
  233. AFTER INSERT ON public.repository
  234. FOR EACH ROW EXECUTE PROCEDURE insert_repository_data();
  235. CREATE OR REPLACE FUNCTION public.update_repository() RETURNS trigger AS
  236. $def$
  237. BEGIN
  238. update public.repository_es SET description=NEW.description,
  239. name=NEW.name,
  240. lower_name=NEW.lower_name,
  241. owner_name=NEW.owner_name,
  242. website=NEW.website,
  243. updated_unix=NEW.updated_unix,
  244. num_watches=NEW.num_watches,
  245. num_stars=NEW.num_stars,
  246. num_forks=NEW.num_forks,
  247. topics=NEW.topics
  248. where id=NEW.id;
  249. return new;
  250. END
  251. $def$
  252. LANGUAGE plpgsql;
  253. DROP TRIGGER IF EXISTS es_update_repository on public.repository;
  254. CREATE TRIGGER es_update_repository
  255. AFTER UPDATE ON public.repository
  256. FOR EACH ROW EXECUTE PROCEDURE update_repository();
  257. CREATE OR REPLACE FUNCTION public.delete_repository() RETURNS trigger AS
  258. $def$
  259. declare
  260. BEGIN
  261. DELETE FROM public.repository_es where id=OLD.id;
  262. return new;
  263. END
  264. $def$
  265. LANGUAGE plpgsql;
  266. DROP TRIGGER IF EXISTS es_delete_repository on public.repository;
  267. CREATE TRIGGER es_delete_repository
  268. AFTER DELETE ON public.repository
  269. FOR EACH ROW EXECUTE PROCEDURE delete_repository();
  270. CREATE OR REPLACE FUNCTION public.udpate_repository_lang() RETURNS trigger AS
  271. $def$
  272. BEGIN
  273. if (TG_OP = 'DELETE') then
  274. update public.repository_es SET lang=(select string_agg(language, ',') from public.language_stat where repo_id=OLD.repo_id) where id=OLD.repo_id;
  275. elsif (TG_OP = 'UPDATE') then
  276. update public.repository_es SET lang=(select string_agg(language, ',') from public.language_stat where repo_id=NEW.repo_id) where id=NEW.repo_id;
  277. elsif (TG_OP = 'INSERT') then
  278. update public.repository_es SET lang=(select string_agg(language, ',') from public.language_stat where repo_id=NEW.repo_id) where id=NEW.repo_id;
  279. end if;
  280. return null;
  281. END;
  282. $def$
  283. LANGUAGE plpgsql;
  284. DROP TRIGGER IF EXISTS es_udpate_repository_lang on public.language_stat;
  285. CREATE TRIGGER es_udpate_repository_lang
  286. AFTER INSERT OR UPDATE OR DELETE ON public.language_stat
  287. FOR EACH ROW EXECUTE PROCEDURE udpate_repository_lang();