CREATE FOREIGN TABLE public.issue_es
(
    id bigint NOT NULL,
    repo_id bigint,
    index bigint,
    poster_id bigint,
    original_author character varying(255),
    original_author_id bigint,
    name character varying(255) ,
    content text,
    comment text,
    milestone_id bigint,
    priority integer,
    is_closed boolean,
    is_pull boolean,
    pr_id bigint,
    num_comments integer,
    ref character varying(255),
    deadline_unix bigint,
    created_unix bigint,
    updated_unix bigint,
    closed_unix bigint,
    is_locked boolean NOT NULL,
    amount bigint,
    is_transformed boolean NOT NULL,
)SERVER multicorn_es
OPTIONS
    (
        host '192.168.207.94',
        port '9200',
        index 'user_es-index',
        rowid_column 'id'
    )
;

CREATE OR REPLACE FUNCTION public.insert_issue_data() RETURNS trigger AS 
$def$
    BEGIN
         INSERT INTO public.issue_es(
	     id, 
         repo_id, 
         index, 
         poster_id, 
         original_author, 
         original_author_id, 
         name, 
         content, 
         milestone_id, 
         priority, 
         is_closed, 
         is_pull, 
         num_comments, 
         ref, 
         deadline_unix, 
         created_unix, 
         updated_unix, 
         closed_unix, 
         is_locked, 
         amount, 
         is_transformed)
	     VALUES (
         NEW.id, 
         NEW.repo_id, 
         NEW.index, 
         NEW.poster_id, 
         NEW.original_author, 
         NEW.original_author_id, 
         NEW.name, 
         NEW.content, 
         NEW.milestone_id, 
         NEW.priority, 
         NEW.is_closed, 
         NEW.is_pull, 
         NEW.num_comments, 
         NEW.ref, 
         NEW.deadline_unix, 
         NEW.created_unix, 
         NEW.updated_unix, 
         NEW.closed_unix, 
         NEW.is_locked, 
         NEW.amount, 
         NEW.is_transformed    
         );
    
        RETURN NEW;
    END;
$def$ 
LANGUAGE plpgsql;

DROP TRIGGER  IF EXISTS es_insert_issue on public.issue;

CREATE TRIGGER es_insert_issue
    AFTER INSERT ON public.issue
    FOR EACH ROW EXECUTE PROCEDURE insert_issue_data();



CREATE OR REPLACE FUNCTION public.udpate_issue_comment() RETURNS trigger AS 
$def$
    BEGIN
       if (TG_OP = 'DELETE') then
	       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 order by created_unix desc) where id=OLD.issue_id;
	   elsif (TG_OP = 'INSERT') then
	       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 order by created_unix desc) where id=NEW.issue_id;
	   elsif (TG_OP = 'UPDATE') then
	       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 order by created_unix desc) where id=NEW.issue_id;
       end if;
	   
       return null;
    END;
$def$ 
LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS es_udpate_issue_comment on public.comment;
CREATE TRIGGER es_udpate_issue_comment
    AFTER INSERT OR DELETE OR UPDATE ON public.comment
    FOR EACH ROW EXECUTE PROCEDURE udpate_issue_comment();


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

DROP TRIGGER  IF EXISTS es_update_issue_content on public.issue;

CREATE TRIGGER es_update_issue_content
    AFTER UPDATE OF "content" ON public.issue
    FOR EACH ROW EXECUTE PROCEDURE update_issue_content();


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

DROP TRIGGER  IF EXISTS es_update_issue_name on public.issue;

CREATE TRIGGER es_update_issue_name
    AFTER UPDATE OF "name" ON public.issue
    FOR EACH ROW EXECUTE PROCEDURE update_issue_name();


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

DROP TRIGGER  IF EXISTS es_update_issue_is_closed on public.issue;

CREATE TRIGGER es_update_issue_is_closed
    AFTER UPDATE OF "is_closed" ON public.issue
    FOR EACH ROW EXECUTE PROCEDURE update_issue_is_closed();

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

DROP TRIGGER  IF EXISTS es_update_issue_num_comments on public.issue;

CREATE TRIGGER es_update_issue_num_comments
    AFTER UPDATE OF "num_comments" ON public.issue
    FOR EACH ROW EXECUTE PROCEDURE update_issue_num_comments();


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

DROP TRIGGER IF EXISTS es_delete_issue on public.issue;
CREATE TRIGGER es_delete_issue
    AFTER DELETE ON public.issue
    FOR EACH ROW EXECUTE PROCEDURE delete_issue();