CREATE UNIQUE INDEX IF NOT EXISTS user_invitations_unique_email
    ON public.user_invitations (email)
    WHERE email != 'ANONYMOUS';

ALTER TABLE public.user_invitations
    ADD CONSTRAINT user_invitations_unique_invitation UNIQUE (invitation);

ALTER TABLE public.user_spaces_users
    ADD CONSTRAINT user_spaces_users_unique_invitation UNIQUE (invitation),
    ADD CONSTRAINT user_spaces_users_unique_entry UNIQUE (userspaceid, userid);