DROP FOREIGN TABLE public.pull_request_es;
CREATE FOREIGN TABLE public.pull_request_es
(
    id bigint NOT NULL,
    type integer,
    status integer,
    conflicted_files json,
    commits_ahead integer,
    commits_behind integer,
    issue_id bigint,
    index bigint,
    head_repo_id bigint,
    base_repo_id bigint,
    head_branch character varying(255),
    base_branch character varying(255),
    merge_base character varying(40),
    has_merged boolean,
    merged_commit_id character varying(40),
    merger_id bigint,
    merged_unix bigint,
    is_transformed boolean NOT NULL DEFAULT false,
    amount integer NOT NULL DEFAULT 0
)SERVER multicorn_es
OPTIONS
    (
        host '192.168.207.94',
        port '9200',
        index 'pr-es-index',
        rowid_column 'id',
        default_sort '_id'
    )
;

delete from public.pull_request_es;
 INSERT INTO public.pull_request_es(
	        id, 
            type, 
            status, 
            conflicted_files, 
            commits_ahead, 
            commits_behind, 
            issue_id, 
            index, 
            head_repo_id, 
            base_repo_id, 
            head_branch, 
            base_branch, 
            merge_base, 
            has_merged, 
            merged_commit_id, 
            merger_id, 
            merged_unix, 
            is_transformed, 
            amount)
	        SELECT 
	        b.id, 
            b.type, 
            b.status, 
            b.conflicted_files, 
            b.commits_ahead, 
            b.commits_behind, 
            b.issue_id, 
            b.index, 
            b.head_repo_id, 
            b.base_repo_id, 
            b.head_branch, 
            b.base_branch, 
            b.merge_base, 
            b.has_merged, 
            b.merged_commit_id, 
            b.merger_id, 
            b.merged_unix, 
            b.is_transformed, 
            b.amount
            FROM public.pull_request b,public.repository c where b.base_repo_id=c.id and c.is_private=false;


CREATE OR REPLACE FUNCTION public.insert_pull_request_data() RETURNS trigger AS 
$def$
    DECLARE
        privateValue boolean=false;
    BEGIN
        select into privateValue is_private from public.repository where id=NEW.base_repo_id;
        if not privateValue then
            INSERT INTO public.pull_request_es(
                id, 
                type, 
                status, 
                conflicted_files, 
                commits_ahead, 
                commits_behind, 
                issue_id, 
                index, 
                head_repo_id, 
                base_repo_id, 
                head_branch, 
                base_branch, 
                merge_base, 
                has_merged, 
                merged_commit_id, 
                merger_id, 
                merged_unix, 
                is_transformed, 
                amount)
                VALUES (
                NEW.id, 
                NEW.type, 
                NEW.status, 
                NEW.conflicted_files, 
                NEW.commits_ahead, 
                NEW.commits_behind, 
                NEW.issue_id, 
                NEW.index, 
                NEW.head_repo_id, 
                NEW.base_repo_id, 
                NEW.head_branch, 
                NEW.base_branch, 
                NEW.merge_base, 
                NEW.has_merged, 
                NEW.merged_commit_id, 
                NEW.merger_id, 
                NEW.merged_unix, 
                NEW.is_transformed, 
                NEW.amount
                );
            
        UPDATE public.issue_es SET pr_id=NEW.id where id=NEW.issue_id;
        end if;
        RETURN NEW;
    END;
$def$ 
LANGUAGE plpgsql;

DROP TRIGGER  IF EXISTS es_insert_pull_request on public.pull_request;

CREATE TRIGGER es_insert_pull_request
    AFTER INSERT ON public.pull_request
    FOR EACH ROW EXECUTE PROCEDURE insert_pull_request_data();

ALTER TABLE public.pull_request ENABLE ALWAYS TRIGGER es_insert_pull_request;

CREATE OR REPLACE FUNCTION public.update_pull_request() RETURNS trigger AS 
$def$
    declare
    BEGIN
	   UPDATE public.pull_request_es 
       SET has_merged=NEW.has_merged 
       where id=NEW.id;
       return new;
    END
$def$ 
LANGUAGE plpgsql;

DROP TRIGGER  IF EXISTS es_update_pull_request on public.pull_request;

CREATE TRIGGER es_update_pull_request
    AFTER UPDATE ON public.pull_request
    FOR EACH ROW EXECUTE PROCEDURE update_pull_request();

ALTER TABLE public.pull_request ENABLE ALWAYS TRIGGER es_update_pull_request;

CREATE OR REPLACE FUNCTION public.delete_pull_request() RETURNS trigger AS 
$def$
    declare
    BEGIN
	   DELETE FROM public.pull_request_es  where id=OLD.id;
       return new;
    END
$def$ 
LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS es_delete_pull_request on public.pull_request;
CREATE TRIGGER es_delete_pull_request
    AFTER DELETE ON public.pull_request
    FOR EACH ROW EXECUTE PROCEDURE delete_pull_request();

ALTER TABLE public.pull_request ENABLE ALWAYS TRIGGER es_delete_pull_request;