Skip to content

Commit caa850b

Browse files
authored
fix: return '[]' instead of NULL from all_collections when the table is empty (#187)
1 parent 23f72c8 commit caa850b

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## Unreleased
8+
### Fixed
9+
- Return an empty jsonb array from all_collections() when the collections table is empty, instead of NULL. Fixes #186.
10+
711
## [v0.7.9]
812

913
### Fixed

src/pgstac/sql/002_collections.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,5 @@ $$ LANGUAGE SQL SET SEARCH_PATH TO pgstac,public;
6363

6464

6565
CREATE OR REPLACE FUNCTION all_collections() RETURNS jsonb AS $$
66-
SELECT jsonb_agg(content) FROM collections;
66+
SELECT coalesce(jsonb_agg(content), '[]'::jsonb) FROM collections;
6767
$$ LANGUAGE SQL SET SEARCH_PATH TO pgstac,public;

src/pgstac/tests/pgtap/002a_queryables.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ SELECT results_eq(
2121
);
2222

2323
DELETE FROM collections WHERE id in ('pgstac-test-collection', 'pgstac-test-collection2');
24+
25+
SELECT results_eq(
26+
$$ SELECT all_collections(); $$,
27+
$$ SELECT '[]'::jsonb; $$,
28+
'Make sure all_collections returns an empty array when the collection table is empty.'
29+
);
30+
2431
\copy collections (content) FROM 'tests/testdata/collections.ndjson';
2532

2633
SELECT results_eq(

0 commit comments

Comments
 (0)