CREATE SCHEMA IF NOT EXISTS public;

CREATE TABLE IF NOT EXISTS public.user_settings
(
    Id         SERIAL PRIMARY KEY,
    Key        TEXT        NOT NULL,
    Value      TEXT        NOT NULL,
    UserId     INT         NOT NULL REFERENCES administration.users (Id) ON DELETE CASCADE,
    -- 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 public.user_settings_history
(
    LIKE public.user_settings
);

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