-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathinformation_schema.rs
341 lines (299 loc) · 11.3 KB
/
information_schema.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use std::borrow::Cow;
use std::error::Error;
use diesel::backend::Backend;
use diesel::connection::LoadConnection;
use diesel::deserialize::FromSql;
use diesel::dsl::*;
use diesel::expression::QueryMetadata;
#[cfg(feature = "mysql")]
use diesel::mysql::Mysql;
#[cfg(feature = "postgres")]
use diesel::pg::Pg;
use diesel::query_builder::QueryFragment;
use diesel::*;
use self::information_schema::{key_column_usage, table_constraints, tables};
use super::inference;
use super::table_data::TableName;
pub trait DefaultSchema: Backend {
fn default_schema<C>(conn: &mut C) -> QueryResult<String>
where
C: LoadConnection<Backend = Self>,
String: FromSql<sql_types::Text, C::Backend>;
}
#[cfg(feature = "postgres")]
impl DefaultSchema for Pg {
fn default_schema<C>(_conn: &mut C) -> QueryResult<String> {
Ok("public".into())
}
}
#[cfg(feature = "mysql")]
sql_function!(fn database() -> VarChar);
#[cfg(feature = "mysql")]
impl DefaultSchema for Mysql {
fn default_schema<C>(conn: &mut C) -> QueryResult<String>
where
C: LoadConnection<Backend = Self>,
String: FromSql<sql_types::Text, C::Backend>,
{
select(database()).get_result(conn)
}
}
#[allow(clippy::module_inception)]
pub mod information_schema {
use diesel::prelude::{allow_tables_to_appear_in_same_query, table};
table! {
information_schema.tables (table_schema, table_name) {
table_schema -> VarChar,
table_name -> VarChar,
table_type -> VarChar,
}
}
table! {
information_schema.key_column_usage (table_schema, table_name, column_name, constraint_name) {
table_schema -> VarChar,
table_name -> VarChar,
column_name -> VarChar,
constraint_schema -> VarChar,
constraint_name -> VarChar,
ordinal_position -> BigInt,
}
}
table! {
information_schema.table_constraints (table_schema, table_name, constraint_name) {
table_schema -> VarChar,
table_name -> VarChar,
constraint_schema -> VarChar,
constraint_name -> VarChar,
constraint_type -> VarChar,
}
}
table! {
information_schema.referential_constraints (constraint_schema, constraint_name) {
constraint_schema -> VarChar,
constraint_name -> VarChar,
unique_constraint_schema -> Nullable<VarChar>,
unique_constraint_name -> Nullable<VarChar>,
}
}
allow_tables_to_appear_in_same_query!(table_constraints, referential_constraints);
allow_tables_to_appear_in_same_query!(key_column_usage, table_constraints);
}
pub fn get_primary_keys<'a, Conn>(conn: &mut Conn, table: &'a TableName) -> QueryResult<Vec<String>>
where
Conn: LoadConnection,
Conn::Backend: DefaultSchema,
String: FromSql<sql_types::Text, Conn::Backend>,
Order<
Filter<
Filter<
Filter<
Select<key_column_usage::table, key_column_usage::column_name>,
EqAny<
key_column_usage::constraint_name,
Filter<
Select<table_constraints::table, table_constraints::constraint_name>,
Eq<table_constraints::constraint_type, &'static str>,
>,
>,
>,
Eq<key_column_usage::table_name, &'a String>,
>,
Eq<key_column_usage::table_schema, Cow<'a, String>>,
>,
key_column_usage::ordinal_position,
>: QueryFragment<Conn::Backend>,
Conn::Backend: QueryMetadata<sql_types::Text> + 'static,
{
use self::information_schema::key_column_usage::dsl::*;
use self::information_schema::table_constraints::constraint_type;
let pk_query = table_constraints::table
.select(table_constraints::constraint_name)
.filter(constraint_type.eq("PRIMARY KEY"));
let schema_name = match table.schema {
Some(ref name) => Cow::Borrowed(name),
None => Cow::Owned(Conn::Backend::default_schema(conn)?),
};
key_column_usage
.select(column_name)
.filter(constraint_name.eq_any(pk_query))
.filter(table_name.eq(&table.sql_name))
.filter(table_schema.eq(schema_name))
.order(ordinal_position)
.load(conn)
}
pub fn load_table_names<'a, Conn>(
connection: &mut Conn,
schema_name: Option<&'a str>,
) -> Result<Vec<TableName>, Box<dyn Error + Send + Sync + 'static>>
where
Conn: LoadConnection,
Conn::Backend: DefaultSchema + 'static,
String: FromSql<sql_types::Text, Conn::Backend>,
Filter<
Filter<
Filter<
Select<tables::table, tables::table_name>,
Eq<tables::table_schema, Cow<'a, str>>,
>,
NotLike<tables::table_name, &'static str>,
>,
Like<tables::table_type, &'static str>,
>: QueryFragment<Conn::Backend>,
Conn::Backend: QueryMetadata<sql_types::Text>,
{
use self::information_schema::tables::dsl::*;
let default_schema = Conn::Backend::default_schema(connection)?;
let db_schema_name = schema_name
.map(Cow::Borrowed)
.unwrap_or_else(|| Cow::Owned(default_schema.clone()));
let mut table_names = tables
.select(table_name)
.filter(table_schema.eq(db_schema_name))
.filter(table_name.not_like("\\_\\_%"))
.filter(table_type.like("BASE TABLE"))
.load::<String>(connection)?;
table_names.sort_unstable();
Ok(table_names
.into_iter()
.map(|name| TableName {
rust_name: inference::rust_name_for_sql_name(&name),
sql_name: name,
schema: schema_name
.filter(|&schema| schema != default_schema)
.map(|schema| schema.to_owned()),
})
.collect())
}
#[cfg(all(test, feature = "postgres"))]
mod tests {
extern crate dotenvy;
use self::dotenvy::dotenv;
use super::*;
use std::env;
fn connection() -> PgConnection {
dotenv().ok();
let connection_url = env::var("PG_DATABASE_URL")
.or_else(|_| env::var("DATABASE_URL"))
.expect("DATABASE_URL must be set in order to run tests");
let mut connection = PgConnection::establish(&connection_url).unwrap();
connection.begin_test_transaction().unwrap();
connection
}
#[test]
fn skip_views() {
let mut connection = connection();
diesel::sql_query("CREATE TABLE a_regular_table (id SERIAL PRIMARY KEY)")
.execute(&mut connection)
.unwrap();
diesel::sql_query("CREATE VIEW a_view AS SELECT 42")
.execute(&mut connection)
.unwrap();
let table_names = load_table_names(&mut connection, None).unwrap();
assert!(table_names.contains(&TableName::from_name("a_regular_table")));
assert!(!table_names.contains(&TableName::from_name("a_view")));
}
#[test]
fn load_table_names_loads_from_public_schema_if_none_given() {
let mut connection = connection();
diesel::sql_query(
"CREATE TABLE load_table_names_loads_from_public_schema_if_none_given (id SERIAL PRIMARY KEY)",
).execute(&mut connection)
.unwrap();
let table_names = load_table_names(&mut connection, None).unwrap();
for TableName { schema, .. } in &table_names {
assert_eq!(None, *schema);
}
assert!(table_names.contains(&TableName::from_name(
"load_table_names_loads_from_public_schema_if_none_given",
),));
}
#[test]
fn load_table_names_loads_from_custom_schema() {
let mut connection = connection();
diesel::sql_query("CREATE SCHEMA test_schema")
.execute(&mut connection)
.unwrap();
diesel::sql_query("CREATE TABLE test_schema.table_1 (id SERIAL PRIMARY KEY)")
.execute(&mut connection)
.unwrap();
let table_names = load_table_names(&mut connection, Some("test_schema")).unwrap();
assert_eq!(vec![TableName::new("table_1", "test_schema")], table_names);
diesel::sql_query("CREATE TABLE test_schema.table_2 (id SERIAL PRIMARY KEY)")
.execute(&mut connection)
.unwrap();
let table_names = load_table_names(&mut connection, Some("test_schema")).unwrap();
let expected = vec![
TableName::new("table_1", "test_schema"),
TableName::new("table_2", "test_schema"),
];
assert_eq!(expected, table_names);
diesel::sql_query("CREATE SCHEMA other_test_schema")
.execute(&mut connection)
.unwrap();
diesel::sql_query("CREATE TABLE other_test_schema.table_1 (id SERIAL PRIMARY KEY)")
.execute(&mut connection)
.unwrap();
let table_names = load_table_names(&mut connection, Some("test_schema")).unwrap();
let expected = vec![
TableName::new("table_1", "test_schema"),
TableName::new("table_2", "test_schema"),
];
assert_eq!(expected, table_names);
let table_names = load_table_names(&mut connection, Some("other_test_schema")).unwrap();
assert_eq!(
vec![TableName::new("table_1", "other_test_schema")],
table_names
);
}
#[test]
fn load_table_names_output_is_ordered() {
let mut connection = connection();
diesel::sql_query("CREATE SCHEMA test_schema")
.execute(&mut connection)
.unwrap();
diesel::sql_query("CREATE TABLE test_schema.ccc (id SERIAL PRIMARY KEY)")
.execute(&mut connection)
.unwrap();
diesel::sql_query("CREATE TABLE test_schema.aaa (id SERIAL PRIMARY KEY)")
.execute(&mut connection)
.unwrap();
diesel::sql_query("CREATE TABLE test_schema.bbb (id SERIAL PRIMARY KEY)")
.execute(&mut connection)
.unwrap();
let table_names = load_table_names(&mut connection, Some("test_schema"))
.unwrap()
.iter()
.map(|table| table.to_string())
.collect::<Vec<_>>();
assert_eq!(
vec!["test_schema.aaa", "test_schema.bbb", "test_schema.ccc"],
table_names
);
}
#[test]
fn get_primary_keys_only_includes_primary_key() {
let mut connection = connection();
diesel::sql_query("CREATE SCHEMA test_schema")
.execute(&mut connection)
.unwrap();
diesel::sql_query(
"CREATE TABLE test_schema.table_1 (id SERIAL PRIMARY KEY, not_id INTEGER)",
)
.execute(&mut connection)
.unwrap();
diesel::sql_query(
"CREATE TABLE test_schema.table_2 (id INTEGER, id2 INTEGER, not_id INTEGER, PRIMARY KEY (id, id2))",
).execute(&mut connection)
.unwrap();
let table_1 = TableName::new("table_1", "test_schema");
let table_2 = TableName::new("table_2", "test_schema");
assert_eq!(
vec!["id".to_string()],
get_primary_keys(&mut connection, &table_1).unwrap()
);
assert_eq!(
vec!["id".to_string(), "id2".to_string()],
get_primary_keys(&mut connection, &table_2).unwrap()
);
}
}