Browse Source

error if DATABASE_URL can't be parsed:

DbConnType.from_url() returns error if it fails to parse DATABASE_URL
pull/2872/head
Vilhelm Bergsøe 3 years ago
committed by Vilhelm Bergsoe
parent
commit
9a340a50c7
  1. 41
      src/db/mod.rs

41
src/db/mod.rs

@ -190,23 +190,30 @@ generate_connections! {
impl DbConnType { impl DbConnType {
pub fn from_url(url: &str) -> Result<DbConnType, Error> { pub fn from_url(url: &str) -> Result<DbConnType, Error> {
// Mysql if url.contains(':')
if url.starts_with("mysql:") { && !(url.len() >= 2
#[cfg(mysql)] && url.chars().next().unwrap().is_ascii_alphabetic()
return Ok(DbConnType::mysql); && url.chars().nth(1).unwrap() == ':')
{
#[cfg(not(mysql))] // MySQL
err!("`DATABASE_URL` is a MySQL URL, but the 'mysql' feature is not enabled") if url.starts_with("mysql:") {
#[cfg(mysql)]
// Postgres return Ok(DbConnType::mysql);
} else if url.starts_with("postgresql:") || url.starts_with("postgres:") {
#[cfg(postgresql)] #[cfg(not(mysql))]
return Ok(DbConnType::postgresql); err!("`DATABASE_URL` is a MySQL URL, but the 'mysql' feature is not enabled")
// Postgres
#[cfg(not(postgresql))] } else if url.starts_with("postgresql:") || url.starts_with("postgres:") {
err!("`DATABASE_URL` is a PostgreSQL URL, but the 'postgresql' feature is not enabled") #[cfg(postgresql)]
return Ok(DbConnType::postgresql);
//Sqlite
#[cfg(not(postgresql))]
err!("`DATABASE_URL` is a PostgreSQL URL, but the 'postgresql' feature is not enabled")
// Failed to parse
} else {
err!("`DATABASE_URL` looks like a URI but it could not be parsed, make sure it is valid. Note that ':' in filepaths are disallowed and DATABASE_URL only accepts 'postgresql://', 'postgres://' and 'mysql://' as valid URIs")
}
// SQLite
} else { } else {
#[cfg(sqlite)] #[cfg(sqlite)]
return Ok(DbConnType::sqlite); return Ok(DbConnType::sqlite);

Loading…
Cancel
Save