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 'user_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 
	        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
            FROM public.pull_request;



CREATE OR REPLACE FUNCTION public.insert_pull_request_data() RETURNS trigger AS 
$def$
    BEGIN
        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
            );
        
        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();



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();


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();