Skip to content

Commit

Permalink
Merge pull request diesel-rs#3681 from DannyGoldberg/patch-1
Browse files Browse the repository at this point in the history
fix(diesel_cli): use separate lines for multiple annotations
  • Loading branch information
weiznich committed Aug 15, 2023
1 parent 6cd70ff commit cb0abc0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
19 changes: 5 additions & 14 deletions diesel_cli/src/print_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,20 +697,11 @@ impl<'a> Display for ColumnDefinitions<'a> {
}

// Write out attributes
if column.rust_name != column.sql_name || column.ty.max_length.is_some() {
let mut is_first = true;
write!(out, r#"#["#)?;
if column.rust_name != column.sql_name {
write!(out, r#"sql_name = {:?}"#, column.sql_name)?;
is_first = false;
}
if let Some(max_length) = column.ty.max_length {
if !is_first {
write!(out, ", ")?;
}
write!(out, "max_length = {}", max_length)?;
}
writeln!(out, r#"]"#)?;
if column.rust_name != column.sql_name {
writeln!(out, r#"#[sql_name = "{}"]"#, column.sql_name)?;
}
if let Some(max_length) = column.ty.max_length {
writeln!(out, r#"#[max_length = {}]"#, max_length)?;
}

writeln!(out, "{} -> {},", column.rust_name, column_type)?;
Expand Down
6 changes: 6 additions & 0 deletions diesel_cli/tests/print_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ fn print_schema_generated_columns_with_generated_always() {
test_print_schema("print_schema_generated_columns_generated_always", vec![])
}

#[test]
#[cfg(feature = "postgres")]
fn print_schema_multiple_annotations() {
test_print_schema("print_schema_multiple_annotations", vec![])
}

#[test]
#[cfg(feature = "postgres")]
fn print_schema_array_type() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[print_schema]
file = "src/schema.rs"
with_docs = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: diesel_cli/tests/print_schema.rs
description: "Test: print_schema_multiple_annotations"
---
// @generated automatically by Diesel CLI.

diesel::table! {
example (id) {
id -> Int4,
#[sql_name = "type"]
#[max_length = 10]
type_ -> Nullable<Varchar>,
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE example (
id SERIAL PRIMARY KEY,
"type" VARCHAR(10)
);

0 comments on commit cb0abc0

Please sign in to comment.