这是indexloc提供的服务,不要输入任何密码
Skip to content

skip empty collections when building schemas via database introspection #76

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

Merged
merged 2 commits into from
Jun 13, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This changelog documents the changes between release versions.
- Support for root collection column references ([#75](https://github.com/hasura/ndc-mongodb/pull/75))
- Fix for databases with field names that begin with a dollar sign, or that contain dots ([#74](https://github.com/hasura/ndc-mongodb/pull/74))
- Implement column-to-column comparisons within the same collection ([#74](https://github.com/hasura/ndc-mongodb/pull/74))
- Fix error tracking collection with no documents by skipping such collections during CLI introspection ([#76](https://github.com/hasura/ndc-mongodb/pull/76))

## [0.0.6] - 2024-05-01
- Enables logging events from the MongoDB driver by setting the `RUST_LOG` variable ([#67](https://github.com/hasura/ndc-mongodb/pull/67))
Expand Down
73 changes: 50 additions & 23 deletions crates/cli/src/introspection/sampling.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::collections::{BTreeMap, HashSet};

use crate::log_warning;

use super::type_unification::{make_nullable_field, unify_object_types, unify_type};
use configuration::{
schema::{self, Type},
Expand Down Expand Up @@ -31,9 +33,18 @@ pub async fn sample_schema_from_db(
while let Some(collection_spec) = collections_cursor.try_next().await? {
let collection_name = collection_spec.name;
if !existing_schemas.contains(&collection_name) || config_file_changed {
let collection_schema =
sample_schema_from_collection(&collection_name, sample_size, all_schema_nullalble, state).await?;
schemas.insert(collection_name, collection_schema);
let collection_schema = sample_schema_from_collection(
&collection_name,
sample_size,
all_schema_nullalble,
state,
)
.await?;
if let Some(collection_schema) = collection_schema {
schemas.insert(collection_name, collection_schema);
} else {
log_warning!("could not find any documents to sample from collection, {collection_name} - skipping");
}
}
}
Ok(schemas)
Expand All @@ -44,7 +55,7 @@ async fn sample_schema_from_collection(
sample_size: u32,
all_schema_nullalble: bool,
state: &ConnectorState,
) -> anyhow::Result<Schema> {
) -> anyhow::Result<Option<Schema>> {
let db = state.database();
let options = None;
let mut cursor = db
Expand All @@ -60,21 +71,28 @@ async fn sample_schema_from_collection(
unify_object_types(collected_object_types, object_types)
};
}
let collection_info = WithName::named(
collection_name.to_string(),
schema::Collection {
description: None,
r#type: collection_name.to_string(),
},
);

Ok(Schema {
collections: WithName::into_map([collection_info]),
object_types: WithName::into_map(collected_object_types),
})
if collected_object_types.is_empty() {
Ok(None)
} else {
let collection_info = WithName::named(
collection_name.to_string(),
schema::Collection {
description: None,
r#type: collection_name.to_string(),
},
);
Ok(Some(Schema {
collections: WithName::into_map([collection_info]),
object_types: WithName::into_map(collected_object_types),
}))
}
}

fn make_object_type(object_type_name: &str, document: &Document, all_schema_nullalble: bool) -> Vec<ObjectType> {
fn make_object_type(
object_type_name: &str,
document: &Document,
all_schema_nullalble: bool,
) -> Vec<ObjectType> {
let (mut object_type_defs, object_fields) = {
let type_prefix = format!("{object_type_name}_");
let (object_type_defs, object_fields): (Vec<Vec<ObjectType>>, Vec<ObjectField>) = document
Expand Down Expand Up @@ -105,7 +123,8 @@ fn make_object_field(
all_schema_nullalble: bool,
) -> (Vec<ObjectType>, ObjectField) {
let object_type_name = format!("{type_prefix}{field_name}");
let (collected_otds, field_type) = make_field_type(&object_type_name, field_value, all_schema_nullalble);
let (collected_otds, field_type) =
make_field_type(&object_type_name, field_value, all_schema_nullalble);
let object_field_value = WithName::named(
field_name.to_owned(),
schema::ObjectField {
Expand All @@ -132,7 +151,11 @@ pub fn type_from_bson(
(WithName::into_map(object_types), t)
}

fn make_field_type(object_type_name: &str, field_value: &Bson, all_schema_nullalble: bool) -> (Vec<ObjectType>, Type) {
fn make_field_type(
object_type_name: &str,
field_value: &Bson,
all_schema_nullalble: bool,
) -> (Vec<ObjectType>, Type) {
fn scalar(t: BsonScalarType) -> (Vec<ObjectType>, Type) {
(vec![], Type::Scalar(t))
}
Expand All @@ -144,7 +167,8 @@ fn make_field_type(object_type_name: &str, field_value: &Bson, all_schema_nullal
let mut collected_otds = vec![];
let mut result_type = Type::Scalar(Undefined);
for elem in arr {
let (elem_collected_otds, elem_type) = make_field_type(object_type_name, elem, all_schema_nullalble);
let (elem_collected_otds, elem_type) =
make_field_type(object_type_name, elem, all_schema_nullalble);
collected_otds = if collected_otds.is_empty() {
elem_collected_otds
} else {
Expand Down Expand Up @@ -195,7 +219,8 @@ mod tests {
fn simple_doc() -> Result<(), anyhow::Error> {
let object_name = "foo";
let doc = doc! {"my_int": 1, "my_string": "two"};
let result = WithName::into_map::<BTreeMap<_, _>>(make_object_type(object_name, &doc, false));
let result =
WithName::into_map::<BTreeMap<_, _>>(make_object_type(object_name, &doc, false));

let expected = BTreeMap::from([(
object_name.to_owned(),
Expand Down Expand Up @@ -229,7 +254,8 @@ mod tests {
fn array_of_objects() -> Result<(), anyhow::Error> {
let object_name = "foo";
let doc = doc! {"my_array": [{"foo": 42, "bar": ""}, {"bar": "wut", "baz": 3.77}]};
let result = WithName::into_map::<BTreeMap<_, _>>(make_object_type(object_name, &doc, false));
let result =
WithName::into_map::<BTreeMap<_, _>>(make_object_type(object_name, &doc, false));

let expected = BTreeMap::from([
(
Expand Down Expand Up @@ -289,7 +315,8 @@ mod tests {
fn non_unifiable_array_of_objects() -> Result<(), anyhow::Error> {
let object_name = "foo";
let doc = doc! {"my_array": [{"foo": 42, "bar": ""}, {"bar": 17, "baz": 3.77}]};
let result = WithName::into_map::<BTreeMap<_, _>>(make_object_type(object_name, &doc, false));
let result =
WithName::into_map::<BTreeMap<_, _>>(make_object_type(object_name, &doc, false));

let expected = BTreeMap::from([
(
Expand Down
1 change: 1 addition & 0 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! The interpretation of the commands that the CLI can handle.

mod introspection;
mod logging;

use std::path::PathBuf;

Expand Down
7 changes: 7 additions & 0 deletions crates/cli/src/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[macro_export]
macro_rules! log_warning {
($msg:literal) => {
eprint!("warning: ");
eprintln!($msg);
};
}