2024-12-23 12:26:05 +00:00
|
|
|
-- name: GetQueryConfig :one
|
2025-01-03 10:35:42 +00:00
|
|
|
SELECT id, config FROM queryConfigs where queryId = $1 and addedVersion >= $2 and COALESCE(removedVersion, $2 - 1) < $2;
|
|
|
|
|
|
|
|
|
|
-- name: GetQuery :one
|
2025-01-21 12:28:46 +00:00
|
|
|
SELECT * FROM fullActiveQueries WHERE id = $1;
|
2025-01-03 14:08:04 +00:00
|
|
|
|
2025-01-29 11:52:37 +00:00
|
|
|
-- name: GetQueryWithVersion :one
|
2025-02-14 10:56:24 +00:00
|
|
|
WITH query as (
|
2025-02-14 18:43:26 +00:00
|
|
|
SELECT id, type FROM queries WHERE id = @id
|
2025-02-14 10:56:24 +00:00
|
|
|
),
|
|
|
|
|
config as (
|
|
|
|
|
SELECT c.queryId, c.config
|
|
|
|
|
FROM query AS q
|
2025-01-29 11:52:37 +00:00
|
|
|
LEFT JOIN queryConfigs AS c ON q.id = c.queryId
|
2025-02-14 18:43:26 +00:00
|
|
|
and isInVersion(@version, c.addedVersion, c.removedVersion)
|
2025-02-14 10:56:24 +00:00
|
|
|
),
|
|
|
|
|
requiredIds as (
|
|
|
|
|
SELECT r.queryId,
|
|
|
|
|
ARRAY_AGG(DISTINCT r.requiredQueryId)
|
|
|
|
|
FILTER (WHERE r.requiredQueryId != '00000000-0000-0000-0000-000000000000')::uuid[]
|
|
|
|
|
as requiredIds
|
|
|
|
|
FROM query AS q
|
2025-01-29 11:52:37 +00:00
|
|
|
LEFT JOIN requiredQueries AS r ON q.id = r.queryId
|
2025-02-14 18:43:26 +00:00
|
|
|
and isInVersion(@version, r.addedVersion, r.removedVersion)
|
2025-02-14 10:56:24 +00:00
|
|
|
GROUP BY r.queryId
|
|
|
|
|
)
|
2025-02-14 18:43:26 +00:00
|
|
|
SELECT DISTINCT q.id, q.type, coalesce(av.id, 0) as activeVersion, coalesce(lv.id, 0) as latestVersion, c.config,
|
2025-02-14 10:56:24 +00:00
|
|
|
coalesce(
|
|
|
|
|
r.requiredIds,
|
|
|
|
|
array[]::uuid[]
|
|
|
|
|
)::uuid[] as requiredIds
|
|
|
|
|
FROM query AS q
|
2025-02-14 18:43:26 +00:00
|
|
|
LEFT JOIN queryCurrentActiveVersions as av on q.id = av.queryId
|
|
|
|
|
LEFT JOIN queryLatestVersions as lv on lv.queryId = q.id
|
2025-02-14 10:56:24 +00:00
|
|
|
LEFT JOIN config AS c ON q.id = c.queryId
|
|
|
|
|
LEFT JOIN requiredIds AS r ON q.id = r.queryId;
|
2025-01-29 11:52:37 +00:00
|
|
|
|
2025-01-03 14:08:04 +00:00
|
|
|
-- name: ListQueries :many
|
2025-01-21 12:28:46 +00:00
|
|
|
SELECT * FROM fullActiveQueries;
|
2025-01-06 12:26:28 +00:00
|
|
|
|
2025-01-29 11:52:37 +00:00
|
|
|
-- name: ListQueriesById :many
|
|
|
|
|
SELECT * FROM fullActiveQueries WHERE id = any($1);
|
|
|
|
|
|
2025-01-06 12:26:28 +00:00
|
|
|
-- name: CreateQuery :one
|
|
|
|
|
INSERT INTO queries (type) VALUES ($1) RETURNING id;
|
|
|
|
|
|
2025-02-17 14:30:37 +00:00
|
|
|
-- name: AddLatestQueryVersion :one
|
|
|
|
|
INSERT INTO queryVersions (queryId) VALUES ($1) RETURNING id;
|
2025-02-14 18:43:26 +00:00
|
|
|
|
|
|
|
|
-- name: AddActiveQueryVersion :exec
|
|
|
|
|
INSERT INTO queryActiveVersions (queryId, versionId) VALUES ($1, $2);
|
2025-01-20 13:31:48 +00:00
|
|
|
|
|
|
|
|
-- name: AddRequiredQuery :exec
|
2025-01-06 12:26:28 +00:00
|
|
|
INSERT INTO requiredQueries (queryId, requiredQueryId, addedVersion) VALUES ($1, $2, $3);
|
|
|
|
|
|
2025-01-20 13:31:48 +00:00
|
|
|
-- name: RemoveRequiredQuery :exec
|
|
|
|
|
UPDATE requiredQueries SET removedVersion = $1 WHERE requiredQueryId = $2 and queryId = $3 and removedVersion is null;
|
|
|
|
|
|
2025-02-17 14:30:37 +00:00
|
|
|
-- name: SetQueryConfig :exec
|
2025-01-20 13:31:48 +00:00
|
|
|
INSERT INTO queryConfigs (queryId, config, addedVersion) VALUES ($1, $2, $3);
|
|
|
|
|
|
|
|
|
|
-- name: AllQueriesExist :one
|
|
|
|
|
SELECT COUNT(*) = COUNT(DISTINCT id) AS all_exist
|
|
|
|
|
FROM unnest($1::uuid[]) AS input_id
|
2025-01-23 14:56:20 +00:00
|
|
|
LEFT JOIN queries ON input_id = queries.id;
|
|
|
|
|
|
|
|
|
|
-- name: IsQueryInDependencyTree :one
|
|
|
|
|
SELECT EXISTS (
|
|
|
|
|
SELECT 1 FROM queryActiveDependencies WHERE id = any($1) and requiredQueryId = $2 or $2 = any($1)
|
2025-02-11 15:22:59 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
-- name: ListQueryDirectDependentsByDocumentID :many
|
|
|
|
|
WITH doc AS (
|
|
|
|
|
SELECT id, jobId FROM documents where id = $2
|
|
|
|
|
)
|
|
|
|
|
SELECT dt.queryId
|
|
|
|
|
FROM collectorQueryDependencyTree as dt
|
|
|
|
|
JOIN doc as d on d.jobId = dt.jobId
|
2025-02-14 10:56:24 +00:00
|
|
|
where $1 = any(dt.requiredIds);
|
|
|
|
|
|
|
|
|
|
-- name: ListQueryJobIDs :many
|
|
|
|
|
SELECT jobId FROM collectorQueryDependencyTree WHERE queryId = $1;
|