Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2270,7 +2270,11 @@ pub async fn test_connection<R: Runtime>(

// For file-based drivers, verify the database file exists before attempting connection
if drv.manifest().capabilities.file_based {
let db_path = std::path::Path::new(resolved_params.database.primary());
let db_path = if resolved_params.driver == "sqlite" {
crate::sqlite_database::expand_sqlite_filename(resolved_params.database.primary())
} else {
PathBuf::from(resolved_params.database.primary())
};
if !db_path.exists() {
return Err(format!(
"Database file not found: {}",
Expand Down
3 changes: 2 additions & 1 deletion src-tauri/src/pool_manager.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::models::ConnectionParams;
use crate::sqlite_database::expand_sqlite_filename;
use deadpool_postgres::{Hook as PgHook, HookError as PgHookError, Manager as PgPoolManager, Pool as PgPool};
use once_cell::sync::Lazy;
use rustls::client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier};
Expand Down Expand Up @@ -646,7 +647,7 @@ impl ServerCertVerifier for VerifyCaCertVerifier {
}

fn build_sqlite_connectoptions(params: &ConnectionParams) -> SqliteConnectOptions {
SqliteConnectOptions::new().filename(params.database.to_string())
SqliteConnectOptions::new().filename(expand_sqlite_filename(params.database.primary()))
}

/// Return the connection's startup script if it is set and not blank.
Expand Down
36 changes: 36 additions & 0 deletions src-tauri/src/pool_manager_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,42 @@ mod postgres_tls_connector_tests {
}
}

#[cfg(test)]
mod sqlite_path_tests {
use crate::sqlite_database::expand_sqlite_filename_with_home;
use std::path::{Path, PathBuf};

#[test]
fn expands_sqlite_home_prefixes() {
let home = PathBuf::from("/home/dev");

assert_eq!(
expand_sqlite_filename_with_home("~/db.sqlite", Some(&home)),
home.join("db.sqlite")
);
assert_eq!(
expand_sqlite_filename_with_home("~\\db.sqlite", Some(&home)),
home.join("db.sqlite")
);
}

#[test]
fn leaves_non_home_sqlite_paths_unchanged() {
assert_eq!(
expand_sqlite_filename_with_home("relative/db.sqlite", None),
PathBuf::from("relative/db.sqlite")
);
assert_eq!(
expand_sqlite_filename_with_home("~", Some(Path::new("/home/dev"))),
PathBuf::from("~")
);
assert_eq!(
expand_sqlite_filename_with_home("~user/db.sqlite", Some(Path::new("/home/dev"))),
PathBuf::from("~user/db.sqlite")
);
}
}

#[cfg(test)]
mod startup_script_tests {
use crate::models::{ConnectionParams, DatabaseSelection};
Expand Down
18 changes: 17 additions & 1 deletion src-tauri/src/sqlite_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub(crate) fn normalize_sqlite_path(raw_path: &str) -> Result<PathBuf, String> {
return Err("Choose a file name for the SQLite database.".to_string());
}

let mut path = PathBuf::from(trimmed);
let mut path = expand_sqlite_filename(trimmed);
if path.file_name().is_none() {
return Err("Choose a valid SQLite database file name.".to_string());
}
Expand All @@ -36,6 +36,22 @@ pub(crate) fn normalize_sqlite_path(raw_path: &str) -> Result<PathBuf, String> {
Ok(path)
}

pub(crate) fn expand_sqlite_filename(value: &str) -> PathBuf {
let home = directories::BaseDirs::new().map(|dirs| dirs.home_dir().to_path_buf());
expand_sqlite_filename_with_home(value, home.as_deref())
}

pub(crate) fn expand_sqlite_filename_with_home(value: &str, home: Option<&Path>) -> PathBuf {
let home_relative = value
.strip_prefix("~/")
.or_else(|| value.strip_prefix("~\\"));

match (home_relative, home) {
(Some(relative), Some(home)) => home.join(relative),
_ => PathBuf::from(value),
}
}

fn connection_name(path: &Path) -> Result<String, String> {
path.file_stem()
.and_then(|name| name.to_str())
Expand Down
19 changes: 18 additions & 1 deletion src-tauri/src/sqlite_database_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::sqlite_database::{create_sqlite_file, normalize_sqlite_path};
use crate::sqlite_database::{
create_sqlite_file, expand_sqlite_filename_with_home, normalize_sqlite_path,
};
use sqlx::sqlite::SqliteConnectOptions;
use sqlx::{Connection, SqliteConnection};
use std::fs;
use std::path::PathBuf;

#[test]
fn appends_db_extension_when_missing() {
Expand All @@ -10,6 +13,20 @@ fn appends_db_extension_when_missing() {
assert_eq!(path.to_string_lossy(), "/tmp/customer-data.db");
}

#[test]
fn normalizes_home_relative_sqlite_paths() {
let home = PathBuf::from("/home/dev");

assert_eq!(
expand_sqlite_filename_with_home("~/customer-data", Some(&home)),
home.join("customer-data")
);
assert_eq!(
expand_sqlite_filename_with_home("~\\customer-data", Some(&home)),
home.join("customer-data")
);
}

#[test]
fn accepts_supported_extensions_case_insensitively() {
for path in ["data.db", "data.sqlite", "data.sqlite3", "data.SQLITE"] {
Expand Down