Skip to content

Commit cfc0aef

Browse files
authored
Merge branch 'main' into fix/allow-missing-datasource-url
2 parents 9b1823b + afec5a9 commit cfc0aef

9 files changed

Lines changed: 139 additions & 22 deletions

File tree

.github/workflows/test-query-compiler-template.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,6 @@ jobs:
7676
username: ${{ secrets.DOCKERHUB_USERNAME }}
7777
password: ${{ secrets.DOCKERHUB_TOKEN }}
7878

79-
- name: Cache Docker images.
80-
uses: ScribeMD/docker-cache@0.5.0
81-
with:
82-
key: docker-${{ inputs.setup_task }}-${{hashFiles('docker-compose.yaml')}}
83-
8479
- name: Set Prisma branch
8580
id: set-prisma-branch
8681
run: |

.github/workflows/test-schema-engine-linux-template.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ jobs:
4444
username: ${{ secrets.DOCKERHUB_USERNAME }}
4545
password: ${{ secrets.DOCKERHUB_TOKEN }}
4646

47-
- name: Cache Docker images.
48-
uses: ScribeMD/docker-cache@0.5.0
49-
with:
50-
key: docker-${{ inputs.database_name }}-${{ hashFiles('docker-compose.yaml') }}
51-
5247
#
5348
# Multithreaded tests
5449
#

.github/workflows/test-schema-engine.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ jobs:
4646
username: ${{ secrets.DOCKERHUB_USERNAME }}
4747
password: ${{ secrets.DOCKERHUB_TOKEN }}
4848

49-
- name: Cache Docker images.
50-
uses: ScribeMD/docker-cache@0.5.0
51-
with:
52-
key: docker-${{ matrix.database.name }}-${{hashFiles('docker-compose.yaml')}}
53-
5449
- name: 'Start ${{ matrix.database.name }}'
5550
run: make start-${{ matrix.database.name }}-single
5651

Cargo.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

