CREATE
    SCHEMA IF NOT EXISTS public;

-- groups
CREATE TABLE IF NOT EXISTS public.groups
(
    Id         SERIAL PRIMARY KEY,
    Name       VARCHAR(255) NOT NULL,
    -- for history
    Deleted    BOOLEAN      NOT NULL DEFAULT FALSE,
    EditorId   INT          NULL REFERENCES administration.users (Id),
    CreatedUtc timestamptz  NOT NULL DEFAULT NOW(),
    UpdatedUtc timestamptz  NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS public.groups_history
(
    LIKE public.groups
);

CREATE TRIGGER groups_history_trigger
    BEFORE INSERT OR UPDATE OR DELETE
    ON public.groups
    FOR EACH ROW
EXECUTE FUNCTION public.history_trigger_function();


-- short urls
CREATE TABLE IF NOT EXISTS public.short_urls
(
    Id          SERIAL PRIMARY KEY,
    ShortUrl   VARCHAR(255) NOT NULL,
    TargetUrl  VARCHAR(255) NOT NULL,
    Description TEXT         NULL,
    GroupId    INT          NULL REFERENCES public.groups (Id),
    -- for history
    Deleted     BOOLEAN      NOT NULL DEFAULT FALSE,
    EditorId    INT          NULL REFERENCES administration.users (Id),
    CreatedUtc  timestamptz  NOT NULL DEFAULT NOW(),
    UpdatedUtc  timestamptz  NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS public.short_urls_history
(
    LIKE public.short_urls
);

CREATE TRIGGER short_urls_history_trigger
    BEFORE INSERT OR UPDATE OR DELETE
    ON public.short_urls
    FOR EACH ROW
EXECUTE FUNCTION public.history_trigger_function();

