Skip to content

Commit 66c1af5

Browse files
committed
Return null for queryables for unknown collections
Don't generate valid queryables for collections which don't exist in the database.
1 parent 2418006 commit 66c1af5

File tree

2 files changed

+46
-19
lines changed

2 files changed

+46
-19
lines changed

sql/002a_queryables.sql

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -133,25 +133,37 @@ CREATE TRIGGER queryables_collection_trigger AFTER INSERT OR UPDATE ON collectio
133133
FOR EACH STATEMENT EXECUTE PROCEDURE queryables_trigger_func();
134134

135135
CREATE OR REPLACE FUNCTION get_queryables(_collection_ids text[] DEFAULT NULL) RETURNS jsonb AS $$
136-
SELECT
137-
jsonb_build_object(
138-
'$schema', 'http://json-schema.org/draft-07/schema#',
139-
'$id', 'https://example.org/queryables',
140-
'type', 'object',
141-
'title', 'Stac Queryables.',
142-
'properties', jsonb_object_agg(
143-
name,
144-
definition
145-
)
146-
)
147-
FROM queryables
148-
WHERE
149-
_collection_ids IS NULL OR
150-
cardinality(_collection_ids) = 0 OR
151-
collection_ids IS NULL OR
152-
_collection_ids && collection_ids
153-
;
154-
$$ LANGUAGE SQL STABLE;
136+
BEGIN
137+
-- Build up queryables if the input contains valid collection ids or is empty
138+
IF (select array_agg(id) from collections) @> _collection_ids OR
139+
_collection_ids IS NULL OR
140+
cardinality(_collection_ids) = 0
141+
THEN
142+
RETURN (
143+
SELECT
144+
jsonb_build_object(
145+
'$schema', 'http://json-schema.org/draft-07/schema#',
146+
'$id', 'https://example.org/queryables',
147+
'type', 'object',
148+
'title', 'STAC Queryables.',
149+
'properties', jsonb_object_agg(
150+
name,
151+
definition
152+
)
153+
)
154+
FROM queryables
155+
WHERE
156+
_collection_ids IS NULL OR
157+
cardinality(_collection_ids) = 0 OR
158+
collection_ids IS NULL OR
159+
_collection_ids && collection_ids
160+
);
161+
ELSE
162+
RETURN NULL;
163+
END IF;
164+
END;
165+
166+
$$ LANGUAGE PLPGSQL STABLE;
155167

156168
CREATE OR REPLACE FUNCTION get_queryables(_collection text DEFAULT NULL) RETURNS jsonb AS $$
157169
SELECT

test/pgtap/002a_queryables.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,18 @@ SELECT results_eq(
2626
$$ SELECT stac_search_to_where('{"filter":{"eq":[{"property":"properties.eo:cloud_cover"},0]}}'); $$,
2727
'Make sure that CQL filter works the same with/without properties prefix.'
2828
);
29+
30+
DELETE FROM collections WHERE id = 'pgstac-test-collection';
31+
\copy collections (content) FROM 'test/testdata/collections.ndjson';
32+
33+
SELECT results_eq(
34+
$$ SELECT get_queryables('pgstac-test-collection') -> 'properties' ? 'id'; $$,
35+
$$ SELECT true; $$,
36+
'Make sure valid schema object is returned for a existing collection.'
37+
);
38+
39+
SELECT results_eq(
40+
$$ SELECT get_queryables('foo'); $$,
41+
$$ SELECT NULL::jsonb; $$,
42+
'Make sure null is returned for a non-existant collection.'
43+
);

0 commit comments

Comments
 (0)