From 9a340a50c7342e5df366e3b3dedfc3fc750a9529 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vilhelm=20Bergs=C3=B8e?= Date: Thu, 27 Oct 2022 13:35:38 +0200 Subject: [PATCH] error if DATABASE_URL can't be parsed: DbConnType.from_url() returns error if it fails to parse DATABASE_URL --- src/db/mod.rs | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/db/mod.rs b/src/db/mod.rs index 09cbd4b0..f4452527 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -190,23 +190,30 @@ generate_connections! { impl DbConnType { pub fn from_url(url: &str) -> Result { - // Mysql - if url.starts_with("mysql:") { - #[cfg(mysql)] - return Ok(DbConnType::mysql); - - #[cfg(not(mysql))] - err!("`DATABASE_URL` is a MySQL URL, but the 'mysql' feature is not enabled") - - // Postgres - } else if url.starts_with("postgresql:") || url.starts_with("postgres:") { - #[cfg(postgresql)] - return Ok(DbConnType::postgresql); - - #[cfg(not(postgresql))] - err!("`DATABASE_URL` is a PostgreSQL URL, but the 'postgresql' feature is not enabled") - - //Sqlite + if url.contains(':') + && !(url.len() >= 2 + && url.chars().next().unwrap().is_ascii_alphabetic() + && url.chars().nth(1).unwrap() == ':') + { + // MySQL + if url.starts_with("mysql:") { + #[cfg(mysql)] + return Ok(DbConnType::mysql); + + #[cfg(not(mysql))] + err!("`DATABASE_URL` is a MySQL URL, but the 'mysql' feature is not enabled") + // Postgres + } else if url.starts_with("postgresql:") || url.starts_with("postgres:") { + #[cfg(postgresql)] + return Ok(DbConnType::postgresql); + + #[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 { #[cfg(sqlite)] return Ok(DbConnType::sqlite);