Skip to content

Commit

Permalink
Updated composite type validation
Browse files Browse the repository at this point in the history
- only check for composites in compound indexes
- clarify message with respect to the above change

tests
- added test for nested scalar case
  • Loading branch information
Druue committed Nov 1, 2023
1 parent 4e0c6ed commit 85972eb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub(super) fn validate(ctx: &mut Context<'_>) {
indexes::supports_clustering_setting(index, ctx);
indexes::clustering_can_be_defined_only_once(index, ctx);
indexes::opclasses_are_not_allowed_with_other_than_normal_indices(index, ctx);
indexes::composite_types_non_conjunctive_in_indexes(index, ctx);
indexes::composite_type_in_compound_index(index, ctx);

for field_attribute in index.scalar_field_attributes() {
let span = index.ast_attribute().span;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,14 +386,16 @@ pub(crate) fn opclasses_are_not_allowed_with_other_than_normal_indices(index: In
}
}

pub(crate) fn composite_types_non_conjunctive_in_indexes(index: IndexWalker<'_>, ctx: &mut Context<'_>) {
pub(crate) fn composite_type_in_compound_index(index: IndexWalker<'_>, ctx: &mut Context<'_>) {
let composite_type = index
.fields()
.find(|f| f.scalar_field_type().as_composite_type().is_some());

if index.fields().len() > 1 && composite_type.is_some() {

let message = format!(
"Prisma currently does not composite types in indexes when used in conjunction with other fields.",
"Prisma does not currently support composite types in compound indices. Please remove {:?} from the index.",
composite_type.unwrap().name()
);
ctx.push_error(DatamodelError::new_attribute_validation_error(
&message,
Expand Down
2 changes: 2 additions & 0 deletions query-engine/prisma-models/src/field/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ impl ScalarField {
};

match scalar_field_type {
// TODO: Fix message -- composite types do work?
// ? Is this message not validated anywhere ?
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.",)
}
Expand Down
51 changes: 46 additions & 5 deletions query-engine/prisma-models/tests/datamodel_converter_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ fn converting_enums() {
}
}

// region: composite
#[test]
fn converting_composite_types() {
fn converting_composite_types_compound() {
let res = psl::parse_schema(
r#"
datasource db {
Expand All @@ -55,8 +56,7 @@ fn converting_composite_types() {
@@unique([authorId, attributes])
// ^^^^^^^^^^^^^^^^^^^^^^
// Indexes only support composite types when not used in
// conjunction with other fields
// Prisma does not currently support composite types in compound indices
}
type Attribute {
Expand All @@ -74,11 +74,11 @@ fn converting_composite_types() {

assert!(res
.unwrap_err()
.contains("Prisma currently does not composite types in indexes when used in conjunction with other fields."));
.contains(r#"Prisma does not currently support composite types in compound indices. Please remove "attributes" from the index."#));
}

#[test]
fn converting_composite_types_2() {
fn converting_composite_types_nested() {
let res = psl::parse_schema(
r#"
datasource db {
Expand Down Expand Up @@ -117,6 +117,47 @@ fn converting_composite_types_2() {
assert!(res.is_ok());
}

#[test]
fn converting_composite_types_nested_scalar() {
let res = psl::parse_schema(
r#"
datasource db {
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() {
let datamodel = convert(
Expand Down

0 comments on commit 85972eb

Please sign in to comment.