Skip to content

Commit

Permalink
Merge pull request prisma#2833 from prisma/format/weird-at-unique
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhoule authored Apr 8, 2022
2 parents ba8e3c2 + 85a4e97 commit 8940c97
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 23 deletions.
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(clippy::vec_init_then_push)]
#![allow(clippy::ptr_arg)] // remove after https://github.com/rust-lang/rust-clippy/issues/8482 is fixed and shipped

pub mod calculate_datamodel; // only exported to be able to unit test it

Expand Down
30 changes: 20 additions & 10 deletions libs/datamodel/core/src/reformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,16 +408,16 @@ impl<'a> Reformatter<'a> {
let attributes = Self::extract_and_sort_attributes(token, true);

// iterate through tokens and reorder attributes
let mut count = 0;
let mut attributes_count = 0;
let inner_pairs_with_sorted_attributes = token.clone().into_inner().map(|p| match p.as_rule() {
Rule::attribute => {
count += 1;
attributes[count - 1].clone()
attributes_count += 1;
attributes[attributes_count - 1].clone()
}
_ => p,
});

// write to target
// Write existing attributes first.
for current in inner_pairs_with_sorted_attributes {
match current.as_rule() {
Rule::non_empty_identifier | Rule::maybe_empty_identifier => {
Expand Down Expand Up @@ -449,12 +449,22 @@ impl<'a> Reformatter<'a> {
}
}

for missing_field_attribute in &self.missing_field_attributes {
if missing_field_attribute.field == field_name && missing_field_attribute.model.as_str() == model_name {
Renderer::render_field_attribute(
&mut target.column_locked_writer_for(2),
&missing_field_attribute.attribute,
)
// Write missing attributes.
let mut column_writer = target.column_locked_writer_for(2); // third column
let mut missing_field_attributes = self
.missing_field_attributes
.iter()
.filter(|missing_field_attribute| {
missing_field_attribute.field == field_name && missing_field_attribute.model.as_str() == model_name
})
.peekable();
if attributes_count > 0 && missing_field_attributes.peek().is_some() {
column_writer.write(" "); // space between attributes
}
while let Some(missing_field_attribute) = missing_field_attributes.next() {
Renderer::render_field_attribute(&mut column_writer, &missing_field_attribute.attribute);
if missing_field_attributes.peek().is_some() {
column_writer.write(" "); // space between attributes
}
}

Expand Down
74 changes: 74 additions & 0 deletions libs/datamodel/core/tests/reformat/reformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1334,3 +1334,77 @@ fn composite_type_native_types_roundtrip() {

expected.assert_eq(&reformat(schema));
}

#[test]
fn added_missing_at_unique_attribute_with_existing_native_type() {
let schema = r#"
generator client {
provider = "prisma-client-js"
previewFeatures = "mongodb"
}
datasource db {
provider = "mongodb"
url = "m...ty"
}
model Foo {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String @unique
json Json
bar Bar
bars Bar[]
baz Baz @relation(fields: [bazId], references: [id])
bazId String @db.ObjectId
list String[]
jsonList Json[]
}
type Bar {
label String
number Int
}
model Baz {
id String @id @default(auto()) @map("_id") @db.ObjectId
foo Foo?
}
"#;

let expected = expect![[r#"
generator client {
provider = "prisma-client-js"
previewFeatures = "mongodb"
}
datasource db {
provider = "mongodb"
url = "m...ty"
}
model Foo {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String @unique
json Json
bar Bar
bars Bar[]
baz Baz @relation(fields: [bazId], references: [id])
bazId String @db.ObjectId @unique
list String[]
jsonList Json[]
}
type Bar {
label String
number Int
}
model Baz {
id String @id @default(auto()) @map("_id") @db.ObjectId
foo Foo?
}
"#]];

expected.assert_eq(&reformat(schema));
}
1 change: 1 addition & 0 deletions libs/datamodel/parser-database/src/relations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ impl Relations {

/// Iterator over relations where the provided model is model A, or the forward side of the
/// relation.
#[allow(clippy::wrong_self_convention)] // this is the name we want
pub(crate) fn from_model(&self, model_a_id: ast::ModelId) -> impl Iterator<Item = RelationId> + '_ {
self.forward
.range((model_a_id, ast::ModelId::ZERO, RelationId::MIN)..(model_a_id, ast::ModelId::MAX, RelationId::MAX))
Expand Down
1 change: 0 additions & 1 deletion libs/datamodel/schema-ast/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,6 @@ impl<'a> Renderer<'a> {
impl<'a> LineWriteable for Renderer<'a> {
fn write(&mut self, param: &str) {
self.is_new = false;
// TODO: Proper result handling.
if self.new_line > 0 || self.maybe_new_line > 0 {
for _i in 0..std::cmp::max(self.new_line, self.maybe_new_line) {
self.stream.write_char('\n').expect("Writer error.");
Expand Down
2 changes: 1 addition & 1 deletion libs/datamodel/schema-ast/src/renderer/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl TableFormat {
if row[index].is_empty() {
row[index] = String::from(text);
} else {
row[index] = format!("{}{}", &row[index], text);
row[index].push_str(text);
}
}
Row::Interleaved(_) => panic!("Cannot append to col in interleaved mode"),
Expand Down
7 changes: 1 addition & 6 deletions libs/prisma-inflector/src/inflector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,7 @@ impl Inflector {
// Rules are all 1-byte characters, so we can use slices.
if first_singular == first_plural {
vec![Rule::regex(
Regex::new(&format!(
"(?i)({}){}$",
first_singular.to_owned(),
singular[1..].to_owned()
))
.unwrap(),
Regex::new(&format!("(?i)({}){}$", first_singular, singular[1..].to_owned())).unwrap(),
format!("${{1}}{}", plural[1..].to_owned()),
)]
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![deny(rust_2018_idioms, unsafe_code, missing_docs)]
#![allow(clippy::trivial_regex)] // these will grow
#![allow(clippy::redundant_closure)] // too eager, sometimes wrong
#![allow(clippy::ptr_arg)] // remove after https://github.com/rust-lang/rust-clippy/issues/8482 is fixed and shipped

mod apply_migration;
mod connection_wrapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl PipelineQuery {
.aggregate_with_session(self.stages, opts, with_session)
.await?;

Ok(vacuum_cursor(cursor, with_session).await?)
vacuum_cursor(cursor, with_session).await
}
}

Expand All @@ -73,7 +73,7 @@ impl FindQuery {
.find_with_session(self.filter, self.options, with_session)
.await?;

Ok(vacuum_cursor(cursor, with_session).await?)
vacuum_cursor(cursor, with_session).await
}
}

Expand Down

0 comments on commit 8940c97

Please sign in to comment.