Skip to content

Commit 650d628

Browse files
committed
Move default_schema handling into table loading
The concept of `default_schema` is only a thing for `information_schema` implementors, so we should handle it there.
1 parent afb8129 commit 650d628

2 files changed

Lines changed: 32 additions & 40 deletions

File tree

diesel_infer_schema/src/codegen.rs

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,21 @@ pub fn expand_infer_table_from_schema(database_url: &str, table: &TableData)
2323
for a in data {
2424
tokens.push(column_def_tokens(table, &a, &connection)?);
2525
}
26-
let default_schema = default_schema(&connection);
27-
if table.schema != default_schema {
28-
if let Some(ref schema) = table.schema {
29-
let schema_name = syn::Ident::new(&schema[..]);
30-
return Ok(quote!(table! {
31-
#schema_name.#table_name (#(#primary_keys),*) {
32-
#(#tokens),*,
33-
}
34-
}));
35-
}
26+
27+
if let Some(ref schema) = table.schema {
28+
let schema_name = syn::Ident::new(&**schema);
29+
Ok(quote!(table! {
30+
#schema_name.#table_name (#(#primary_keys),*) {
31+
#(#tokens),*,
32+
}
33+
}))
34+
} else {
35+
Ok(quote!(table! {
36+
#table_name (#(#primary_keys),*) {
37+
#(#tokens),*,
38+
}
39+
}))
3640
}
37-
Ok(quote!(table! {
38-
#table_name (#(#primary_keys),*) {
39-
#(#tokens),*,
40-
}
41-
}))
4241
}
4342

4443
pub fn handle_schema<I>(tables: I, schema_name: Option<&str>) -> quote::Tokens
@@ -82,19 +81,3 @@ fn column_def_tokens(
8281
}
8382
Ok(quote!(#column_name -> #tpe))
8483
}
85-
86-
fn default_schema(conn: &InferConnection) -> Option<String> {
87-
#[cfg(feature="mysql")]
88-
use information_schema::UsesInformationSchema;
89-
#[cfg(feature="mysql")]
90-
use diesel::mysql::Mysql;
91-
92-
match *conn {
93-
#[cfg(feature="sqlite")]
94-
InferConnection::Sqlite(_) => None,
95-
#[cfg(feature="postgres")]
96-
InferConnection::Pg(_) => Some("public".into()),
97-
#[cfg(feature="mysql")]
98-
InferConnection::Mysql(ref c) => Mysql::default_schema(c).ok(),
99-
}
100-
}

diesel_infer_schema/src/information_schema.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,24 @@ pub fn load_table_names<Conn>(connection: &Conn, schema_name: Option<&str>)
164164
{
165165
use self::information_schema::tables::dsl::*;
166166

167+
let default_schema = Conn::Backend::default_schema(connection)?;
167168
let schema_name = match schema_name {
168-
Some(name) => name.into(),
169-
None => Conn::Backend::default_schema(connection)?,
169+
Some(name) => name,
170+
None => &default_schema,
170171
};
171172

172-
tables.select((table_name, table_schema))
173+
let mut table_names = tables.select((table_name, table_schema))
173174
.filter(table_schema.eq(schema_name))
174175
.filter(table_name.not_like("\\_\\_%"))
175176
.filter(table_type.like("BASE TABLE"))
176177
.order(table_name)
177-
.load(connection)
178-
.map_err(Into::into)
178+
.load::<TableData>(connection)?;
179+
if schema_name == default_schema {
180+
for table in &mut table_names {
181+
table.schema = None;
182+
}
183+
}
184+
Ok(table_names)
179185
}
180186

181187
#[cfg_attr(feature = "clippy", allow(similar_names))]
@@ -255,8 +261,8 @@ mod tests {
255261

256262
let table_names = load_table_names(&connection, None).unwrap();
257263

258-
assert!(table_names.contains(&TableData::new("a_regular_table", "public")));
259-
assert!(!table_names.contains(&TableData::new("a_view", "public")));
264+
assert!(table_names.contains(&TableData::from_name("a_regular_table")));
265+
assert!(!table_names.contains(&TableData::from_name("a_view")));
260266
}
261267

262268
#[test]
@@ -267,9 +273,12 @@ mod tests {
267273
.unwrap();
268274

269275
let table_names = load_table_names(&connection, None).unwrap();
270-
for TableData { schema, .. } in table_names {
271-
assert_eq!(Some("public".into()), schema);
276+
for &TableData { ref schema, .. } in &table_names {
277+
assert_eq!(None, *schema);
272278
}
279+
assert!(table_names.contains(&TableData::from_name(
280+
"load_table_names_loads_from_public_schema_if_none_given",
281+
)));
273282
}
274283

275284
#[test]

0 commit comments

Comments
 (0)