From ff2c751c7431cd5525b047c0dc38ba9004622b06 Mon Sep 17 00:00:00 2001 From: Georg Semmler Date: Wed, 19 May 2021 11:34:54 +0200 Subject: [PATCH] Generate SQL type definitions for unknown types This commit adds support for generating SQL type definitions for unknown types to `diesel print-schema`. The basic idea is to generate the corresponding marker type to always end up with an existing schema. Especially this does not generate any code that is required for serializing/deserializing rust values. Additionally this commit tweaks the `table!` macro to import types from the parent scope. This allows us to just reference those newly generated types easily + simplifies the handling of other custom type imports in my opinion. The old behaviour of having as part of the `table!` definition remains supported. --- CHANGELOG.md | 2 + diesel/src/macros/mod.rs | 3 + diesel/src/pg/types/mod.rs | 1 - diesel_cli/src/cli.rs | 7 + diesel_cli/src/config.rs | 6 + diesel_cli/src/database.rs | 4 +- .../infer_schema_internals/data_structures.rs | 3 +- .../src/infer_schema_internals/mysql.rs | 1 + diesel_cli/src/infer_schema_internals/pg.rs | 1 + .../src/infer_schema_internals/sqlite.rs | 3 +- diesel_cli/src/main.rs | 4 + diesel_cli/src/print_schema.rs | 170 +++++++++++++++--- diesel_cli/tests/print_schema.rs | 27 +++ .../mysql/expected.rs | 8 +- .../postgres/expected.rs | 8 +- .../sqlite/expected.rs | 8 +- .../diesel.toml | 3 + .../postgres/expected.rs | 28 +++ .../postgres/schema.sql | 5 + .../diesel.toml | 3 + .../postgres/expected.rs | 8 + .../postgres/schema.sql | 5 + .../postgres/expected.rs | 7 + .../diesel.toml | 2 + .../postgres/expected.rs | 12 ++ .../postgres/schema.sql | 5 + 26 files changed, 291 insertions(+), 43 deletions(-) create mode 100644 diesel_cli/tests/print_schema/print_schema_default_is_to_generate_custom_types/diesel.toml create mode 100644 diesel_cli/tests/print_schema/print_schema_default_is_to_generate_custom_types/postgres/expected.rs create mode 100644 diesel_cli/tests/print_schema/print_schema_default_is_to_generate_custom_types/postgres/schema.sql create mode 100644 diesel_cli/tests/print_schema/print_schema_disabling_custom_type_works/diesel.toml create mode 100644 diesel_cli/tests/print_schema/print_schema_disabling_custom_type_works/postgres/expected.rs create mode 100644 diesel_cli/tests/print_schema/print_schema_disabling_custom_type_works/postgres/schema.sql create mode 100644 diesel_cli/tests/print_schema/schema_file_contains_custom_types/diesel.toml create mode 100644 diesel_cli/tests/print_schema/schema_file_contains_custom_types/postgres/expected.rs create mode 100644 diesel_cli/tests/print_schema/schema_file_contains_custom_types/postgres/schema.sql diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d4b7b70b6f7..9e80933211ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,8 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/ * Add support for HAVING clauses. +* Diesel CLI will now generate SQL type definitions for SQL types that are not supported by diesel out of the box. It's possible to disable this behavior via the `generate_missing_sql_type_definitions` config option. + ### Removed * All previously deprecated items have been removed. diff --git a/diesel/src/macros/mod.rs b/diesel/src/macros/mod.rs index cbc789017e63..6451c1465b98 100644 --- a/diesel/src/macros/mod.rs +++ b/diesel/src/macros/mod.rs @@ -620,6 +620,7 @@ macro_rules! __diesel_table_impl { #![allow(dead_code)] $($imports)* pub use self::columns::*; + use super::*; /// Re-exports all of the columns of this table, as well as the /// table struct renamed to the module name. This is meant to be @@ -798,6 +799,8 @@ macro_rules! __diesel_table_impl { pub mod columns { use super::table; $($imports)* + #[allow(unused_imports)] + use super::super::*; #[allow(non_camel_case_types, dead_code)] #[derive(Debug, Clone, Copy, $crate::query_builder::QueryId)] diff --git a/diesel/src/pg/types/mod.rs b/diesel/src/pg/types/mod.rs index 220382b5cda4..fe661bcd7853 100644 --- a/diesel/src/pg/types/mod.rs +++ b/diesel/src/pg/types/mod.rs @@ -183,7 +183,6 @@ pub mod sql_types { pub type BigSerial = crate::sql_types::BigInt; /// The `UUID` SQL type. This type can only be used with `feature = "uuid"` - /// (uuid <=0.6) or `feature = "uuidv07"` (uuid = 0.7) /// /// ### [`ToSql`] impls /// diff --git a/diesel_cli/src/cli.rs b/diesel_cli/src/cli.rs index c8c3b955bb12..1ebf99f3148f 100644 --- a/diesel_cli/src/cli.rs +++ b/diesel_cli/src/cli.rs @@ -218,6 +218,13 @@ pub fn build_cli() -> App<'static, 'static> { .multiple(true) .number_of_values(1) .help("A list of types to import for every table, separated by commas"), + ) + .arg( + Arg::with_name("generate-custom-type-definitions") + .long("generate-custom-type-definitions") + .takes_value(true) + .possible_values(&["true", "false"]) + .help("Generate SQL type definitions for types not provided by diesel"), ); let config_arg = Arg::with_name("CONFIG_FILE") diff --git a/diesel_cli/src/config.rs b/diesel_cli/src/config.rs index 6ff3420f8b49..8d97386b928b 100644 --- a/diesel_cli/src/config.rs +++ b/diesel_cli/src/config.rs @@ -67,9 +67,15 @@ pub struct PrintSchema { pub patch_file: Option, #[serde(default)] pub import_types: Option>, + #[serde(default)] + pub generate_missing_sql_type_definitions: Option, } impl PrintSchema { + pub fn generate_missing_sql_type_definitions(&self) -> bool { + self.generate_missing_sql_type_definitions.unwrap_or(true) + } + pub fn schema_name(&self) -> Option<&str> { self.schema.as_deref() } diff --git a/diesel_cli/src/database.rs b/diesel_cli/src/database.rs index a8b71495bd98..e3ff0a7793f8 100644 --- a/diesel_cli/src/database.rs +++ b/diesel_cli/src/database.rs @@ -16,7 +16,7 @@ use std::fs::{self, File}; use std::io::Write; use std::path::Path; -enum Backend { +pub enum Backend { #[cfg(feature = "postgres")] Pg, #[cfg(feature = "sqlite")] @@ -26,7 +26,7 @@ enum Backend { } impl Backend { - fn for_url(database_url: &str) -> Self { + pub fn for_url(database_url: &str) -> Self { match database_url { _ if database_url.starts_with("postgres://") || database_url.starts_with("postgresql://") => diff --git a/diesel_cli/src/infer_schema_internals/data_structures.rs b/diesel_cli/src/infer_schema_internals/data_structures.rs index e8c693a38288..78f803dd2939 100644 --- a/diesel_cli/src/infer_schema_internals/data_structures.rs +++ b/diesel_cli/src/infer_schema_internals/data_structures.rs @@ -15,9 +15,10 @@ pub struct ColumnInformation { pub nullable: bool, } -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub struct ColumnType { pub rust_name: String, + pub sql_name: String, pub is_array: bool, pub is_nullable: bool, pub is_unsigned: bool, diff --git a/diesel_cli/src/infer_schema_internals/mysql.rs b/diesel_cli/src/infer_schema_internals/mysql.rs index 73c0fcdc6004..359bce7a40d2 100644 --- a/diesel_cli/src/infer_schema_internals/mysql.rs +++ b/diesel_cli/src/infer_schema_internals/mysql.rs @@ -92,6 +92,7 @@ pub fn determine_column_type( let unsigned = determine_unsigned(&attr.type_name); Ok(ColumnType { + sql_name: tpe.trim().to_lowercase(), rust_name: tpe.trim().to_camel_case(), is_array: false, is_nullable: attr.nullable, diff --git a/diesel_cli/src/infer_schema_internals/pg.rs b/diesel_cli/src/infer_schema_internals/pg.rs index 0fcbc6e71659..03a2bdd62c38 100644 --- a/diesel_cli/src/infer_schema_internals/pg.rs +++ b/diesel_cli/src/infer_schema_internals/pg.rs @@ -30,6 +30,7 @@ pub fn determine_column_type( } Ok(ColumnType { + sql_name: tpe.to_lowercase(), rust_name: tpe.to_camel_case(), is_array, is_nullable: attr.nullable, diff --git a/diesel_cli/src/infer_schema_internals/sqlite.rs b/diesel_cli/src/infer_schema_internals/sqlite.rs index ed4adca465a8..01ac37af4689 100644 --- a/diesel_cli/src/infer_schema_internals/sqlite.rs +++ b/diesel_cli/src/infer_schema_internals/sqlite.rs @@ -169,7 +169,8 @@ pub fn determine_column_type( }; Ok(ColumnType { - rust_name: path, + rust_name: path.clone(), + sql_name: path, is_array: false, is_nullable: attr.nullable, is_unsigned: false, diff --git a/diesel_cli/src/main.rs b/diesel_cli/src/main.rs index d70f07fb53f0..9c845a6ef630 100644 --- a/diesel_cli/src/main.rs +++ b/diesel_cli/src/main.rs @@ -562,6 +562,10 @@ fn run_infer_schema(matches: &ArgMatches) -> Result<(), Box; @@ -64,6 +66,93 @@ pub fn run_print_schema( Ok(()) } +fn common_diesel_types(types: &mut HashSet<&str>) { + types.insert("Bool"); + types.insert("Integer"); + types.insert("SmallInt"); + types.insert("BigInt"); + types.insert("Binary"); + types.insert("Text"); + types.insert("Double"); + types.insert("Float"); + types.insert("Numeric"); + + // hidden type defs + types.insert("Float4"); + types.insert("Smallint"); + types.insert("Int2"); + types.insert("Int4"); + types.insert("Int8"); + types.insert("Bigint"); + types.insert("Float8"); + types.insert("Decimal"); + types.insert("VarChar"); + types.insert("Varchar"); + types.insert("Char"); + types.insert("Tinytext"); + types.insert("Mediumtext"); + types.insert("Longtext"); + types.insert("Tinyblob"); + types.insert("Blob"); + types.insert("Mediumblob"); + types.insert("Longblob"); + types.insert("Varbinary"); + types.insert("Bit"); +} + +#[cfg(feature = "postgres")] +fn pg_diesel_types() -> HashSet<&'static str> { + let mut types = HashSet::new(); + types.insert("Cidr"); + types.insert("Date"); + types.insert("Inet"); + types.insert("Jsonb"); + types.insert("MacAddr"); + types.insert("Money"); + types.insert("Oid"); + types.insert("Range"); + types.insert("Timestamptz"); + types.insert("Uuid"); + types.insert("Json"); + types.insert("Timestamp"); + types.insert("Record"); + types.insert("Interval"); + + // hidden type defs + types.insert("Int4range"); + types.insert("Int8range"); + types.insert("Daterange"); + types.insert("Numrange"); + types.insert("Tsrange"); + types.insert("Tstzrange"); + types.insert("SmallSerial"); + types.insert("BigSerial"); + types.insert("Serial"); + types.insert("Bytea"); + types.insert("Bpchar"); + types.insert("Macaddr"); + + common_diesel_types(&mut types); + types +} + +#[cfg(feature = "mysql")] +fn mysql_diesel_types() -> HashSet<&'static str> { + let mut types = HashSet::new(); + common_diesel_types(&mut types); + + types.insert("TinyInt"); + types.insert("Tinyint"); + types +} + +#[cfg(feature = "sqlite")] +fn sqlite_diesel_types() -> HashSet<&'static str> { + let mut types = HashSet::new(); + common_diesel_types(&mut types); + types +} + pub fn output_schema( database_url: &str, config: &config::PrintSchema, @@ -78,17 +167,68 @@ pub fn output_schema( let table_data = table_names .into_iter() .map(|t| load_table_data(database_url, t, &config.column_sorting)) - .collect::>>()?; + .collect::, Box>>()?; + + let mut out = String::new(); + writeln!(out, "{}", SCHEMA_HEADER)?; + + if let Some(import_types) = config.import_types() { + for import_type in import_types { + writeln!(out, "use {};", import_type)?; + } + } + writeln!(out)?; + + if config.generate_missing_sql_type_definitions() { + let backend = Backend::for_url(database_url); + let diesel_provided_types = match backend { + #[cfg(feature = "postgres")] + Backend::Pg => pg_diesel_types(), + #[cfg(feature = "sqlite")] + Backend::Sqlite => sqlite_diesel_types(), + #[cfg(feature = "mysql")] + Backend::Mysql => mysql_diesel_types(), + }; + + let mut all_types = table_data + .iter() + .flat_map(|t| t.column_data.iter().map(|c| &c.ty)) + .filter(|t| !diesel_provided_types.contains(&t.rust_name as &str)) + .collect::>(); + + all_types.sort_unstable_by_key(|ty| &ty.rust_name); + all_types.dedup_by_key(|ty| &ty.rust_name); + + for t in all_types { + match backend { + #[cfg(feature = "postgres")] + Backend::Pg => { + if config.with_docs { + writeln!(out, "/// The `{}` SQL type", t.rust_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)?; + } + #[cfg(feature = "sqlite")] + Backend::Sqlite => { + unreachable!("We only generate a closed set of types for sqlite") + } + #[cfg(feature = "mysql")] + Backend::Mysql => todo!(), + } + } + } + let definitions = TableDefinitions { tables: table_data, fk_constraints: foreign_keys, include_docs: config.with_docs, - import_types: config.import_types(), }; - let mut out = String::new(); - writeln!(out, "{}", SCHEMA_HEADER)?; - if let Some(schema_name) = config.schema_name() { write!(out, "{}", ModuleDefinition(schema_name, definitions))?; } else { @@ -105,7 +245,7 @@ pub fn output_schema( Ok(out) } -struct ModuleDefinition<'a>(&'a str, TableDefinitions<'a>); +struct ModuleDefinition<'a>(&'a str, TableDefinitions); impl<'a> Display for ModuleDefinition<'a> { fn fmt(&self, f: &mut Formatter) -> fmt::Result { @@ -119,14 +259,13 @@ impl<'a> Display for ModuleDefinition<'a> { } } -struct TableDefinitions<'a> { +struct TableDefinitions { tables: Vec, fk_constraints: Vec, include_docs: bool, - import_types: Option<&'a [String]>, } -impl<'a> Display for TableDefinitions<'a> { +impl Display for TableDefinitions { fn fmt(&self, f: &mut Formatter) -> fmt::Result { let mut is_first = true; for table in &self.tables { @@ -141,7 +280,6 @@ impl<'a> Display for TableDefinitions<'a> { TableDefinition { table, include_docs: self.include_docs, - import_types: self.import_types, } )?; } @@ -176,7 +314,6 @@ impl<'a> Display for TableDefinitions<'a> { struct TableDefinition<'a> { table: &'a TableData, - import_types: Option<&'a [String]>, include_docs: bool, } @@ -187,13 +324,6 @@ impl<'a> Display for TableDefinition<'a> { let mut out = PadAdapter::new(f); writeln!(out)?; - if let Some(types) = self.import_types { - for import in types { - writeln!(out, "use {};", import)?; - } - writeln!(out)?; - } - if self.include_docs { for d in self.table.docs.lines() { writeln!(out, "///{}{}", if d.is_empty() { "" } else { " " }, d)?; diff --git a/diesel_cli/tests/print_schema.rs b/diesel_cli/tests/print_schema.rs index 2a9849446dd9..014ada318cba 100644 --- a/diesel_cli/tests/print_schema.rs +++ b/diesel_cli/tests/print_schema.rs @@ -171,6 +171,33 @@ 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"], + ) +} + +#[test] +#[cfg(feature = "postgres")] +fn print_schema_default_is_to_generate_custom_types() { + test_print_schema( + "print_schema_default_is_to_generate_custom_types", + vec!["--with-docs"], + ) +} + #[cfg(feature = "sqlite")] const BACKEND: &str = "sqlite"; #[cfg(feature = "postgres")] diff --git a/diesel_cli/tests/print_schema/print_schema_custom_types/mysql/expected.rs b/diesel_cli/tests/print_schema/print_schema_custom_types/mysql/expected.rs index 6c1ab616beec..654e86a223ed 100644 --- a/diesel_cli/tests/print_schema/print_schema_custom_types/mysql/expected.rs +++ b/diesel_cli/tests/print_schema/print_schema_custom_types/mysql/expected.rs @@ -1,18 +1,14 @@ // @generated automatically by Diesel CLI. +use foo::*; +use bar::*; diesel::table! { - use foo::*; - use bar::*; - users1 (id) { id -> Integer, } } diesel::table! { - use foo::*; - use bar::*; - users2 (id) { id -> Integer, } diff --git a/diesel_cli/tests/print_schema/print_schema_custom_types/postgres/expected.rs b/diesel_cli/tests/print_schema/print_schema_custom_types/postgres/expected.rs index f7c6f7aba662..25cf7869a30c 100644 --- a/diesel_cli/tests/print_schema/print_schema_custom_types/postgres/expected.rs +++ b/diesel_cli/tests/print_schema/print_schema_custom_types/postgres/expected.rs @@ -1,18 +1,14 @@ // @generated automatically by Diesel CLI. +use foo::*; +use bar::*; diesel::table! { - use foo::*; - use bar::*; - users1 (id) { id -> Int4, } } diesel::table! { - use foo::*; - use bar::*; - users2 (id) { id -> Int4, } diff --git a/diesel_cli/tests/print_schema/print_schema_custom_types/sqlite/expected.rs b/diesel_cli/tests/print_schema/print_schema_custom_types/sqlite/expected.rs index 6748759d4d8e..f0486a079908 100644 --- a/diesel_cli/tests/print_schema/print_schema_custom_types/sqlite/expected.rs +++ b/diesel_cli/tests/print_schema/print_schema_custom_types/sqlite/expected.rs @@ -1,18 +1,14 @@ // @generated automatically by Diesel CLI. +use foo::*; +use bar::*; diesel::table! { - use foo::*; - use bar::*; - users1 (id) { id -> Nullable, } } diesel::table! { - use foo::*; - use bar::*; - users2 (id) { id -> Nullable, } diff --git a/diesel_cli/tests/print_schema/print_schema_default_is_to_generate_custom_types/diesel.toml b/diesel_cli/tests/print_schema/print_schema_default_is_to_generate_custom_types/diesel.toml new file mode 100644 index 000000000000..750e5ba85830 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_default_is_to_generate_custom_types/diesel.toml @@ -0,0 +1,3 @@ +[print_schema] +file = "src/schema.rs" +with_docs = true diff --git a/diesel_cli/tests/print_schema/print_schema_default_is_to_generate_custom_types/postgres/expected.rs b/diesel_cli/tests/print_schema/print_schema_default_is_to_generate_custom_types/postgres/expected.rs new file mode 100644 index 000000000000..e451ac77528a --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_default_is_to_generate_custom_types/postgres/expected.rs @@ -0,0 +1,28 @@ +// @generated automatically by Diesel CLI. + +/// The `MyType` SQL type +/// +/// (Automatically generated by Diesel.) +#[derive(diesel::SqlType)] +#[postgres(type_name = "my_type")] +pub struct MyType; + +diesel::table! { + /// Representation of the `custom_types` table. + /// + /// (Automatically generated by Diesel.) + custom_types (id) { + /// The `id` column of the `custom_types` table. + /// + /// Its SQL type is `Int4`. + /// + /// (Automatically generated by Diesel.) + id -> Int4, + /// The `custom_enum` column of the `custom_types` table. + /// + /// Its SQL type is `MyType`. + /// + /// (Automatically generated by Diesel.) + custom_enum -> MyType, + } +} diff --git a/diesel_cli/tests/print_schema/print_schema_default_is_to_generate_custom_types/postgres/schema.sql b/diesel_cli/tests/print_schema/print_schema_default_is_to_generate_custom_types/postgres/schema.sql new file mode 100644 index 000000000000..16de9d2dcf63 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_default_is_to_generate_custom_types/postgres/schema.sql @@ -0,0 +1,5 @@ +CREATE TYPE my_type AS ENUM ('foo', 'bar'); +CREATE TABLE custom_types ( + id SERIAL PRIMARY KEY, + custom_enum my_type NOT NULL +); diff --git a/diesel_cli/tests/print_schema/print_schema_disabling_custom_type_works/diesel.toml b/diesel_cli/tests/print_schema/print_schema_disabling_custom_type_works/diesel.toml new file mode 100644 index 000000000000..a9381ebda0ec --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_disabling_custom_type_works/diesel.toml @@ -0,0 +1,3 @@ +[print_schema] +file = "src/schema.rs" +generate_missing_sql_type_definitions = false diff --git a/diesel_cli/tests/print_schema/print_schema_disabling_custom_type_works/postgres/expected.rs b/diesel_cli/tests/print_schema/print_schema_disabling_custom_type_works/postgres/expected.rs new file mode 100644 index 000000000000..1dc573bcc0f7 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_disabling_custom_type_works/postgres/expected.rs @@ -0,0 +1,8 @@ +// @generated automatically by Diesel CLI. + +diesel::table! { + custom_types (id) { + id -> Int4, + custom_enum -> MyType, + } +} diff --git a/diesel_cli/tests/print_schema/print_schema_disabling_custom_type_works/postgres/schema.sql b/diesel_cli/tests/print_schema/print_schema_disabling_custom_type_works/postgres/schema.sql new file mode 100644 index 000000000000..16de9d2dcf63 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_disabling_custom_type_works/postgres/schema.sql @@ -0,0 +1,5 @@ +CREATE TYPE my_type AS ENUM ('foo', 'bar'); +CREATE TABLE custom_types ( + id SERIAL PRIMARY KEY, + custom_enum my_type NOT NULL +); diff --git a/diesel_cli/tests/print_schema/print_schema_type_renaming/postgres/expected.rs b/diesel_cli/tests/print_schema/print_schema_type_renaming/postgres/expected.rs index 1e0a66310d27..04dfedc7c09d 100644 --- a/diesel_cli/tests/print_schema/print_schema_type_renaming/postgres/expected.rs +++ b/diesel_cli/tests/print_schema/print_schema_type_renaming/postgres/expected.rs @@ -1,5 +1,12 @@ // @generated automatically by Diesel CLI. +/// The `UserJob` SQL type +/// +/// (Automatically generated by Diesel.) +#[derive(diesel::SqlType)] +#[postgres(type_name = "user_job")] +pub struct UserJob; + diesel::table! { /// Representation of the `users` table. /// diff --git a/diesel_cli/tests/print_schema/schema_file_contains_custom_types/diesel.toml b/diesel_cli/tests/print_schema/schema_file_contains_custom_types/diesel.toml new file mode 100644 index 000000000000..f57985adb185 --- /dev/null +++ b/diesel_cli/tests/print_schema/schema_file_contains_custom_types/diesel.toml @@ -0,0 +1,2 @@ +[print_schema] +file = "src/schema.rs" diff --git a/diesel_cli/tests/print_schema/schema_file_contains_custom_types/postgres/expected.rs b/diesel_cli/tests/print_schema/schema_file_contains_custom_types/postgres/expected.rs new file mode 100644 index 000000000000..545b05e21f53 --- /dev/null +++ b/diesel_cli/tests/print_schema/schema_file_contains_custom_types/postgres/expected.rs @@ -0,0 +1,12 @@ +// @generated automatically by Diesel CLI. + +#[derive(diesel::SqlType)] +#[postgres(type_name = "my_type")] +pub struct MyType; + +diesel::table! { + custom_types (id) { + id -> Int4, + custom_enum -> MyType, + } +} diff --git a/diesel_cli/tests/print_schema/schema_file_contains_custom_types/postgres/schema.sql b/diesel_cli/tests/print_schema/schema_file_contains_custom_types/postgres/schema.sql new file mode 100644 index 000000000000..16de9d2dcf63 --- /dev/null +++ b/diesel_cli/tests/print_schema/schema_file_contains_custom_types/postgres/schema.sql @@ -0,0 +1,5 @@ +CREATE TYPE my_type AS ENUM ('foo', 'bar'); +CREATE TABLE custom_types ( + id SERIAL PRIMARY KEY, + custom_enum my_type NOT NULL +);