Skip to content
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

feat(validation): referentialIntegrity and relationMode attributes cannot be used together #3340

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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