create database webscrapper_dev;

drop table content;
drop table queue;
drop table session;

create table session(
                        id serial primary key,
                        name varchar(255)
);

create table queue(
                      id serial primary key,
                      session_id int references session(id),
                      url varchar(255),
                      status smallint not null default 0,
                      started_date timestamp null,
                      finished_date timestamp null,
                      attempts int not null default 0,
                      last_error text null,
                      created_date timestamp default now()
);

create index idx_queue_session_status on queue(session_id, status);

create table content(
                        id serial primary key,
                        queue_id int references queue(id),
                        content text,
                        created_date timestamp default now()
);

create unique index if not exists ux_queue_session_url
    on queue(session_id, url);

alter table content
    add column content_encoding varchar(20) not null default 'gzip',
    add column content_bytes bytea null,
    add column original_length int null,
    add column compressed_length int null;