CREATE SCHEMA IF NOT EXISTS system;

CREATE TABLE IF NOT EXISTS system.settings
(
    Id         SERIAL PRIMARY KEY,
    Key        TEXT        NOT NULL,
    Value      TEXT        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 system.settings_history
(
    LIKE system.settings
);

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