Skip to content

Commit 3eacf7f

Browse files
committed
Ensure MySQL schema inference loads tables when no schema given
Reminder: MySQL has no concept of schemas, and calls databases schemas instead. All connections have access to all databases on that server that the user has permission to access. On PG, the default schema is always simply called `public`. For MySQL, it'll be based on the connection URL. In theory we could actually get this information without executing a query, but that would require adding a method to `Connection` explicitly for this which I don't want to do. I'm fine with just executing a query. As an aside, I really want to find a way to require `String: FromSql<types::Text, Self>` as a constraint of `Backend` without forcing that to be specified *every* time I do `DB: Backend`. I haven't written a test case for this, since I still need to figure out what we're going to do about tests which need to mutate schema without fucking with each other.
1 parent e5fb60b commit 3eacf7f

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

diesel_infer_schema/src/information_schema.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ pub trait UsesInformationSchema: Backend {
2020
> + NonAggregate + QueryId + QueryFragment<Self>;
2121

2222
fn type_column() -> Self::TypeColumn;
23+
fn default_schema<C>(conn: &C) -> QueryResult<String> where
24+
C: Connection,
25+
String: FromSql<types::Text, C::Backend>;
2326
}
2427

2528
#[cfg(feature="postgres")]
@@ -29,6 +32,10 @@ impl UsesInformationSchema for Pg {
2932
fn type_column() -> Self::TypeColumn {
3033
self::information_schema::columns::udt_name
3134
}
35+
36+
fn default_schema<C>(_conn: &C) -> QueryResult<String> {
37+
Ok("public".into())
38+
}
3239
}
3340

3441
#[cfg(feature="mysql")]
@@ -38,6 +45,14 @@ impl UsesInformationSchema for Mysql {
3845
fn type_column() -> Self::TypeColumn {
3946
self::information_schema::columns::column_type
4047
}
48+
49+
fn default_schema<C>(conn: &C) -> QueryResult<String> where
50+
C: Connection,
51+
String: FromSql<types::Text, C::Backend>,
52+
{
53+
no_arg_sql_function!(database, types::VarChar);
54+
select(database).get_result(conn)
55+
}
4156
}
4257

4358
mod information_schema {
@@ -114,7 +129,7 @@ pub fn get_table_data<Conn>(conn: &Conn, table: &TableData)
114129
{
115130
use self::information_schema::columns::dsl::*;
116131

117-
let type_column = <Conn::Backend as UsesInformationSchema>::type_column();
132+
let type_column = Conn::Backend::type_column();
118133
columns.select((column_name, type_column, is_nullable))
119134
.filter(table_name.eq(&table.name))
120135
.filter(table_schema.nullable().eq(&table.schema))
@@ -150,7 +165,10 @@ pub fn load_table_names<Conn>(connection: &Conn, schema_name: Option<&str>)
150165
{
151166
use self::information_schema::tables::dsl::*;
152167

153-
let schema_name = schema_name.unwrap_or("public");
168+
let schema_name = match schema_name {
169+
Some(name) => name.into(),
170+
None => Conn::Backend::default_schema(connection)?,
171+
};
154172

155173
tables.select((table_name, table_schema))
156174
.filter(table_schema.eq(schema_name))

0 commit comments

Comments
 (0)