CREATE
    SCHEMA IF NOT EXISTS public;

-- groups
CREATE TABLE IF NOT EXISTS public.group_role_assignments
(
    Id         SERIAL PRIMARY KEY,
    GroupId    INT         NOT NULL REFERENCES public.groups (Id),
    RoleId     INT         NOT NULL REFERENCES permission.roles (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.group_role_assignments_history
(
    LIKE public.group_role_assignments
);

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

