-- name: GetQueryConfig :one
SELECT id, config FROM queryConfigs where queryId = $1 and addedVersion >= $2 and COALESCE(removedVersion, $2 - 1) < $2;

-- name: DeprecateQuery :exec
INSERT INTO queryDeprecations (queryId) VALUES ($1);

-- name: IsQueryDeprecated :one
SELECT EXISTS (
    SELECT 1 FROM queryDeprecations where queryId = $1 and removedAt is null LIMIT 1
);

-- name: GetQuery :one
SELECT id, type, activeVersion, latestVersion, config, requiredIds FROM fullActiveQueries WHERE id = $1;

-- name: ListQueries :many
SELECT id, type, activeVersion, latestVersion, config, requiredIds FROM fullActiveQueries;

-- name: CreateQuery :one
INSERT INTO queries (type) VALUES ($1) RETURNING id;

-- name: CreateRequiredQuery :exec
INSERT INTO requiredQueries (queryId, requiredQueryId, addedVersion) VALUES ($1, $2, $3);

-- name: CreateQueryConfig :exec
INSERT INTO queryConfigs (queryId, config, addedVersion) VALUES ($1, $2, $3);