-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(psl): add warning when
SetDefault
is used on mysql
with `rel…
…ationMode = "foreignKeys"` (#3435) * feat(psl): support connector-specific warnings in DatamodelWarning * feat(psl): add warning when SetDefault is used in MySQL with foreignKeys mode active * test(psl): add tests for SetDefault warning on MySQL * test: add introspection and migration tests for SetDefault warning on MySQL * chore: removed deprecated previewFeature referentialIntegrity * chore: fix snapshots in unit tests * test: exclude Vitess from "introspect_set_default_should_warn" test * chore: simplify conditional for SetDefault
- Loading branch information
Showing
16 changed files
with
251 additions
and
15 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
introspection-engine/introspection-engine-tests/tests/referential_actions/mod.rs
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod cockroachdb; | ||
mod mysql; | ||
mod sqlite; | ||
|
||
use barrel::types; | ||
|
77 changes: 77 additions & 0 deletions
77
introspection-engine/introspection-engine-tests/tests/referential_actions/mysql.rs
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use introspection_engine_tests::test_api::*; | ||
|
||
// Older versions of MySQL (5.6+) raise a syntax error on `CREATE TABLE` declarations with `SET DEFAULT` referential actions, | ||
// so we can skip introspecting those. MariaDb 10.0 suffers from the same issue. | ||
// We should see validation warnings on MySQL 8+. | ||
#[test_connector(tags(Mysql8), exclude(Vitess))] | ||
async fn introspect_set_default_should_warn(api: &TestApi) -> TestResult { | ||
let setup = r#" | ||
CREATE TABLE `SomeUser` ( | ||
`id` INTEGER NOT NULL AUTO_INCREMENT, | ||
PRIMARY KEY (`id`) | ||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; | ||
CREATE TABLE `Post` ( | ||
`id` INTEGER NOT NULL AUTO_INCREMENT, | ||
`userId` INTEGER NULL DEFAULT 3, | ||
PRIMARY KEY (`id`) | ||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; | ||
ALTER TABLE `Post` ADD CONSTRAINT `Post_userId_fkey` | ||
FOREIGN KEY (`userId`) REFERENCES `SomeUser`(`id`) | ||
ON DELETE SET DEFAULT ON UPDATE SET DEFAULT; | ||
"#; | ||
|
||
api.raw_cmd(setup).await; | ||
|
||
let expected_schema = expect![[r#" | ||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
datasource db { | ||
provider = "mysql" | ||
url = "env(TEST_DATABASE_URL)" | ||
} | ||
model Post { | ||
id Int @id @default(autoincrement()) | ||
userId Int? @default(3) | ||
SomeUser SomeUser? @relation(fields: [userId], references: [id], onDelete: SetDefault, onUpdate: SetDefault) | ||
@@index([userId], map: "Post_userId_fkey") | ||
} | ||
model SomeUser { | ||
id Int @id @default(autoincrement()) | ||
Post Post[] | ||
} | ||
"#]]; | ||
|
||
expected_schema.assert_eq(&api.introspect().await?); | ||
let schema = psl::parse_schema(expected_schema.data())?; | ||
|
||
let warning_messages = schema | ||
.diagnostics | ||
.warnings_to_pretty_string("schema.prisma", &schema.db.source()); | ||
|
||
let expected_validation = expect![[r#" | ||
[1;93mwarning[0m: [1mUsing SetDefault on MySQL may yield to unexpected results, as the database will silently change the referential action to `NoAction`.[0m | ||
[1;94m-->[0m [4mschema.prisma:14[0m | ||
[1;94m | [0m | ||
[1;94m13 | [0m userId Int? @default(3) | ||
[1;94m14 | [0m SomeUser SomeUser? @relation(fields: [userId], references: [id], [1;93monDelete: SetDefault[0m, onUpdate: SetDefault) | ||
[1;94m | [0m | ||
[1;93mwarning[0m: [1mUsing SetDefault on MySQL may yield to unexpected results, as the database will silently change the referential action to `NoAction`.[0m | ||
[1;94m-->[0m [4mschema.prisma:14[0m | ||
[1;94m | [0m | ||
[1;94m13 | [0m userId Int? @default(3) | ||
[1;94m14 | [0m SomeUser SomeUser? @relation(fields: [userId], references: [id], onDelete: SetDefault, [1;93monUpdate: SetDefault[0m) | ||
[1;94m | [0m | ||
"#]]; | ||
expected_validation.assert_eq(&warning_messages); | ||
|
||
Ok(()) | ||
} |
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
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
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
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
32 changes: 32 additions & 0 deletions
32
psl/psl/tests/validation/mysql/set_default_no_warning_when_relation_mode_prisma.prisma
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
datasource db { | ||
provider = "mysql" | ||
url = "mysql://" | ||
relationMode = "prisma" | ||
} | ||
|
||
model A { | ||
id Int @id | ||
bs B[] | ||
} | ||
|
||
model B { | ||
id Int @id | ||
aId Int? @default(3) | ||
a A? @relation(fields: [aId], references: [id], onUpdate: SetDefault, onDelete: SetDefault) | ||
} | ||
// [1;91merror[0m: [1mError validating: Invalid referential action: `SetDefault`. Allowed values: (`Cascade`, `Restrict`, `NoAction`, `SetNull`)[0m | ||
// [1;94m-->[0m [4mschema.prisma:19[0m | ||
// [1;94m | [0m | ||
// [1;94m18 | [0m aId Int? @default(3) | ||
// [1;94m19 | [0m a A? @relation(fields: [aId], references: [id], onUpdate: SetDefault, [1;91monDelete: SetDefault[0m) | ||
// [1;94m | [0m | ||
// [1;91merror[0m: [1mError validating: Invalid referential action: `SetDefault`. Allowed values: (`Cascade`, `Restrict`, `NoAction`, `SetNull`)[0m | ||
// [1;94m-->[0m [4mschema.prisma:19[0m | ||
// [1;94m | [0m | ||
// [1;94m18 | [0m aId Int? @default(3) | ||
// [1;94m19 | [0m a A? @relation(fields: [aId], references: [id], [1;91monUpdate: SetDefault[0m, onDelete: SetDefault) | ||
// [1;94m | [0m |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
datasource db { | ||
provider = "mysql" | ||
url = "mysql://" | ||
} | ||
|
||
model A { | ||
id Int @id | ||
bs B[] | ||
} | ||
|
||
model B { | ||
id Int @id | ||
aId Int? @default(3) | ||
a A? @relation(fields: [aId], references: [id], onUpdate: SetDefault, onDelete: SetDefault) | ||
} | ||
// [1;93mwarning[0m: [1mUsing SetDefault on MySQL may yield to unexpected results, as the database will silently change the referential action to `NoAction`.[0m | ||
// [1;94m-->[0m [4mschema.prisma:14[0m | ||
// [1;94m | [0m | ||
// [1;94m13 | [0m aId Int? @default(3) | ||
// [1;94m14 | [0m a A? @relation(fields: [aId], references: [id], onUpdate: SetDefault, [1;93monDelete: SetDefault[0m) | ||
// [1;94m | [0m | ||
// [1;93mwarning[0m: [1mUsing SetDefault on MySQL may yield to unexpected results, as the database will silently change the referential action to `NoAction`.[0m | ||
// [1;94m-->[0m [4mschema.prisma:14[0m | ||
// [1;94m | [0m | ||
// [1;94m13 | [0m aId Int? @default(3) | ||
// [1;94m14 | [0m a A? @relation(fields: [aId], references: [id], [1;93monUpdate: SetDefault[0m, onDelete: SetDefault) | ||
// [1;94m | [0m |
32 changes: 32 additions & 0 deletions
32
psl/psl/tests/validation/mysql/set_default_warning_when_relation_mode_foreignkeys.prisma
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
datasource db { | ||
provider = "mysql" | ||
url = "mysql://" | ||
relationMode = "foreignKeys" | ||
} | ||
|
||
model A { | ||
id Int @id | ||
bs B[] | ||
} | ||
|
||
model B { | ||
id Int @id | ||
aId Int? @default(3) | ||
a A? @relation(fields: [aId], references: [id], onUpdate: SetDefault, onDelete: SetDefault) | ||
} | ||
// [1;93mwarning[0m: [1mUsing SetDefault on MySQL may yield to unexpected results, as the database will silently change the referential action to `NoAction`.[0m | ||
// [1;94m-->[0m [4mschema.prisma:19[0m | ||
// [1;94m | [0m | ||
// [1;94m18 | [0m aId Int? @default(3) | ||
// [1;94m19 | [0m a A? @relation(fields: [aId], references: [id], onUpdate: SetDefault, [1;93monDelete: SetDefault[0m) | ||
// [1;94m | [0m | ||
// [1;93mwarning[0m: [1mUsing SetDefault on MySQL may yield to unexpected results, as the database will silently change the referential action to `NoAction`.[0m | ||
// [1;94m-->[0m [4mschema.prisma:19[0m | ||
// [1;94m | [0m | ||
// [1;94m18 | [0m aId Int? @default(3) | ||
// [1;94m19 | [0m a A? @relation(fields: [aId], references: [id], [1;93monUpdate: SetDefault[0m, onDelete: SetDefault) | ||
// [1;94m | [0m |