dbmigrate_lib/drivers/
mod.rs

1///! Driver interface and implementations
2use url::{Url};
3
4use errors::{Result, ResultExt};
5
6#[cfg(feature = "mysql_support")]
7pub mod mysql;
8#[cfg(feature = "postgres_support")]
9pub mod postgres;
10#[cfg(feature = "sqlite_support")]
11pub mod sqlite;
12
13
14/// The common trait that all databases need to implement in order
15/// for migrations to work
16pub trait Driver {
17    /// A fn that will create a migration table if it doesn't exist
18    /// Otherwise do nothing
19    fn ensure_migration_table_exists(&mut self);
20    /// A fn that will delete migration table
21    fn remove_migration_table(&mut self);
22    /// Get the current migration number from the database
23    fn get_current_number(&mut self) -> i32;
24    /// Set the current migration number in the database
25    fn set_current_number(&mut self, number: i32);
26    /// Perform the `migration` content on the database and set
27    /// the migration number to be the `number` given
28    fn migrate(&mut self, migration: String, number: i32) -> Result<()>;
29}
30
31/// Returns a driver instance depending on url
32pub fn get_driver(url: &str) -> Result<Box<Driver>> {
33    let parsed_url = Url::parse(url)
34        .chain_err(|| format!("Invalid URL: {}", url))?;
35
36    match parsed_url.scheme() {
37        #[cfg(feature = "postgres_support")]
38        "postgres" => postgres::Postgres::new(url).map(|d| Box::new(d) as Box<Driver>),
39        #[cfg(feature = "mysql_support")]
40        "mysql" => mysql::Mysql::new(url).map(|d| Box::new(d) as Box<Driver>),
41        #[cfg(feature = "sqlite_support")]
42        "sqlite" => sqlite::Sqlite::new(url).map(|d| Box::new(d) as Box<Driver>),
43        _ => bail!("Invalid URL: {}", url)
44    }
45}
46