-- name: IsDocumentTextExtracted :one
SELECT EXISTS(
    SELECT 1
        FROM documentTextExtractions 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.minTextVersion
);

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

-- name: GetDocumentTextEntry :one
SELECT dc.documentId, dc.bucket, dc.key
    FROM documentTextExtractions 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.minTextVersion
    ORDER BY d.id DESC LIMIT 1;