DO $$ 
    BEGIN
        CREATE TYPE link_type as ENUM ('article', 'blog');
    EXCEPTION
        WHEN duplicate_object THEN null;
    END 
$$;

CREATE TABLE IF NOT EXISTS articles (
        reference varchar(20) not null,
        title varchar(50) not null,
        previous varchar(20),
        next varchar(20),
        description text,
        content text not null,
        date date not null,
        id serial primary key);

CREATE TABLE IF NOT EXISTS links (
        url varchar(100) not null,
        date_added date not null,
        description text,
        title text not null,
        author varchar(50) not null,
        link_type link_type not null,
        id serial primary key);

CREATE TABLE IF NOT EXISTS users (
        id integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
        username text NOT NULL UNIQUE,
        password text NOT NULL,
        admin boolean NOT NULL);

CREATE TABLE IF NOT EXISTS sessions (
        session_token BYTEA PRIMARY KEY,
        user_id integer REFERENCES users (id) ON DELETE CASCADE NOT NULL);
