Skip to content

Commit 5900235

Browse files
authored
Merge pull request diesel-rs#1637 from diesel-rs/sg-print-schema-custom-types
Allow specifying type imports for `diesel print-schema`
2 parents e67985b + 70f7db6 commit 5900235

12 files changed

Lines changed: 116 additions & 3 deletions

File tree

diesel_cli/src/cli.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ pub fn build_cli() -> App<'static, 'static> {
134134
.long("patch-file")
135135
.takes_value(true)
136136
.help("A unified diff file to be applied to the final schema"),
137+
)
138+
.arg(
139+
Arg::with_name("import-types")
140+
.long("import-types")
141+
.takes_value(true)
142+
.multiple(true)
143+
.number_of_values(1)
144+
.help("A list of types to import for every table, separated by commas"),
137145
);
138146

139147
let config_arg = Arg::with_name("CONFIG_FILE")

diesel_cli/src/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,16 @@ pub struct PrintSchema {
5050
pub schema: Option<String>,
5151
#[serde(default)]
5252
pub patch_file: Option<PathBuf>,
53+
#[serde(default)]
54+
pub import_types: Option<Vec<String>>,
5355
}
5456

5557
impl PrintSchema {
5658
pub fn schema_name(&self) -> Option<&str> {
5759
self.schema.as_ref().map(|s| &**s)
5860
}
61+
62+
pub fn import_types(&self) -> Option<&[String]> {
63+
self.import_types.as_ref().map(|v| &**v)
64+
}
5965
}

diesel_cli/src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,11 @@ fn run_infer_schema(matches: &ArgMatches) -> Result<(), Box<Error>> {
366366
config.patch_file = Some(PathBuf::from(path));
367367
}
368368

369+
if let Some(types) = matches.values_of("import-types") {
370+
let types = types.map(String::from).collect();
371+
config.import_types = Some(types);
372+
}
373+
369374
run_print_schema(&database_url, &config)?;
370375
Ok(())
371376
}

diesel_cli/src/print_schema.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pub fn output_schema(
7070
tables: table_data,
7171
fk_constraints: foreign_keys,
7272
include_docs: config.with_docs,
73+
import_types: config.import_types(),
7374
};
7475

7576
if let Some(schema_name) = config.schema_name() {
@@ -96,7 +97,7 @@ pub fn output_schema(
9697
Ok(())
9798
}
9899

99-
struct ModuleDefinition<'a>(&'a str, TableDefinitions);
100+
struct ModuleDefinition<'a>(&'a str, TableDefinitions<'a>);
100101

101102
impl<'a> Display for ModuleDefinition<'a> {
102103
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
@@ -110,13 +111,14 @@ impl<'a> Display for ModuleDefinition<'a> {
110111
}
111112
}
112113

113-
struct TableDefinitions {
114+
struct TableDefinitions<'a> {
114115
tables: Vec<TableData>,
115116
fk_constraints: Vec<ForeignKeyConstraint>,
116117
include_docs: bool,
118+
import_types: Option<&'a [String]>,
117119
}
118120

119-
impl Display for TableDefinitions {
121+
impl<'a> Display for TableDefinitions<'a> {
120122
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
121123
let mut is_first = true;
122124
for table in &self.tables {
@@ -131,6 +133,7 @@ impl Display for TableDefinitions {
131133
TableDefinition {
132134
table,
133135
include_docs: self.include_docs,
136+
import_types: self.import_types,
134137
}
135138
)?;
136139
}
@@ -161,6 +164,7 @@ impl Display for TableDefinitions {
161164

162165
struct TableDefinition<'a> {
163166
table: &'a TableData,
167+
import_types: Option<&'a [String]>,
164168
include_docs: bool,
165169
}
166170

@@ -171,6 +175,13 @@ impl<'a> Display for TableDefinition<'a> {
171175
let mut out = PadAdapter::new(f);
172176
write!(out, "\n")?;
173177

178+
if let Some(types) = self.import_types {
179+
for import in types {
180+
writeln!(out, "use {};", import)?;
181+
}
182+
write!(out, "\n")?;
183+
}
184+
174185
if self.include_docs {
175186
for d in self.table.docs.lines() {
176187
writeln!(out, "///{}{}", if d.is_empty() { "" } else { " " }, d)?;

diesel_cli/tests/print_schema.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ fn print_schema_patch_file() {
8282
test_print_schema("print_schema_patch_file", vec!["--patch-file", &path]);
8383
}
8484

85+
#[test]
86+
fn print_schema_custom_types() {
87+
test_print_schema(
88+
"print_schema_custom_types",
89+
vec!["--import-types", "foo::*", "--import-types", "bar::*"],
90+
);
91+
}
92+
8593
#[cfg(feature = "sqlite")]
8694
const BACKEND: &str = "sqlite";
8795
#[cfg(feature = "postgres")]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[print_schema]
2+
file = "src/schema.rs"
3+
import_types = ["foo::*", "bar::*"]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
table! {
2+
use foo::*;
3+
use bar::*;
4+
5+
users1 (id) {
6+
id -> Integer,
7+
}
8+
}
9+
10+
table! {
11+
use foo::*;
12+
use bar::*;
13+
14+
users2 (id) {
15+
id -> Integer,
16+
}
17+
}
18+
19+
allow_tables_to_appear_in_same_query!(
20+
users1,
21+
users2,
22+
);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CREATE TABLE users1 (id INTEGER PRIMARY KEY);
2+
CREATE TABLE users2 (id INTEGER PRIMARY KEY);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
table! {
2+
use foo::*;
3+
use bar::*;
4+
5+
users1 (id) {
6+
id -> Int4,
7+
}
8+
}
9+
10+
table! {
11+
use foo::*;
12+
use bar::*;
13+
14+
users2 (id) {
15+
id -> Int4,
16+
}
17+
}
18+
19+
allow_tables_to_appear_in_same_query!(
20+
users1,
21+
users2,
22+
);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CREATE TABLE users1 (id SERIAL PRIMARY KEY);
2+
CREATE TABLE users2 (id SERIAL PRIMARY KEY);

0 commit comments

Comments
 (0)