psl/parser-database/src/attributes.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ fn visit_scalar_field_attributes(
271271

272272
// @unique
273273
if ctx.visit_optional_single_attr("unique") {
274-
visit_field_unique(scalar_field_id, model_data, ctx);
274+
visit_field_unique(scalar_field_id, model_id, model_data, ctx);
275275
ctx.validate_visited_arguments();
276276
}
277277

@@ -284,7 +284,12 @@ fn visit_scalar_field_attributes(
284284
ctx.validate_visited_attributes();
285285
}
286286

287-
fn visit_field_unique(scalar_field_id: ScalarFieldId, model_data: &mut ModelAttributes, ctx: &mut Context<'_>) {
287+
fn visit_field_unique(
288+
scalar_field_id: ScalarFieldId,
289+
model_id: crate::ModelId,
290+
model_data: &mut ModelAttributes,
291+
ctx: &mut Context<'_>,
292+
) {
288293
let mapped_name = match ctx
289294
.visit_optional_arg("map")
290295
.and_then(|arg| coerce::string(arg, ctx.diagnostics))
@@ -318,6 +323,7 @@ fn visit_field_unique(scalar_field_id: ScalarFieldId, model_data: &mut ModelAttr
318323
};
319324

320325
let clustered = validate_clustering_setting(ctx);
326+
let where_clause = parse_where_clause(model_id, ctx);
321327

322328
let attribute_id = ctx.current_attribute_id();
323329
model_data.ast_indexes.push((
@@ -333,6 +339,7 @@ fn visit_field_unique(scalar_field_id: ScalarFieldId, model_data: &mut ModelAttr
333339
source_field: Some(scalar_field_id),
334340
mapped_name,
335341
clustered,
342+
where_clause,
336343
..Default::default()
337344
},
338345
))

psl/psl/tests/attributes/partial_index.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,3 +1030,76 @@ fn partial_index_object_syntax_rejects_boolean_for_enum_field() {
10301030
"#]];
10311031
expected.assert_eq(&err);
10321032
}
1033+
1034+
#[test]
1035+
fn field_level_partial_unique_with_raw_on_postgres() {
1036+
let dml = indoc! {r#"
1037+
model User {
1038+
id Int @id
1039+
email String @unique(where: raw("status = 'active'"))
1040+
status String
1041+
}
1042+
"#};
1043+
1044+
psl::parse_schema_without_extensions(with_header(dml, Provider::Postgres, &["partialIndexes"]))
1045+
.unwrap()
1046+
.assert_has_model("User")
1047+
.assert_unique_on_fields(&["email"])
1048+
.assert_raw_where_clause("status = 'active'");
1049+
}
1050+
1051+
#[test]
1052+
fn field_level_partial_unique_with_object_syntax() {
1053+
let dml = indoc! {r#"
1054+
model User {
1055+
id Int @id
1056+
email String @unique(where: { active: true })
1057+
active Boolean
1058+
}
1059+
"#};
1060+
1061+
psl::parse_schema_without_extensions(with_header(dml, Provider::Postgres, &["partialIndexes"]))
1062+
.unwrap()
1063+
.assert_has_model("User")
1064+
.assert_unique_on_fields(&["email"])
1065+
.assert_where_object(&[("active", WhereCondition::Equals(WhereValue::Boolean(true)))]);
1066+
}
1067+
1068+
#[test]
1069+
fn field_level_partial_unique_requires_preview_feature() {
1070+
let dml = indoc! {r#"
1071+
model User {
1072+
id Int @id
1073+
email String @unique(where: raw("status = 'active'"))
1074+
status String
1075+
}
1076+
"#};
1077+
1078+
let err = parse_unwrap_err(&with_header(dml, Provider::Postgres, &[]));
1079+
let expected = expect![[r#"
1080+
error: Error parsing attribute "@unique": Partial indexes are a preview feature. Add "partialIndexes" to previewFeatures in your generator block.
1081+
--> schema.prisma:12
1082+
 | 
1083+
11 |  id Int @id
1084+
12 |  email String @unique(where: raw("status = 'active'"))
1085+
 | 
1086+
"#]];
1087+
expected.assert_eq(&err);
1088+
}
1089+
1090+
#[test]
1091+
fn field_level_partial_unique_with_map() {
1092+
let dml = indoc! {r#"
1093+
model User {
1094+
id Int @id
1095+
email String @unique(map: "email_active_idx", where: raw("status = 'active'"))
1096+
status String
1097+
}
1098+
"#};
1099+
1100+
psl::parse_schema_without_extensions(with_header(dml, Provider::Postgres, &["partialIndexes"]))
1101+
.unwrap()
1102+
.assert_has_model("User")
1103+
.assert_unique_on_fields(&["email"])
1104+
.assert_raw_where_clause("status = 'active'");
1105+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
model Post {
2+
id Int @id
3+
title String
4+
published Boolean
5+
6+
@@unique([title], where:{published:{not:false}})
7+
@@index([title], where:{published:true})
8+
@@index([title], where:{published:{not: false},id:{not: null}})
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
model Post {
2+
id Int @id
3+
title String
4+
published Boolean
5+
6+
@@unique([title], where: { published: { not: false } })
7+
@@index([title], where: { published: true })
8+
@@index([title], where: { published: { not: false }, id: { not: null } })
9+
}

psl/schema-ast/src/reformat.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,40 @@ fn reformat_expression(pair: Pair<'_>, target: &mut dyn LineWriteable) {
377377
Rule::path => target.write(current.as_str()),
378378
Rule::function_call => reformat_function_call(current, target),
379379
Rule::array_expression => reformat_array_expression(current, target),
380+
Rule::object_expression => reformat_object_expression(current, target),
381+
_ => unreachable(&current),
382+
}
383+
}
384+
}
385+
386+
fn reformat_object_expression(pair: Pair<'_>, target: &mut dyn LineWriteable) {
387+
target.write("{ ");
388+
let mut is_first_member = true;
389+
390+
for current in pair.into_inner() {
391+
match current.as_rule() {
392+
Rule::object_member => {
393+
if !is_first_member {
394+
target.write(", ");
395+
}
396+
reformat_object_member(current, target);
397+
is_first_member = false;
398+
}
399+
_ => unreachable(&current),
400+
}
401+
}
402+
403+
target.write(" }");
404+
}
405+
406+
fn reformat_object_member(pair: Pair<'_>, target: &mut dyn LineWriteable) {
407+
for current in pair.into_inner() {
408+
match current.as_rule() {
409+
Rule::identifier => {
410+
target.write(current.as_str());
411+
target.write(": ");
412+
}
413+
Rule::expression => reformat_expression(current, target),
380414
_ => unreachable(&current),
381415
}
382416
}

0 commit comments

Comments
 (0)