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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. -- 要处理项目从私有变为公有,并且从公有变成私有的情况
  2. DELETE FROM public.repository_es;
  3. DROP FOREIGN table if exists public.repository_es;
  4. DROP TRIGGER IF EXISTS es_insert_repository on public.repository;
  5. DROP FUNCTION public.insert_repository_data;
  6. DROP TRIGGER IF EXISTS es_update_repository on public.repository;
  7. DROP FUNCTION public.update_repository;
  8. DROP TRIGGER IF EXISTS es_delete_repository on public.repository;
  9. DROP FUNCTION public.delete_repository;
  10. DROP TRIGGER IF EXISTS es_udpate_repository_lang on public.language_stat;
  11. DROP FUNCTION public.udpate_repository_lang;
  12. CREATE FOREIGN TABLE public.repository_es (
  13. id bigint NOT NULL,
  14. owner_id bigint,
  15. owner_name character varying(255),
  16. lower_name character varying(255) NOT NULL,
  17. name character varying(255) NOT NULL,
  18. description text,
  19. website character varying(2048),
  20. original_service_type integer,
  21. original_url character varying(2048),
  22. default_branch character varying(255),
  23. num_watches integer,
  24. num_stars integer,
  25. num_forks integer,
  26. num_issues integer,
  27. num_closed_issues integer,
  28. num_pulls integer,
  29. num_closed_pulls integer,
  30. num_milestones integer DEFAULT 0 NOT NULL,
  31. num_closed_milestones integer DEFAULT 0 NOT NULL,
  32. is_private boolean,
  33. is_empty boolean,
  34. is_archived boolean,
  35. is_mirror boolean,
  36. status integer DEFAULT 0 NOT NULL,
  37. is_fork boolean DEFAULT false NOT NULL,
  38. fork_id bigint,
  39. is_template boolean DEFAULT false NOT NULL,
  40. template_id bigint,
  41. size bigint DEFAULT 0 NOT NULL,
  42. is_fsck_enabled boolean DEFAULT true NOT NULL,
  43. close_issues_via_commit_in_any_branch boolean DEFAULT false NOT NULL,
  44. topics text,
  45. avatar character varying(64),
  46. created_unix bigint,
  47. updated_unix bigint,
  48. contract_address character varying(255),
  49. block_chain_status integer DEFAULT 0 NOT NULL,
  50. balance character varying(255) DEFAULT '0'::character varying NOT NULL,
  51. clone_cnt bigint DEFAULT 0 NOT NULL,
  52. license character varying(100),
  53. download_cnt bigint DEFAULT 0 NOT NULL,
  54. num_commit bigint DEFAULT 0 NOT NULL,
  55. git_clone_cnt bigint DEFAULT 0 NOT NULL,
  56. creator_id bigint NOT NULL DEFAULT 0,
  57. repo_type integer NOT NULL DEFAULT 0,
  58. lang character varying(2048),
  59. alias character varying(255),
  60. lower_alias character varying(255)
  61. ) SERVER multicorn_es
  62. OPTIONS
  63. (
  64. host '192.168.207.94',
  65. port '9200',
  66. index 'repository-es-index',
  67. rowid_column 'id',
  68. default_sort '_id'
  69. )
  70. ;
  71. delete from public.repository_es;
  72. INSERT INTO public.repository_es (id,
  73. owner_id,
  74. owner_name,
  75. lower_name,
  76. name,
  77. description,
  78. website,
  79. original_service_type,
  80. original_url,
  81. default_branch,
  82. num_watches,
  83. num_stars,
  84. num_forks,
  85. num_issues,
  86. num_closed_issues,
  87. num_pulls,
  88. num_closed_pulls,
  89. num_milestones,
  90. num_closed_milestones,
  91. is_private,
  92. is_empty,
  93. is_archived,
  94. is_mirror,
  95. status,
  96. is_fork,
  97. fork_id,
  98. is_template,
  99. template_id,
  100. size,
  101. is_fsck_enabled,
  102. close_issues_via_commit_in_any_branch,
  103. topics,
  104. avatar,
  105. created_unix,
  106. updated_unix,
  107. contract_address,
  108. block_chain_status,
  109. balance,
  110. clone_cnt,
  111. num_commit,
  112. git_clone_cnt,
  113. creator_id,
  114. repo_type,
  115. lang,
  116. alias,
  117. lower_alias
  118. )
  119. SELECT
  120. id,
  121. owner_id,
  122. owner_name,
  123. lower_name,
  124. name,
  125. description,
  126. website,
  127. original_service_type,
  128. original_url,
  129. default_branch,
  130. num_watches,
  131. num_stars,
  132. num_forks,
  133. num_issues,
  134. num_closed_issues,
  135. num_pulls,
  136. num_closed_pulls,
  137. num_milestones,
  138. num_closed_milestones,
  139. is_private,
  140. is_empty,
  141. is_archived,
  142. is_mirror,
  143. status,
  144. is_fork,
  145. fork_id,
  146. is_template,
  147. template_id,
  148. size,
  149. is_fsck_enabled,
  150. close_issues_via_commit_in_any_branch,
  151. topics,
  152. avatar,
  153. created_unix,
  154. updated_unix,
  155. contract_address,
  156. block_chain_status,
  157. balance,
  158. clone_cnt,
  159. num_commit,
  160. git_clone_cnt,
  161. creator_id,
  162. repo_type,
  163. (select array_to_string(array_agg(language order by percentage desc),',') from public.language_stat a where a.repo_id=b.id),
  164. alias,
  165. lower_alias
  166. FROM public.repository b where b.is_private=false;
  167. DROP TRIGGER IF EXISTS es_insert_repository on public.repository;
  168. CREATE OR REPLACE FUNCTION public.insert_repository_data() RETURNS trigger AS
  169. $def$
  170. BEGIN
  171. if not NEW.is_private then
  172. INSERT INTO public.repository_es (id,
  173. owner_id,
  174. owner_name,
  175. lower_name,
  176. name,
  177. description,
  178. website,
  179. original_service_type,
  180. original_url,
  181. default_branch,
  182. num_watches,
  183. num_stars,
  184. num_forks,
  185. num_issues,
  186. num_closed_issues,
  187. num_pulls,
  188. num_closed_pulls,
  189. num_milestones,
  190. num_closed_milestones,
  191. is_private,
  192. is_empty,
  193. is_archived,
  194. is_mirror,
  195. status,
  196. is_fork,
  197. fork_id,
  198. is_template,
  199. template_id,
  200. size,
  201. is_fsck_enabled,
  202. close_issues_via_commit_in_any_branch,
  203. topics,
  204. avatar,
  205. created_unix,
  206. updated_unix,
  207. contract_address,
  208. block_chain_status,
  209. balance,
  210. clone_cnt,
  211. num_commit,
  212. git_clone_cnt,
  213. creator_id,
  214. repo_type,
  215. alias,
  216. lower_alias) VALUES
  217. (NEW.id,
  218. NEW.owner_id,
  219. NEW.owner_name,
  220. NEW.lower_name,
  221. NEW.name,
  222. NEW.description,
  223. NEW.website,
  224. NEW.original_service_type,
  225. NEW.original_url,
  226. NEW.default_branch,
  227. NEW.num_watches,
  228. NEW.num_stars,
  229. NEW.num_forks,
  230. NEW.num_issues,
  231. NEW.num_closed_issues,
  232. NEW.num_pulls,
  233. NEW.num_closed_pulls,
  234. NEW.num_milestones,
  235. NEW.num_closed_milestones,
  236. NEW.is_private,
  237. NEW.is_empty,
  238. NEW.is_archived,
  239. NEW.is_mirror,
  240. NEW.status,
  241. NEW.is_fork,
  242. NEW.fork_id,
  243. NEW.is_template,
  244. NEW.template_id,
  245. NEW.size,
  246. NEW.is_fsck_enabled,
  247. NEW.close_issues_via_commit_in_any_branch,
  248. NEW.topics,
  249. NEW.avatar,
  250. NEW.created_unix,
  251. NEW.updated_unix,
  252. NEW.contract_address,
  253. NEW.block_chain_status,
  254. NEW.balance,
  255. NEW.clone_cnt,
  256. NEW.num_commit,
  257. NEW.git_clone_cnt,
  258. NEW.creator_id,
  259. NEW.repo_type,
  260. NEW.alias,
  261. NEW.lower_alias);
  262. end if;
  263. RETURN NEW;
  264. END;
  265. $def$
  266. LANGUAGE plpgsql;
  267. CREATE TRIGGER es_insert_repository
  268. AFTER INSERT ON public.repository
  269. FOR EACH ROW EXECUTE PROCEDURE insert_repository_data();
  270. ALTER TABLE public.repository ENABLE ALWAYS TRIGGER es_insert_repository;
  271. DROP TRIGGER IF EXISTS es_update_repository on public.repository;
  272. CREATE OR REPLACE FUNCTION public.update_repository() RETURNS trigger AS
  273. $def$
  274. BEGIN
  275. if OLD.is_private != NEW.is_private then
  276. if OLD.is_private and not NEW.is_private then
  277. --insert
  278. INSERT INTO public.repository_es (id,
  279. owner_id,
  280. owner_name,
  281. lower_name,
  282. name,
  283. description,
  284. website,
  285. original_service_type,
  286. original_url,
  287. default_branch,
  288. num_watches,
  289. num_stars,
  290. num_forks,
  291. num_issues,
  292. num_closed_issues,
  293. num_pulls,
  294. num_closed_pulls,
  295. num_milestones,
  296. num_closed_milestones,
  297. is_private,
  298. is_empty,
  299. is_archived,
  300. is_mirror,
  301. status,
  302. is_fork,
  303. fork_id,
  304. is_template,
  305. template_id,
  306. size,
  307. is_fsck_enabled,
  308. close_issues_via_commit_in_any_branch,
  309. topics,
  310. avatar,
  311. created_unix,
  312. updated_unix,
  313. contract_address,
  314. block_chain_status,
  315. balance,
  316. clone_cnt,
  317. num_commit,
  318. git_clone_cnt,
  319. creator_id,
  320. repo_type,
  321. lang,
  322. alias,
  323. lower_alias)
  324. SELECT
  325. id,
  326. owner_id,
  327. owner_name,
  328. lower_name,
  329. name,
  330. description,
  331. website,
  332. original_service_type,
  333. original_url,
  334. default_branch,
  335. num_watches,
  336. num_stars,
  337. num_forks,
  338. num_issues,
  339. num_closed_issues,
  340. num_pulls,
  341. num_closed_pulls,
  342. num_milestones,
  343. num_closed_milestones,
  344. is_private,
  345. is_empty,
  346. is_archived,
  347. is_mirror,
  348. status,
  349. is_fork,
  350. fork_id,
  351. is_template,
  352. template_id,
  353. size,
  354. is_fsck_enabled,
  355. close_issues_via_commit_in_any_branch,
  356. topics,
  357. avatar,
  358. created_unix,
  359. updated_unix,
  360. contract_address,
  361. block_chain_status,
  362. balance,
  363. clone_cnt,
  364. num_commit,
  365. git_clone_cnt,
  366. creator_id,
  367. repo_type,
  368. (select array_to_string(array_agg(language order by percentage desc),',') from public.language_stat a where a.repo_id=b.id),
  369. alias,
  370. lower_alias
  371. FROM public.repository b where b.id=NEW.id;
  372. INSERT INTO public.dataset_es(
  373. id,
  374. title,
  375. status,
  376. category,
  377. description,
  378. download_times,
  379. license, task,
  380. release_id,
  381. user_id,
  382. repo_id,
  383. created_unix,
  384. updated_unix,file_name)
  385. SELECT
  386. b.id,
  387. b.title,
  388. b.status,
  389. b.category,
  390. b.description,
  391. b.download_times,
  392. b.license,
  393. b.task,
  394. b.release_id,
  395. b.user_id,
  396. b.repo_id,
  397. b.created_unix,
  398. b.updated_unix,(select array_to_string(array_agg(name order by created_unix desc),',') from public.attachment a where a.dataset_id=b.id and a.is_private=false)
  399. FROM public.dataset b where b.repo_id=NEW.id;
  400. INSERT INTO public.issue_es(
  401. id,
  402. repo_id,
  403. index,
  404. poster_id,
  405. original_author,
  406. original_author_id,
  407. name,
  408. content,
  409. milestone_id,
  410. priority,
  411. is_closed,
  412. is_pull,
  413. num_comments,
  414. ref,
  415. deadline_unix,
  416. created_unix,
  417. updated_unix,
  418. closed_unix,
  419. is_locked,
  420. amount,
  421. is_transformed,comment,pr_id)
  422. SELECT
  423. b.id,
  424. b.repo_id,
  425. b.index,
  426. b.poster_id,
  427. b.original_author,
  428. b.original_author_id,
  429. b.name,
  430. b.content,
  431. b.milestone_id,
  432. b.priority,
  433. b.is_closed,
  434. b.is_pull,
  435. b.num_comments,
  436. b.ref,
  437. b.deadline_unix,
  438. b.created_unix,
  439. b.updated_unix,
  440. b.closed_unix,
  441. b.is_locked,
  442. b.amount,
  443. b.is_transformed,
  444. (select array_to_string(array_agg(content order by created_unix desc),',') from public.comment a where a.issue_id=b.id),
  445. (select id from public.pull_request d where d.issue_id=b.id)
  446. FROM public.issue b where b.repo_id=NEW.id;
  447. end if;
  448. if not OLD.is_private and NEW.is_private then
  449. delete from public.issue_es where repo_id=NEW.id;
  450. -- delete from public.dataset_es where repo_id=NEW.id;
  451. delete from public.repository_es where id=NEW.id;
  452. end if;
  453. end if;
  454. if not NEW.is_private then
  455. raise notice 'update repo,the updated_unix is %',NEW.updated_unix;
  456. update public.repository_es SET description=NEW.description,
  457. name=NEW.name,
  458. lower_name=NEW.lower_name,
  459. owner_name=NEW.owner_name,
  460. website=NEW.website,
  461. updated_unix=NEW.updated_unix,
  462. num_watches=NEW.num_watches,
  463. num_stars=NEW.num_stars,
  464. num_forks=NEW.num_forks,
  465. topics=NEW.topics,
  466. alias = NEW.alias,
  467. lower_alias = NEW.lower_alias,
  468. avatar=NEW.avatar
  469. where id=NEW.id;
  470. end if;
  471. return new;
  472. END
  473. $def$
  474. LANGUAGE plpgsql;
  475. CREATE TRIGGER es_update_repository
  476. AFTER UPDATE ON public.repository
  477. FOR EACH ROW EXECUTE PROCEDURE update_repository();
  478. ALTER TABLE public.repository ENABLE ALWAYS TRIGGER es_update_repository;
  479. DROP TRIGGER IF EXISTS es_delete_repository on public.repository;
  480. CREATE OR REPLACE FUNCTION public.delete_repository() RETURNS trigger AS
  481. $def$
  482. declare
  483. BEGIN
  484. delete from public.issue_es where repo_id=OLD.id;
  485. delete from public.dataset_es where repo_id=OLD.id;
  486. DELETE FROM public.repository_es where id=OLD.id;
  487. return new;
  488. END
  489. $def$
  490. LANGUAGE plpgsql;
  491. CREATE TRIGGER es_delete_repository
  492. AFTER DELETE ON public.repository
  493. FOR EACH ROW EXECUTE PROCEDURE delete_repository();
  494. ALTER TABLE public.repository ENABLE ALWAYS TRIGGER es_delete_repository;
  495. DROP TRIGGER IF EXISTS es_udpate_repository_lang on public.language_stat;
  496. CREATE OR REPLACE FUNCTION public.udpate_repository_lang() RETURNS trigger AS
  497. $def$
  498. DECLARE
  499. privateValue bigint;
  500. BEGIN
  501. if (TG_OP = 'UPDATE') then
  502. select into privateValue updated_unix from public.repository where id=NEW.repo_id;
  503. update public.repository_es SET updated_unix=privateValue,lang=(select array_to_string(array_agg(language order by percentage desc),',') from public.language_stat where repo_id=NEW.repo_id) where id=NEW.repo_id;
  504. elsif (TG_OP = 'INSERT') then
  505. select into privateValue updated_unix from public.repository where id=NEW.repo_id;
  506. update public.repository_es SET updated_unix=privateValue,lang=(select array_to_string(array_agg(language order by percentage desc),',') from public.language_stat where repo_id=NEW.repo_id) where id=NEW.repo_id;
  507. elsif (TG_OP = 'DELETE') then
  508. if exists(select 1 from public.repository where id=OLD.repo_id) then
  509. update public.repository_es SET lang=(select array_to_string(array_agg(language order by percentage desc),',') from public.language_stat where repo_id=OLD.repo_id) where id=OLD.repo_id;
  510. end if;
  511. end if;
  512. return NEW;
  513. END;
  514. $def$
  515. LANGUAGE plpgsql;
  516. CREATE TRIGGER es_udpate_repository_lang
  517. AFTER INSERT OR UPDATE OR DELETE ON public.language_stat
  518. FOR EACH ROW EXECUTE PROCEDURE udpate_repository_lang();
  519. ALTER TABLE public.language_stat ENABLE ALWAYS TRIGGER es_udpate_repository_lang;