-- Update the current messages table so it stores the year that we'll partition
-- on.

ALTER TABLE messages ADD COLUMN year integer;
update messages set year = extract(year from timestamp);



-- Create the messages2 table that will be partitioned and will have the messages
-- moved to

CREATE TABLE messages2 (
    id integer NOT NULL,
    i integer NOT NULL,
    "timestamp" timestamp without time zone NOT NULL,
    year integer,
    certificate text,
    signature text,
    topic text,
    _msg text NOT NULL,
    category text,
    source_name text,
    source_version text,
    msg_id text,
    _headers text,
    username text,
    crypto text
) PARTITION BY LIST(year);

CREATE SEQUENCE public.messages2_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER TABLE public.messages2_id_seq OWNER TO datanommer;
ALTER SEQUENCE public.messages2_id_seq OWNED BY public.messages2.id;
ALTER TABLE ONLY public.messages2 ALTER COLUMN id SET DEFAULT nextval('public.messages2_id_seq'::regclass);
ALTER TABLE ONLY public.messages2
    ADD CONSTRAINT messages2_msg_id_key UNIQUE (msg_id);
ALTER TABLE ONLY public.messages2
    ADD CONSTRAINT messages2_pkey PRIMARY KEY (id);
CREATE INDEX index_msg2_category ON public.messages2 USING btree (category);
CREATE INDEX index_msg2_timestamp ON public.messages2 USING btree ("timestamp");
CREATE INDEX index_msg2_topic ON public.messages2 USING btree (topic);
CREATE INDEX messages2_datanommer_timestamp_category_idx ON public.messages2 USING btree ("timestamp" DESC, category);
CREATE INDEX messages2_datanommer_timestamp_topic_idx ON public.messages2 USING btree ("timestamp" DESC, topic);
GRANT SELECT ON TABLE public.messages2 TO datagrepper;
GRANT SELECT ON SEQUENCE public.messages2_id_seq TO datagrepper;

-- Create the partitions

CREATE TABLE messages_2012 PARTITION OF messages FOR VALUES IN (2012);
CREATE TABLE messages_2013 PARTITION OF messages FOR VALUES IN (2013);
CREATE TABLE messages_2014 PARTITION OF messages FOR VALUES IN (2014);
CREATE TABLE messages_2015 PARTITION OF messages FOR VALUES IN (2015);
CREATE TABLE messages_2016 PARTITION OF messages FOR VALUES IN (2016);
CREATE TABLE messages_2017 PARTITION OF messages FOR VALUES IN (2017);
CREATE TABLE messages_2018 PARTITION OF messages FOR VALUES IN (2018);
CREATE TABLE messages_2019 PARTITION OF messages FOR VALUES IN (2019);
CREATE TABLE messages_2020 PARTITION OF messages FOR VALUES IN (2020);
CREATE TABLE messages_2021 PARTITION OF messages FOR VALUES in (2021);
CREATE TABLE messages_2021 PARTITION OF messages DEFAULT;
