diff --git a/CHANGELOG.md b/CHANGELOG.md index df383d3a5..83677089d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Handle connection ids greater than 9 in `Peer` impl of `Human` trait [#634](https://github.com/p2panda/aquadoggo/pull/634) - Check if blob file exists before deleting it from fs [#636](https://github.com/p2panda/aquadoggo/pull/636) - Inconsistent blob storage warning was wrongly shown [#638](https://github.com/p2panda/aquadoggo/pull/638) +- Safely handle missing document when retrieving document view from store [#637](https://github.com/p2panda/aquadoggo/pull/637) ## [0.7.4] diff --git a/aquadoggo/src/db/stores/document.rs b/aquadoggo/src/db/stores/document.rs index 661007b39..f11258a11 100644 --- a/aquadoggo/src/db/stores/document.rs +++ b/aquadoggo/src/db/stores/document.rs @@ -170,8 +170,12 @@ impl DocumentStore for SqlStore { .await .map_err(|e| DocumentStorageError::FatalStorageError(e.to_string()))?; - // Unwrap as we can assume a document for the found document id exists. - let document_row = document_row.unwrap(); + // If no row matched we return None here, otherwise unwrap safely + let document_row = match document_row { + Some(document_row) => document_row, + // Document might have already been deleted + None => return Ok(None), + }; // We now want to retrieve the view (current key-value map) for this document, as we // already filtered out deleted documents in the query above we can expect all documents @@ -220,7 +224,7 @@ impl DocumentStore for SqlStore { ON operations_v1.operation_id = documents.document_id WHERE - documents.schema_id = $1 AND documents.is_deleted = false + documents.schema_id = $1 AND documents.is_deleted = false ", ) .bind(schema_id.to_string()) @@ -420,9 +424,9 @@ impl SqlStore { document_view_fields.name = operation_fields_v1.name WHERE operation_fields_v1.field_type IN ( - 'pinned_relation', - 'pinned_relation_list', - 'relation', + 'pinned_relation', + 'pinned_relation_list', + 'relation', 'relation_list' ) AND @@ -539,11 +543,11 @@ impl SqlStore { ) -> Result { let document_view_id: Option = query_scalar( " - SELECT - documents.document_view_id - FROM + SELECT + documents.document_view_id + FROM documents - WHERE + WHERE documents.document_view_id = $1 ", )