Skip to content

Commit

Permalink
Merge pull request #3340 from prisma/feat/referential-integrity-and-r…
Browse files Browse the repository at this point in the history
…elation-mode-cannot-cooccur

feat(validation): referentialIntegrity and relationMode attributes cannot be used together
  • Loading branch information
jkomyno committed Oct 27, 2022
2 parents f90920b + a86632b commit 190c3eb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
5 changes: 5 additions & 0 deletions psl/diagnostics/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@ impl DatamodelError {
Self::new(msg, span)
}

pub fn new_referential_integrity_and_relation_mode_cooccur_error(span: Span) -> DatamodelError {
let msg = "The `referentialIntegrity` and `relationMode` attributes cannot be used together. Please use only `relationMode` instead.".to_string();
Self::new(msg, span)
}

pub fn span(&self) -> Span {
self.span
}
Expand Down
10 changes: 10 additions & 0 deletions psl/psl-core/src/validate/datasource_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,16 @@ fn lift_datasource(

let connector_data = active_connector.parse_datasource_properties(&mut args, diagnostics);

// referentialIntegrity and relationMode cannot co-occur
if referential_integrity.and(relation_mode).is_some() {
let span = args
.get("referentialIntegrity")
.map(|(_, v)| v.span())
.unwrap_or_else(Span::empty);

diagnostics.push_error(DatamodelError::new_referential_integrity_and_relation_mode_cooccur_error(span));
}

// we handle these elsewhere
args.remove("previewFeatures");
args.remove("referentialIntegrity");
Expand Down
19 changes: 14 additions & 5 deletions psl/psl/tests/config/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,11 +721,11 @@ fn relation_mode_default() {
}

#[test]
fn relation_mode_prisma_has_precedence_over_referential_integrity() {
fn relation_mode_and_referential_integrity_cannot_cooccur() {
let schema = indoc! {r#"
datasource ps {
provider = "sqlserver"
url = "mysql://root:prisma@localhost:3306/mydb"
provider = "sqlite"
url = "sqlite"
relationMode = "prisma"
referentialIntegrity = "foreignKeys"
}
Expand All @@ -735,9 +735,18 @@ fn relation_mode_prisma_has_precedence_over_referential_integrity() {
}
"#};

let config = parse_configuration(schema);
let config = parse_config(schema);
let error = config.unwrap_err();

assert_eq!(config.relation_mode(), Some(RelationMode::Prisma));
let expectation = expect![[r#"
error: The `referentialIntegrity` and `relationMode` attributes cannot be used together. Please use only `relationMode` instead.
--> schema.prisma:5
 | 
 4 |  relationMode = "prisma"
 5 |  referentialIntegrity = "foreignKeys"
 | 
"#]];
expectation.assert_eq(&error);
}

fn load_env_var(key: &str) -> Option<String> {
Expand Down

0 comments on commit 190c3eb

Please sign in to comment.