Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
weiznich committed Jun 18, 2021
1 parent f1d1393 commit 825bda2
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 34 deletions.
2 changes: 1 addition & 1 deletion diesel/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ macro_rules! __diesel_table_impl {
},)+],
) => {
$($meta)*
#[allow(unused_imports)]
#[allow(unused_imports, dead_code)]
pub mod $table_name {
pub use self::columns::*;
$($imports)*
Expand Down
4 changes: 1 addition & 3 deletions diesel_cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,7 @@ pub fn build_cli() -> App<'static, 'static> {
)
.arg(
Arg::with_name("generate-custom-type-definitions")
.long("generate-custom-type-definitions")
.takes_value(true)
.possible_values(&["true", "false"])
.long("no-generate-missing-sql-type-definitions")
.help("Generate SQL type definitions for types not provided by diesel"),
);

Expand Down
4 changes: 2 additions & 2 deletions diesel_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,8 @@ fn run_infer_schema(matches: &ArgMatches) -> Result<(), Box<dyn Error + Send + S
config.import_types = Some(types);
}

if let Some(generate_types) = matches.value_of("generate-custom-type-definitions") {
config.generate_missing_sql_type_definitions = Some(generate_types.parse()?);
if matches.is_present("generate-custom-type-definitions") {
config.generate_missing_sql_type_definitions = Some(false);
}

run_print_schema(&database_url, &config, &mut stdout())?;
Expand Down
33 changes: 25 additions & 8 deletions diesel_cli/src/print_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,11 @@ pub fn output_schema(
import_types: config.import_types(),
};

write!(out, "{}", definitions.custom_type_defs)?;

if let Some(schema_name) = config.schema_name() {
write!(out, "{}", ModuleDefinition(schema_name, definitions))?;
} else {
write!(out, "{}", definitions.custom_type_defs)?;
write!(out, "{}", definitions)?;
}

Expand All @@ -235,33 +236,47 @@ struct CustomTypeList {
}

impl CustomTypeList {
#[cfg(feature = "postgres")]
fn contains(&self, tpe: &str) -> bool {
self.types.iter().find(|c| c.rust_name == tpe).is_some()
}
}

impl Display for CustomTypeList {
fn fmt(&self, out: &mut Formatter<'_>) -> fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
if self.types.is_empty() {
return Ok(());
}
for t in &self.types {
match self.backend {
#[cfg(feature = "postgres")]
Backend::Pg => {
if self.with_docs {
writeln!(out, "/// The `{}` SQL type", t.rust_name)?;
writeln!(f, "/// A module containing custom SQL type definitions")?;
writeln!(f, "///")?;
writeln!(f, "/// (Automatically generated by Diesel.)")?;
}
let mut out = PadAdapter::new(f);
writeln!(out, "pub mod sql_types {{")?;
if self.with_docs {
writeln!(out, "/// The `{}` SQL type", t.sql_name)?;
writeln!(out, "///")?;
writeln!(out, "/// (Automatically generated by Diesel.)")?;
}
writeln!(out, "#[derive(diesel::SqlType)]")?;
writeln!(out, "#[postgres(type_name = \"{}\")]", t.sql_name)?;
writeln!(out, "pub struct {};", t.rust_name)?;
writeln!(out)?;
writeln!(f, "}}")?;
}
#[cfg(feature = "sqlite")]
Backend::Sqlite => {
unreachable!("We only generate a closed set of types for sqlite")
let _ = (&f, self.with_docs, t);
}
#[cfg(feature = "mysql")]
Backend::Mysql => todo!(),
Backend::Mysql => {
let _ = (&f, self.with_docs, t);
}
}
}
Ok(())
Expand Down Expand Up @@ -362,16 +377,18 @@ impl<'a> Display for TableDefinition<'a> {
}
}

#[cfg(feature = "postgres")]
for col in &self.table.column_data {
dbg!(&col.rust_name);
if dbg!(self.custom_type_defs.contains(&col.ty.rust_name)) {
if self.custom_type_defs.contains(&col.ty.rust_name) {
if !has_written_import {
writeln!(out, "use diesel::sql_types::*;")?;
}
writeln!(out, "use super::{};", col.ty.rust_name)?;
writeln!(out, "use super::sql_types::{};", col.ty.rust_name)?;
has_written_import = true;
}
}
#[cfg(not(feature = "postgres"))]
let _ = self.custom_type_defs;

if has_written_import {
writeln!(out)?;
Expand Down
11 changes: 1 addition & 10 deletions diesel_cli/tests/print_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,21 +171,12 @@ fn schema_file_is_relative_to_project_root() {
assert!(p.has_file("src/schema.rs"));
}

#[test]
#[cfg(feature = "postgres")]
fn schema_file_contains_custom_types() {
test_print_schema(
"schema_file_contains_custom_types",
vec!["--generate-custom-type-definitions", "true"],
)
}

#[test]
#[cfg(feature = "postgres")]
fn print_schema_disabling_custom_type_works() {
test_print_schema(
"print_schema_disabling_custom_type_works",
vec!["--generate-custom-type-definitions", "false"],
vec!["--no-generate-missing-sql-type-definitions"],
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
// @generated automatically by Diesel CLI.

/// The `MyType` SQL type
/// A module containing custom SQL type definitions
///
/// (Automatically generated by Diesel.)
#[derive(diesel::SqlType)]
#[postgres(type_name = "my_type")]
pub struct MyType;
pub mod sql_types {
/// The `my_type` SQL type
///
/// (Automatically generated by Diesel.)
#[derive(diesel::SqlType)]
#[postgres(type_name = "my_type")]
pub struct MyType;
}

diesel::table! {
use diesel::sql_types::*;
use super::MyType;
use super::sql_types::MyType;

/// Representation of the `custom_types` table.
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
// @generated automatically by Diesel CLI.

/// The `UserJob` SQL type
/// A module containing custom SQL type definitions
///
/// (Automatically generated by Diesel.)
#[derive(diesel::SqlType)]
#[postgres(type_name = "user_job")]
pub struct UserJob;
pub mod sql_types {
/// The `user_job` SQL type
///
/// (Automatically generated by Diesel.)
#[derive(diesel::SqlType)]
#[postgres(type_name = "user_job")]
pub struct UserJob;
}

diesel::table! {
use diesel::sql_types::*;
use super::UserJob;
use super::sql_types::UserJob;

/// Representation of the `users` table.
///
Expand Down

0 comments on commit 825bda2

Please sign in to comment.