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

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

Merged
merged 2 commits into from
Apr 9, 2024

Conversation

daniel-chambers
Copy link
Contributor

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.

@daniel-chambers daniel-chambers self-assigned this Apr 8, 2024
Copy link

@sordina sordina left a 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))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines +90 to +108
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))
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Collaborator

@hallettj hallettj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good stuff

@daniel-chambers daniel-chambers merged commit 1b96bb8 into main Apr 9, 2024
@daniel-chambers daniel-chambers deleted the daniel/pk-constraints branch April 9, 2024 00:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants