-
Notifications
You must be signed in to change notification settings - Fork 249
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
fix(psl): composite type validation in indices #4401
Merged
+165
−32
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a26a793
fix regression
Druue 204b753
Updated composite type validation
Druue 2da5fe6
clippy
Druue e64b2db
formatting
Druue 0852084
Add prisly for tracking issue
Druue 2f65970
update to be specific to unique indices
Druue b3b058f
Update unreachable message
Druue 5794fe3
accidentally a word
Druue a58ad02
Update test to use correct message
Druue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,7 +91,7 @@ impl ScalarField { | |
|
||
match scalar_field_type { | ||
ScalarFieldType::CompositeType(_) => { | ||
unreachable!("Cannot convert a composite type to a type identifier. This error is typically caused by mistakenly using a composite type within a composite index.",) | ||
unreachable!("This shouldn't be reached; composite types are not supported in compound unique indices.",) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Less cryptic message 👍🏾 |
||
} | ||
ScalarFieldType::Enum(x) => TypeIdentifier::Enum(x), | ||
ScalarFieldType::BuiltInScalar(scalar) => scalar.into(), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,31 +38,159 @@ fn converting_enums() { | |
} | ||
} | ||
|
||
// region: composite | ||
#[test] | ||
fn converting_composite_types() { | ||
fn converting_composite_types_compound() { | ||
let res = psl::parse_schema( | ||
r#" | ||
datasource db { | ||
provider = "mongodb" | ||
url = "mongodb://localhost:27017/hello" | ||
} | ||
datasource db { | ||
provider = "mongodb" | ||
url = "mongodb://localhost:27017/hello" | ||
} | ||
|
||
model MyModel { | ||
id String @id @default(auto()) @map("_id") @db.ObjectId | ||
attribute Attribute | ||
model Post { | ||
id String @id @default(auto()) @map("_id") @db.ObjectId | ||
author User @relation(fields: [authorId], references: [id]) | ||
authorId String @db.ObjectId | ||
attributes Attribute[] | ||
|
||
@@index([authorId, attributes]) | ||
} | ||
|
||
type Attribute { | ||
name String | ||
value String | ||
group String | ||
} | ||
|
||
model User { | ||
id String @id @default(auto()) @map("_id") @db.ObjectId | ||
Post Post[] | ||
} | ||
"#, | ||
); | ||
|
||
@@unique([attribute], name: "composite_index") | ||
} | ||
assert!(res.is_ok()); | ||
} | ||
|
||
type Attribute { | ||
name String | ||
value String | ||
group String | ||
} | ||
#[test] | ||
fn converting_composite_types_compound_unique() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 to the exhaustive tests |
||
let res = psl::parse_schema( | ||
r#" | ||
datasource db { | ||
provider = "mongodb" | ||
url = "mongodb://localhost:27017/hello" | ||
} | ||
|
||
model Post { | ||
id String @id @default(auto()) @map("_id") @db.ObjectId | ||
author User @relation(fields: [authorId], references: [id]) | ||
authorId String @db.ObjectId | ||
attributes Attribute[] | ||
|
||
@@unique([authorId, attributes]) | ||
// ^^^^^^^^^^^^^^^^^^^^^^ | ||
// Prisma does not currently support composite types in compound unique indices... | ||
} | ||
|
||
type Attribute { | ||
name String | ||
value String | ||
group String | ||
} | ||
|
||
model User { | ||
id String @id @default(auto()) @map("_id") @db.ObjectId | ||
Post Post[] | ||
} | ||
"#, | ||
); | ||
assert!(res.unwrap_err().contains("Indexes can only contain scalar attributes. Please remove \"attribute\" from the argument list of the indexes.")); | ||
|
||
assert!(res | ||
.unwrap_err() | ||
.contains(r#"Prisma does not currently support composite types in compound unique indices, please remove "attributes" from the index. See https://pris.ly/d/mongodb-composite-compound-indices for more details"#)); | ||
} | ||
|
||
#[test] | ||
fn converting_composite_types_nested() { | ||
let res = psl::parse_schema( | ||
r#" | ||
datasource db { | ||
Druue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
provider = "mongodb" | ||
url = "mongodb://localhost:27017/hello" | ||
} | ||
|
||
type TheatersLocation { | ||
address TheatersLocationAddress | ||
geo TheatersLocationGeo | ||
} | ||
|
||
type TheatersLocationAddress { | ||
city String | ||
state String | ||
street1 String | ||
street2 String? | ||
zipcode String | ||
} | ||
|
||
type TheatersLocationGeo { | ||
coordinates Float[] | ||
type String | ||
} | ||
|
||
model theaters { | ||
id String @id @default(auto()) @map("_id") @db.ObjectId | ||
location TheatersLocation | ||
theaterId Int | ||
|
||
@@index([location.geo], map: "geo index") | ||
} | ||
"#, | ||
); | ||
|
||
assert!(res.is_ok()); | ||
} | ||
|
||
#[test] | ||
fn converting_composite_types_nested_scalar() { | ||
let res = psl::parse_schema( | ||
r#" | ||
datasource db { | ||
Druue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
provider = "mongodb" | ||
url = "mongodb://localhost:27017/hello" | ||
} | ||
|
||
type TheatersLocation { | ||
address TheatersLocationAddress | ||
geo TheatersLocationGeo | ||
} | ||
|
||
type TheatersLocationAddress { | ||
city String | ||
state String | ||
street1 String | ||
street2 String? | ||
zipcode String | ||
} | ||
|
||
type TheatersLocationGeo { | ||
coordinates Float[] | ||
type String | ||
} | ||
|
||
model theaters { | ||
id String @id @default(auto()) @map("_id") @db.ObjectId | ||
location TheatersLocation | ||
theaterId Int | ||
|
||
@@index([location.geo.type], map: "geo index") | ||
} | ||
"#, | ||
); | ||
|
||
assert!(res.is_ok()); | ||
} | ||
// endregion | ||
|
||
#[test] | ||
fn models_with_only_scalar_fields() { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What about normal indexes? Are those supported with a mix of scalar and composite fields?
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.
They don't actually hit this error (fulltext also doesn't). Not to say they're supported but they run into different issues that are unrelated to the original crash report. Only
@@unique
causes a panic from what I've found