-
Notifications
You must be signed in to change notification settings - Fork 3
Fix collection schemas not having a unique constraint for the _id
column
#32
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!
@@ -6,6 +6,7 @@ This changelog documents the changes between release versions. | |||
- Fix bug in v2 to v3 conversion of query responses containing nested objects ([PR #27](https://github.com/hasura/ndc-mongodb/pull/27)) | |||
- Fixed bug where use of aggregate functions in queries would fail ([#26](https://github.com/hasura/ndc-mongodb/pull/26)) | |||
- Rename Any type to ExtendedJSON to make its representation clearer ([#30](https://github.com/hasura/ndc-mongodb/pull/30)) | |||
- The collection primary key `_id` property now has a unique constraint generated in the NDC schema for it ([#32](https://github.com/hasura/ndc-mongodb/pull/32)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
fn get_primary_key_uniqueness_constraint(object_types: &BTreeMap<String, models::ObjectType>, name: &str, collection: &schema::Collection) -> Option<(String, models::UniquenessConstraint)> { | ||
// Check to make sure our collection's object type contains the _id objectid field | ||
// If it doesn't (should never happen, all collections need an _id column), don't generate the constraint | ||
let object_type = object_types.get(&collection.r#type)?; | ||
let id_field = object_type.fields.get("_id")?; | ||
match &id_field.r#type { | ||
models::Type::Named { name } => { | ||
if *name == BsonScalarType::ObjectId.graphql_name() { Some(()) } else { None } | ||
}, | ||
models::Type::Nullable { .. } => None, | ||
models::Type::Array { .. } => None, | ||
models::Type::Predicate { .. } => None, | ||
}?; | ||
let uniqueness_constraint = models::UniquenessConstraint { | ||
unique_columns: vec!["_id".into()] | ||
}; | ||
let constraint_name = format!("{}_id", name); | ||
Some((constraint_name, uniqueness_constraint)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good stuff
The schema endpoint was not listing a unique constraint for collections that captured the
_id
column that every Mongo table must have that is the primary key of the table.It now generates a unique constraint named
<collection name>_id
for that column, so long as the table's object type indicates the presence of an_id
column of type object id. That should always be the case, but if it's missing for some (bad) reason, we just omit the unique constraint too.The test configuration in
fixtures/connector/chinook
has been updated to fix the_id
columns being nullable, which they aren't._id
must always have a value.