Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: query for child relations fails when relation list empty #614

Merged
merged 4 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Handle null values returned from empty child relation queries [#614](https://github.com/p2panda/aquadoggo/pull/614)

## [0.7.2]

### Changed
Expand Down
29 changes: 28 additions & 1 deletion aquadoggo/src/db/stores/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,9 @@ impl SqlStore {
document_view_id: &DocumentViewId,
) -> Result<Vec<DocumentId>, DocumentStorageError> {
// Collect all ids or view ids of children related to from the passed document view.
let children_ids: Vec<String> = query_scalar(
//
// Value is None when a relation list is empty.
let children_ids: Vec<Option<String>> = query_scalar(
"
SELECT
operation_fields_v1.value
Expand Down Expand Up @@ -432,6 +434,9 @@ impl SqlStore {
.await
.map_err(|err| DocumentStorageError::FatalStorageError(err.to_string()))?;

// Remove any None results from the vec.
let children_ids: Vec<String> = children_ids.into_iter().flatten().collect();

// If no children were found return now already with an empty vec.
if children_ids.is_empty() {
return Ok(vec![]);
Expand Down Expand Up @@ -1431,4 +1436,26 @@ mod tests {
assert_eq!(next_args, (None, None, SeqNum::default(), LogId::new(1)));
});
}

#[rstest]
fn regression_handle_null_relation_list_value(
#[from(populate_store_config)]
#[with(1, 1, vec![KeyPair::new()])]
config: PopulateStoreConfig,
) {
test_runner(|mut node: TestNode| async move {
// Populate the store and materialize all documents.
let documents = populate_and_materialize(&mut node, &config).await;
let document = documents[0].clone();

// The default test document contains an empty pinned relation list field.
let result = node
.context
.store
.get_child_document_ids(&document.view_id())
.await;

assert!(result.is_ok());
});
}
}
Loading