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
use url::{Url};
use errors::{Result, ResultExt};
#[cfg(feature = "mysql_support")]
mod mysql;
#[cfg(feature = "postgres_support")]
mod postgres;
#[cfg(feature = "sqlite_support")]
mod sqlite;
pub trait Driver {
fn ensure_migration_table_exists(&self);
fn remove_migration_table(&self);
fn get_current_number(&self) -> i32;
fn set_current_number(&self, number: i32);
fn migrate(&self, migration: String, number: i32) -> Result<()>;
}
pub fn get_driver(url: &str) -> Result<Box<Driver>> {
let parsed_url = Url::parse(url)
.chain_err(|| format!("Invalid URL: {}", url))?;
match parsed_url.scheme() {
#[cfg(feature = "postgres_support")]
"postgres" => postgres::Postgres::new(url).map(|d| Box::new(d) as Box<Driver>),
#[cfg(feature = "mysql_support")]
"mysql" => mysql::Mysql::new(url).map(|d| Box::new(d) as Box<Driver>),
#[cfg(feature = "sqlite_support")]
"sqlite" => sqlite::Sqlite::new(url).map(|d| Box::new(d) as Box<Driver>),
_ => bail!("Invalid URL: {}", url)
}
}