Skip to content

Commit

Permalink
fix(schema-engine): Ignore views for MongoDB Schemas (#4167)
Browse files Browse the repository at this point in the history
Co-authored-by: Joël Galeran <Jolg42@users.noreply.github.com>
Co-authored-by: Jan Piotrowski <piotrowski+github@gmail.com>
  • Loading branch information
3 people committed Aug 26, 2023
1 parent f957933 commit d7eca61
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ mod index;
mod model_renames;
mod remapping_names;
mod types;
mod views;
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use crate::introspection::test_api::*;
use mongodb::{bson::doc, options::CreateCollectionOptions};

#[test]
fn collection_with_view() {
let res = introspect(|db| async move {
db.create_collection("A", None).await?;
db.create_collection(
"myView".to_string(),
Some(
CreateCollectionOptions::builder()
.view_on("A".to_string())
.pipeline(vec![doc! {
"$lookup": {
"from": "A",
"localField": "first",
"foreignField": "_id",
"as": "someid"
},
}])
.build(),
),
)
.await?;

Ok(())
});

let expected_warning = expect![""];
res.expect_warnings(&expected_warning);

let expected_doc = expect![[r#"
model A {
id String @id @default(auto()) @map("_id") @db.ObjectId
}
"#]];
expected_doc.assert_eq(res.datamodel());
}
12 changes: 12 additions & 0 deletions schema-engine/mongodb-schema-describer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,21 @@ pub async fn describe(client: &mongodb::Client, db_name: &str) -> mongodb::error
while let Some(collection) = cursor.try_next().await? {
let collection_name = collection.name;
let options = collection.options;
let collection_type = collection.collection_type;
let has_schema = options.validator.is_some();
let is_capped = options.capped.is_some();

// We need to skip views, we do not support introspecting them yet.
if collection_type == mongodb::results::CollectionType::View {
continue;
}

// We need to skip system collections, they are only used by MongoDB internally.
// https://www.mongodb.com/docs/manual/reference/system-collections/
if collection_type == mongodb::results::CollectionType::Collection && collection_name.starts_with("system.") {
continue;
}

let collection = database.collection::<Document>(&collection_name);
let collection_id = schema.push_collection(collection_name, has_schema, is_capped);

Expand Down

0 comments on commit d7eca61

Please sign in to comment.