CREATE TABLE document_versions (
    id            UUID         PRIMARY KEY DEFAULT gen_random_uuid(),
    document_id   UUID         NOT NULL REFERENCES documents(id) ON DELETE CASCADE,
    saved_at      TIMESTAMP    NOT NULL DEFAULT now(),
    editor_id     UUID         REFERENCES users(id) ON DELETE SET NULL,
    editor_name   VARCHAR(200) NOT NULL,
    snapshot      JSONB        NOT NULL,
    changed_fields JSONB       NOT NULL DEFAULT '[]'
);

CREATE INDEX ON document_versions (document_id, saved_at DESC);
