3d434eedb8
Job Status Get and DB tidy up * initalquery * tests * shorttests * testing queries * job * solvedthequery * updatingdb * fixingtests * repotests * shorttests * docker * testspassed
91 lines
2.5 KiB
SQL
91 lines
2.5 KiB
SQL
-- name: GetJob :one
|
|
SELECT j.id, j.clientId, cs.canSync
|
|
FROM jobs as j
|
|
JOIN currentJobCanSync as cs on cs.jobId = j.id
|
|
WHERE j.id = $1;
|
|
|
|
-- name: CreateJob :one
|
|
INSERT INTO jobs (clientId) VALUES ($1) RETURNING id;
|
|
|
|
-- name: AddJobCanSync :exec
|
|
INSERT INTO jobCanSync (canSync, jobId) VALUES ($1, $2);
|
|
|
|
-- name: ListJobDocumentIDsBatch :many
|
|
SELECT id, totalCount FROM listJobDocumentIDs(@jobId, @batchSize, @pageOffset);
|
|
|
|
-- name: IsJobSynced :one
|
|
WITH
|
|
docs AS (
|
|
-- Get all documents for this job
|
|
SELECT id, jobId FROM documents WHERE jobId = $1
|
|
),
|
|
doc_text_entries AS (
|
|
-- Documents with their current text entries
|
|
SELECT
|
|
d.id AS document_id,
|
|
cte.id AS text_entry_id
|
|
FROM
|
|
docs d
|
|
LEFT JOIN currentTextEntries cte ON cte.documentId = d.id
|
|
),
|
|
required_results AS (
|
|
-- All required document-query-version combinations
|
|
SELECT
|
|
d.id AS document_id,
|
|
cqdt.queryId,
|
|
cqdt.queryVersion,
|
|
dte.text_entry_id
|
|
FROM
|
|
docs d
|
|
JOIN collectorQueryDependencyTree cqdt ON cqdt.jobId = d.jobId
|
|
JOIN doc_text_entries dte ON dte.document_id = d.id
|
|
and dte.text_entry_id IS NOT NULL
|
|
),
|
|
existing_results AS (
|
|
-- Valid results that exist
|
|
SELECT
|
|
rr.document_id AS document_id,
|
|
r.queryId,
|
|
r.id AS result_id
|
|
FROM
|
|
results r
|
|
JOIN required_results rr ON
|
|
r.queryId = rr.queryId AND
|
|
r.queryVersion = rr.queryVersion AND
|
|
r.textEntryId = rr.text_entry_id
|
|
),
|
|
missing_results AS (
|
|
-- Find missing results
|
|
SELECT rr.queryId
|
|
FROM required_results rr
|
|
LEFT JOIN existing_results er on er.queryId = rr.queryId
|
|
WHERE er.result_id is null
|
|
),
|
|
dependency_check AS (
|
|
-- Check for missing dependencies
|
|
SELECT DISTINCT er.queryId, er.result_id, qcri.queryId, rd.resultId
|
|
FROM existing_results er
|
|
JOIN queryCurrentRequiredIds qcri on qcri.requiredQueryId = er.queryId
|
|
LEFT JOIN resultDependencies rd on rd.requiredResultId = er.result_id
|
|
WHERE rd.resultId is null
|
|
)
|
|
SELECT (
|
|
-- No documents means job is synced
|
|
NOT EXISTS (SELECT 1 FROM docs)
|
|
|
|
OR
|
|
|
|
-- Documents with no text entries
|
|
(NOT EXISTS (SELECT 1 FROM doc_text_entries WHERE text_entry_id IS NULL)
|
|
|
|
and
|
|
|
|
-- Documents with missing results
|
|
NOT EXISTS (SELECT 1 FROM missing_results)
|
|
|
|
and
|
|
|
|
-- Documents with missing dependencies
|
|
NOT EXISTS (SELECT 1 FROM dependency_check))
|
|
)::bool as is_synced;
|