-
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 validation + warning on missing indexes for `relationM…
…ode = "prisma"` (#3429) * feat(psl): add "push_warning" helper to Context * test(psl): add validation tests for missing relation index warning * feat(psl): add skeleton for missing relation indexes when relationMode="prisma" * feat(psl): integrated "validate_missing_relation_indexes" into "validate" function * feat(psl): extracted sets of indexes from the model containing a @relation * feat(psl): fixed missing relation index detection * feat(psl): add pretty_print support to warnings, defaulting to yellow colors * feat(psl): add warning support to validation_tests suite * chore(psl): fix clippy * chore(psl): removed leftover code * chore(psl): address review comments * chore(psl): address review comments * chore: removed redundant static declaration * chore: fix clippy * chore: removed noise * chore: fix clippy, add one more unit test * feat(psl): moved connector-specific logic to the "Connector" trait * feat(psl): attempt to reduce allocations in "validate_missing_relation_indexes" * chore(psl): applied review comments, fixed regression in "validate_missing_relation_indexes" * fix: changed duplicated test * feat(psl): add + Clone to "referencing_fields" * chore: swapped at_unique tests * chore: apply review comments on "validate_missing_relation_indexes" for better readability * chore: removed model argument from "validate_missing_relation_indexes" * chore: moved the "validate_missing_relation_indexes" function before the rest of index validations * chore: changed pris.ly link in the "new_missing_index_on_emulated_relation" warning * chore: updated "new_missing_index_on_emulated_relation" warning message * chore: update Cargo.lock * chore: removed deprecated "referentialIntegrity" preview feature in newly added tests * chore(psl): updated warnings snapshot * chore(psl): fixed unit tests? * chore: apply review suggestions * chore: rephase wording in warning
- Loading branch information
Showing
18 changed files
with
330 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -6,3 +6,4 @@ edition = "2021" | |
[dependencies] | ||
colored = "2" | ||
pest = "2.1.3" | ||
indoc = "1" |
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
17 changes: 17 additions & 0 deletions
17
psl/psl/tests/validation/attributes/index/missing_index_warning/mongodb.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,17 @@ | ||
// no missing relation index validation on mongodb. | ||
|
||
datasource db { | ||
provider = "mongodb" | ||
url = env("TEST_DATABASE_URL") | ||
} | ||
|
||
model SomeUser { | ||
id String @id @map("_id") @default(auto()) @db.ObjectId | ||
posts Post[] | ||
} | ||
|
||
model Post { | ||
id String @id @map("_id") @default(auto()) @db.ObjectId | ||
userId String | ||
user SomeUser @relation(fields: [userId], references: [id]) | ||
} |
17 changes: 17 additions & 0 deletions
17
...tests/validation/attributes/index/missing_index_warning/relation_mode_foreign_keys.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,17 @@ | ||
// no missing relation index validation on relationMode = "foreignKeys" / no relationMode at all. | ||
|
||
datasource db { | ||
provider = "mysql" | ||
url = env("TEST_DATABASE_URL") | ||
} | ||
|
||
model SomeUser { | ||
id Int @id | ||
posts Post[] | ||
} | ||
|
||
model Post { | ||
id Int @id | ||
userId Int | ||
user SomeUser @relation(fields: [userId], references: [id]) | ||
} |
20 changes: 20 additions & 0 deletions
20
...attributes/index/missing_index_warning/relation_mode_prisma/one_field_at_at_unique.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,20 @@ | ||
// no relation index validation warning on relationMode = "prisma" when a referenced field is already in @@unique. | ||
|
||
datasource db { | ||
provider = "mysql" | ||
url = env("TEST_DATABASE_URL") | ||
relationMode = "prisma" | ||
} | ||
|
||
model SomeUser { | ||
id Int @id | ||
profile Profile? | ||
} | ||
|
||
model Profile { | ||
id Int @id | ||
userId Int | ||
user SomeUser? @relation(fields: [userId], references: [id]) | ||
@@unique([userId]) | ||
} |
18 changes: 18 additions & 0 deletions
18
...on/attributes/index/missing_index_warning/relation_mode_prisma/one_field_at_unique.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,18 @@ | ||
// no relation index validation warning on relationMode = "prisma" when a referenced field is already in @unique. | ||
|
||
datasource db { | ||
provider = "mysql" | ||
url = env("TEST_DATABASE_URL") | ||
relationMode = "prisma" | ||
} | ||
|
||
model SomeUser { | ||
id Int @id | ||
profile Profile? | ||
} | ||
|
||
model Profile { | ||
id Int @id | ||
userId Int @unique | ||
user SomeUser? @relation(fields: [userId], references: [id]) | ||
} |
26 changes: 26 additions & 0 deletions
26
...ion/attributes/index/missing_index_warning/relation_mode_prisma/one_field_no_index.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,26 @@ | ||
// add missing relation index validation warning on relationMode = "prisma". | ||
|
||
datasource db { | ||
provider = "mysql" | ||
url = env("TEST_DATABASE_URL") | ||
relationMode = "prisma" | ||
} | ||
|
||
model SomeUser { | ||
id Int @id | ||
posts Post[] | ||
} | ||
|
||
model Post { | ||
id Int @id | ||
userId Int | ||
user SomeUser @relation(fields: [userId], references: [id]) | ||
} | ||
// [1;93mwarning[0m: [1mWith `relationMode = \"prisma\"`, no foreign keys are used, so relation fields will not benefit from the index usually created by the relational database under the hood. | ||
// This can lead to poor performance when querying these fields. We recommend adding an index manually. | ||
// Learn more at https://pris.ly/d/relation-mode#indexes[0m | ||
// [1;94m-->[0m [4mschema.prisma:17[0m | ||
// [1;94m | [0m | ||
// [1;94m16 | [0m userId Int | ||
// [1;94m17 | [0m user SomeUser [1;93m@relation(fields: [userId], references: [id])[0m | ||
// [1;94m | [0m |
34 changes: 34 additions & 0 deletions
34
...butes/index/missing_index_warning/relation_mode_prisma/three_fields_index_leftmost.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,34 @@ | ||
// add missing relation index validation warning on relationMode = "prisma". | ||
|
||
datasource db { | ||
provider = "mysql" | ||
url = env("TEST_DATABASE_URL") | ||
relationMode = "prisma" | ||
} | ||
|
||
model SomeUser { | ||
idA Int | ||
idB Int | ||
idC Int | ||
posts Post[] | ||
@@id([idA, idB, idC]) | ||
} | ||
|
||
model Post { | ||
id Int @id | ||
userIdA Int | ||
userIdB Int | ||
userIdC Int | ||
user SomeUser @relation(fields: [userIdA, userIdB, userIdC], references: [idA, idB, idC]) | ||
@@index([userIdA, userIdB]) | ||
} | ||
// [1;93mwarning[0m: [1mWith `relationMode = \"prisma\"`, no foreign keys are used, so relation fields will not benefit from the index usually created by the relational database under the hood. | ||
// This can lead to poor performance when querying these fields. We recommend adding an index manually. | ||
// Learn more at https://pris.ly/d/relation-mode#indexes[0m | ||
// [1;94m-->[0m [4mschema.prisma:23[0m | ||
// [1;94m | [0m | ||
// [1;94m22 | [0m userIdC Int | ||
// [1;94m23 | [0m user SomeUser [1;93m@relation(fields: [userIdA, userIdB, userIdC], references: [idA, idB, idC])[0m | ||
// [1;94m | [0m |
34 changes: 34 additions & 0 deletions
34
...utes/index/missing_index_warning/relation_mode_prisma/three_fields_index_rightmost.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,34 @@ | ||
// add missing relation index validation warning on relationMode = "prisma". | ||
|
||
datasource db { | ||
provider = "mysql" | ||
url = env("TEST_DATABASE_URL") | ||
relationMode = "prisma" | ||
} | ||
|
||
model SomeUser { | ||
idA Int | ||
idB Int | ||
idC Int | ||
posts Post[] | ||
@@id([idA, idB, idC]) | ||
} | ||
|
||
model Post { | ||
id Int @id | ||
userIdA Int | ||
userIdB Int | ||
userIdC Int | ||
user SomeUser @relation(fields: [userIdA, userIdB, userIdC], references: [idA, idB, idC]) | ||
@@index([userIdB, userIdC]) | ||
} | ||
// [1;93mwarning[0m: [1mWith `relationMode = \"prisma\"`, no foreign keys are used, so relation fields will not benefit from the index usually created by the relational database under the hood. | ||
// This can lead to poor performance when querying these fields. We recommend adding an index manually. | ||
// Learn more at https://pris.ly/d/relation-mode#indexes[0m | ||
// [1;94m-->[0m [4mschema.prisma:23[0m | ||
// [1;94m | [0m | ||
// [1;94m22 | [0m userIdC Int | ||
// [1;94m23 | [0m user SomeUser [1;93m@relation(fields: [userIdA, userIdB, userIdC], references: [idA, idB, idC])[0m | ||
// [1;94m | [0m |
34 changes: 34 additions & 0 deletions
34
...ributes/index/missing_index_warning/relation_mode_prisma/three_fields_mixed_unique.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,34 @@ | ||
// add missing relation index validation warning on relationMode = "prisma". | ||
|
||
datasource db { | ||
provider = "mysql" | ||
url = env("TEST_DATABASE_URL") | ||
relationMode = "prisma" | ||
} | ||
|
||
model SomeUser { | ||
idA Int | ||
idB Int | ||
idC Int | ||
posts Post[] | ||
@@id([idA, idB, idC]) | ||
} | ||
|
||
model Post { | ||
id Int @id | ||
userIdA Int @unique | ||
userIdB Int | ||
userIdC Int @unique | ||
user SomeUser @relation(fields: [userIdA, userIdB, userIdC], references: [idA, idB, idC]) | ||
@@unique([userIdA, userIdB]) | ||
} | ||
// [1;93mwarning[0m: [1mWith `relationMode = \"prisma\"`, no foreign keys are used, so relation fields will not benefit from the index usually created by the relational database under the hood. | ||
// This can lead to poor performance when querying these fields. We recommend adding an index manually. | ||
// Learn more at https://pris.ly/d/relation-mode#indexes[0m | ||
// [1;94m-->[0m [4mschema.prisma:23[0m | ||
// [1;94m | [0m | ||
// [1;94m22 | [0m userIdC Int @unique | ||
// [1;94m23 | [0m user SomeUser [1;93m@relation(fields: [userIdA, userIdB, userIdC], references: [idA, idB, idC])[0m | ||
// [1;94m | [0m |
Oops, something went wrong.