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. 15
      src/db/mod.rs

15
src/db/mod.rs

@ -190,14 +190,18 @@ 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(':')
&& !(url.len() >= 2
&& url.chars().next().unwrap().is_ascii_alphabetic()
&& url.chars().nth(1).unwrap() == ':')
{
// MySQL
if url.starts_with("mysql:") { if url.starts_with("mysql:") {
#[cfg(mysql)] #[cfg(mysql)]
return Ok(DbConnType::mysql); return Ok(DbConnType::mysql);
#[cfg(not(mysql))] #[cfg(not(mysql))]
err!("`DATABASE_URL` is a MySQL URL, but the 'mysql' feature is not enabled") err!("`DATABASE_URL` is a MySQL URL, but the 'mysql' feature is not enabled")
// Postgres // Postgres
} else if url.starts_with("postgresql:") || url.starts_with("postgres:") { } else if url.starts_with("postgresql:") || url.starts_with("postgres:") {
#[cfg(postgresql)] #[cfg(postgresql)]
@ -205,8 +209,11 @@ impl DbConnType {
#[cfg(not(postgresql))] #[cfg(not(postgresql))]
err!("`DATABASE_URL` is a PostgreSQL URL, but the 'postgresql' feature is not enabled") err!("`DATABASE_URL` is a PostgreSQL URL, but the 'postgresql' feature is not enabled")
// Failed to parse
//Sqlite } 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