CREATE
    SCHEMA IF NOT EXISTS public;

-- groups
CREATE TABLE IF NOT EXISTS public.domains
(
    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.domains_history
(
    LIKE public.domains
);

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

ALTER TABLE public.short_urls
    ADD COLUMN domainId INT NULL REFERENCES public.domains (Id);

ALTER TABLE public.short_urls_history
    ADD COLUMN domainId INT NULL REFERENCES public.domains (Id);

