forked from diesel-rs/diesel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.rs
More file actions
321 lines (289 loc) · 11.2 KB
/
Copy pathdatabase.rs
File metadata and controls
321 lines (289 loc) · 11.2 KB
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
use clap::ArgMatches;
use diesel::expression::sql;
#[cfg(feature="postgres")]
use diesel::pg::PgConnection;
#[cfg(feature="sqlite")]
use diesel::sqlite::SqliteConnection;
#[cfg(feature="mysql")]
use diesel::mysql::MysqlConnection;
use diesel::types::Bool;
use diesel::*;
use database_error::{DatabaseError, DatabaseResult};
use std::error::Error;
use std::env;
enum Backend {
#[cfg(feature="postgres")]
Pg,
#[cfg(feature="sqlite")]
Sqlite,
#[cfg(feature="mysql")]
Mysql,
}
impl Backend {
fn for_url(database_url: &str) -> Self {
match database_url {
#[cfg(feature="postgres")]
_ if database_url.starts_with("postgres://") || database_url.starts_with("postgresql://") =>
Backend::Pg,
#[cfg(feature="mysql")]
_ if database_url.starts_with("mysql://") =>
Backend::Mysql,
#[cfg(feature="sqlite")]
_ => Backend::Sqlite,
#[cfg(not(feature="sqlite"))]
_ => {
let mut available_schemes: Vec<&str> = Vec::new();
// One of these will always be true, or you are compiling
// diesel_cli without a backend. And why would you ever want to
// do that?
if cfg!(feature="postgres") {
available_schemes.push("`postgres://`");
}
if cfg!(feature="mysql") {
available_schemes.push("`mysql://`");
}
panic!("`{}` is not a valid database URL. It should start with {}",
database_url, available_schemes.join(" or "));
},
}
}
}
pub enum InferConnection {
#[cfg(feature="postgres")]
Pg(PgConnection),
#[cfg(feature="sqlite")]
Sqlite(SqliteConnection),
#[cfg(feature="mysql")]
Mysql(MysqlConnection),
}
impl InferConnection {
pub fn establish(database_url: &str) -> DatabaseResult<Self> {
match Backend::for_url(database_url) {
#[cfg(feature="postgres")]
Backend::Pg => PgConnection::establish(database_url)
.map(InferConnection::Pg),
#[cfg(feature="sqlite")]
Backend::Sqlite => SqliteConnection::establish(database_url)
.map(InferConnection::Sqlite),
#[cfg(feature="mysql")]
Backend::Mysql => MysqlConnection::establish(database_url)
.map(InferConnection::Mysql),
}.map_err(Into::into)
}
}
macro_rules! call_with_conn {
(
$database_url:expr,
$($func:ident)::+
) => {
call_with_conn!($database_url, $($func)::+ ())
};
(
$database_url:expr,
$($func:ident)::+ ($($args:expr),*)
) => {
match ::database::InferConnection::establish(&$database_url).unwrap() {
#[cfg(feature="postgres")]
::database::InferConnection::Pg(ref conn) => $($func)::+ (conn, $($args),*),
#[cfg(feature="sqlite")]
::database::InferConnection::Sqlite(ref conn) => $($func)::+ (conn, $($args),*),
#[cfg(feature="mysql")]
::database::InferConnection::Mysql(ref conn) => $($func)::+ (conn, $($args),*),
}
};
}
pub fn reset_database(args: &ArgMatches) -> DatabaseResult<()> {
try!(drop_database(&database_url(args)));
setup_database(args)
}
pub fn setup_database(args: &ArgMatches) -> DatabaseResult<()> {
let database_url = database_url(args);
try!(create_database_if_needed(&database_url));
create_schema_table_and_run_migrations_if_needed(&database_url)
}
pub fn drop_database_command(args: &ArgMatches) -> DatabaseResult<()> {
drop_database(&database_url(args))
}
/// Creates the database specified in the connection url. It returns an error
/// it it was unable to create the database.
fn create_database_if_needed(database_url: &str) -> DatabaseResult<()> {
match Backend::for_url(database_url) {
#[cfg(feature="postgres")]
Backend::Pg => {
if PgConnection::establish(database_url).is_err() {
let (database, postgres_url) = change_database_of_url(database_url, "postgres");
println!("Creating database: {}", database);
let conn = try!(PgConnection::establish(&postgres_url));
try!(conn.execute(&format!("CREATE DATABASE {}", database)));
}
},
#[cfg(feature="sqlite")]
Backend::Sqlite => {
if !::std::path::Path::new(database_url).exists() {
println!("Creating database: {}", database_url);
try!(SqliteConnection::establish(database_url));
}
},
#[cfg(feature="mysql")]
Backend::Mysql => {
if MysqlConnection::establish(database_url).is_err() {
let (database, mysql_url) = change_database_of_url(database_url, "information_schema");
println!("Creating database: {}", database);
let conn = try!(MysqlConnection::establish(&mysql_url));
try!(conn.execute(&format!("CREATE DATABASE {}", database)));
}
},
}
Ok(())
}
/// Creates the `__diesel_schema_migrations` table if it doesn't exist. If the
/// table didn't exist, it also runs any pending migrations. Returns a
/// `DatabaseError::ConnectionError` if it can't create the table, and exits
/// with a migration error if it can't run migrations.
fn create_schema_table_and_run_migrations_if_needed(database_url: &str)
-> DatabaseResult<()>
{
if !schema_table_exists(database_url).unwrap_or_else(handle_error) {
try!(call_with_conn!(database_url, migrations::setup_database));
call_with_conn!(database_url, migrations::run_pending_migrations).unwrap_or_else(handle_error);
};
Ok(())
}
/// Drops the database specified in the connection url. It returns an error
/// if it was unable to drop the database.
fn drop_database(database_url: &str) -> DatabaseResult<()> {
match Backend::for_url(database_url) {
#[cfg(feature="postgres")]
Backend::Pg => {
let (database, postgres_url) = change_database_of_url(database_url, "postgres");
let conn = try!(PgConnection::establish(&postgres_url));
if try!(pg_database_exists(&conn, &database)) {
println!("Dropping database: {}", database);
try!(conn.execute(&format!("DROP DATABASE IF EXISTS {}", database)));
}
},
#[cfg(feature="sqlite")]
Backend::Sqlite => {
use std::path::Path;
use std::fs;
if Path::new(database_url).exists() {
println!("Dropping database: {}", database_url);
try!(fs::remove_file(&database_url));
}
},
#[cfg(feature="mysql")]
Backend::Mysql => {
let (database, mysql_url) = change_database_of_url(database_url, "information_schema");
let conn = try!(MysqlConnection::establish(&mysql_url));
if try!(mysql_database_exists(&conn, &database)) {
println!("Dropping database: {}", database);
try!(conn.execute(&format!("DROP DATABASE IF EXISTS {}", database)));
}
},
}
Ok(())
}
#[cfg(feature="postgres")]
table! {
pg_database (datname) {
datname -> Text,
datistemplate -> Bool,
}
}
#[cfg(feature="postgres")]
fn pg_database_exists(conn: &PgConnection, database_name: &str) -> QueryResult<bool> {
use self::pg_database::dsl::*;
pg_database.select(datname) // here come dat name!!!! o shit waddup!!!!
.filter(datname.eq(database_name))
.filter(datistemplate.eq(false))
.get_result::<String>(conn)
.optional()
.map(|x| x.is_some())
}
#[cfg(feature="mysql")]
table! {
information_schema.schemata (schema_name) {
schema_name -> Text,
}
}
#[cfg(feature="mysql")]
fn mysql_database_exists(conn: &MysqlConnection, database_name: &str) -> QueryResult<bool> {
use self::schemata::dsl::*;
schemata.select(schema_name)
.filter(schema_name.eq(database_name))
.get_result::<String>(conn)
.optional()
.map(|x| x.is_some())
}
/// Returns true if the `__diesel_schema_migrations` table exists in the
/// database we connect to, returns false if it does not.
pub fn schema_table_exists(database_url: &str) -> DatabaseResult<bool> {
match InferConnection::establish(database_url).unwrap() {
#[cfg(feature="postgres")]
InferConnection::Pg(conn) => {
select(sql::<Bool>("EXISTS \
(SELECT 1 \
FROM information_schema.tables \
WHERE table_name = '__diesel_schema_migrations')"))
.get_result(&conn)
},
#[cfg(feature="sqlite")]
InferConnection::Sqlite(conn) => {
select(sql::<Bool>("EXISTS \
(SELECT 1 \
FROM sqlite_master \
WHERE type = 'table' \
AND name = '__diesel_schema_migrations')"))
.get_result(&conn)
},
#[cfg(feature="mysql")]
InferConnection::Mysql(conn) => {
select(sql::<Bool>("EXISTS \
(SELECT 1 \
FROM information_schema.tables \
WHERE table_name = '__diesel_schema_migrations'
AND table_schema = DATABASE())"))
.get_result(&conn)
},
}.map_err(Into::into)
}
pub fn database_url(matches: &ArgMatches) -> String {
matches.value_of("DATABASE_URL")
.map(|s| s.into())
.or_else(|| env::var("DATABASE_URL").ok())
.unwrap_or_else(|| {
handle_error(DatabaseError::DatabaseUrlMissing)
})
}
#[cfg(any(feature="postgres", feature="mysql"))]
fn change_database_of_url(database_url: &str, default_database: &str) -> (String, String) {
let mut split: Vec<&str> = database_url.split('/').collect();
let database = split.pop().unwrap();
let new_url = format!("{}/{}", split.join("/"), default_database);
(database.to_owned(), new_url)
}
#[cfg_attr(feature="clippy", allow(needless_pass_by_value))]
fn handle_error<E: Error, T>(error: E) -> T {
println!("{}", error);
::std::process::exit(1);
}
#[cfg(all(test, any(feature="postgres", feature="mysql")))]
mod tests {
use super::change_database_of_url;
#[test]
fn split_pg_connection_string_returns_postgres_url_and_database() {
let database = "database".to_owned();
let base_url = "postgresql://localhost:5432".to_owned();
let database_url = format!("{}/{}", base_url, database);
let postgres_url = format!("{}/{}", base_url, "postgres");
assert_eq!((database, postgres_url), change_database_of_url(&database_url, "postgres"));
}
#[test]
fn split_pg_connection_string_handles_user_and_password() {
let database = "database".to_owned();
let base_url = "postgresql://user:password@localhost:5432".to_owned();
let database_url = format!("{}/{}", base_url, database);
let postgres_url = format!("{}/{}", base_url, "postgres");
assert_eq!((database, postgres_url), change_database_of_url(&database_url, "postgres"));
}
}