-- name: IsDocumentClean :one
SELECT EXISTS(
    SELECT 1
        FROM documentCleans AS dc
        JOIN documents AS d ON d.id = dc.documentId
        JOIN collectors as c ON d.jobId = c.jobId
        JOIN collectorCodeVersions as cv ON c.id = cv.collectorId
        WHERE dc.documentId = $1 and dc.version >= cv.minCleanVersion
);

-- name: AddDocumentCleanEntry :exec
INSERT INTO documentCleans (documentId, version, bucket, key) VALUES ($1, $2, $3, $4);

-- name: GetDocumentCleanEntry :one
SELECT dc.documentId, dc.bucket, dc.key
    FROM documentCleans AS dc
    JOIN documents AS d ON d.id = dc.documentId
    JOIN collectors as c ON d.jobId = c.jobId
    JOIN collectorCodeVersions as cv ON c.id = cv.collectorId
    WHERE dc.documentId = $1 and dc.version >= cv.minCleanVersion
    ORDER BY d.id DESC LIMIT 1;