#!/usr/bin/env bash

echo -en "\tStarting loading at "
date

docker exec postgres_engine psql -h0 -U postgres \
  -c "DROP TABLE IF EXISTS $test;
    CREATE TABLE $test(
    id bigint,
    story_id bigint,
    story_text text,
    story_author varchar(1024),
    comment_id bigint,
    comment_text text,
    comment_author varchar(1024),
    comment_ranking bigint,
    author_comment_count bigint,
    story_comment_count bigint,
    story_text_ts tsvector GENERATED ALWAYS AS (to_tsvector('english', story_text)) STORED,
    story_author_ts tsvector GENERATED ALWAYS AS (to_tsvector('english', story_author)) STORED,
    comment_text_ts tsvector GENERATED ALWAYS AS (to_tsvector('english', comment_text)) STORED,
    comment_author_ts tsvector GENERATED ALWAYS AS (to_tsvector('english', comment_author)) STORED);
    CREATE INDEX ts_idx ON $test USING GIN (story_text_ts, story_author_ts, comment_text_ts, comment_author_ts);
    CREATE INDEX comment_ranking_idx ON $test (comment_ranking);
    CREATE INDEX author_comment_count_idx ON $test (author_comment_count);
    CREATE INDEX story_comment_count_idx ON $test (story_comment_count);"

docker exec postgres_engine psql -h0 -U postgres -c "COPY $test FROM '/input/data.csv' WITH (FORMAT csv);"
