CREATE SCHEMA IF NOT EXISTS administration;

CREATE TABLE IF NOT EXISTS administration.users
(
    Id         SERIAL PRIMARY KEY,
    KeycloakId UUID        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(),

    CONSTRAINT UC_KeycloakId UNIQUE (KeycloakId)
);

CREATE TABLE IF NOT EXISTS administration.users_history
(
    LIKE administration.users
);

CREATE TRIGGER users_history_trigger
    BEFORE INSERT OR UPDATE OR DELETE
    ON administration.users
    FOR EACH ROW
EXECUTE FUNCTION public.history_trigger_function();

