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

Try not to panic with a weird composite index #3039

Merged
merged 1 commit into from
Jul 6, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,10 @@ fn add_missing_types_from_index(
.iter()
.find(|f| f.database_name.as_deref() == Some(field_name) || f.name == field_name);

next_type = match cf {
Some(_) if i + 1 == path_length => None,
Some(cf) => {
let type_name = cf.r#type.as_composite_type().unwrap();
Some(type_name.to_string())
}
None => {
next_type = match (cf, cf.and_then(|cf| cf.r#type.as_composite_type())) {
(Some(_), _) if i + 1 == path_length => None,
(Some(_), Some(type_name)) => Some(type_name.to_string()),
(None, _) | (_, None) => {
let docs = String::from("Field referred in an index, but found no data to define the type.");

let (field_name, database_name) = match super::sanitize_string(field_name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1985,3 +1985,46 @@ fn composite_type_index_without_corresponding_data_should_not_crash() {

expected.assert_eq(res.datamodel());
}

#[test]
fn composite_type_index_with_non_composite_fields_in_the_middle_should_not_crash() {
let res = introspect(|db| async move {
db.create_collection("A", None).await?;
let collection = db.collection::<mongodb::bson::Document>("A");

let model = IndexModel::builder().keys(doc! { "a.b.c": 1 }).build();
collection.create_index(model, None).await?;

let docs = vec![doc! { "a": { "b": 1, "d": { "c": 1 } } }];
collection.insert_many(docs, None).await.unwrap();

Ok(())
});

let expected = expect![[r#"
type AA {
b Int
d AaD
/// Field referred in an index, but found no data to define the type.
b AaB?
}

type AaB {
/// Field referred in an index, but found no data to define the type.
c Json?
}

type AaD {
c Int
}

model A {
id String @id @default(auto()) @map("_id") @db.ObjectId
a AA

@@index([a.b.c], map: "a.b.c_1")
}
"#]];

expected.assert_eq(res.datamodel());
}