-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
88f89f1
commit a3b135d
Showing
5 changed files
with
91 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# .gitignore | ||
/target | ||
.env | ||
.env.test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// src/db_utils.rs | ||
|
||
use dotenvy::dotenv; | ||
use sqlx::MySqlPool; | ||
use std::process::Command; | ||
use url::Url; | ||
|
||
pub async fn initialize_database(database_url: &str) -> Result<(), String> { | ||
dotenv().ok(); | ||
|
||
let parsed_url = Url::parse(database_url).map_err(|e| format!("Invalid DATABASE_URL: {}", e))?; | ||
|
||
let username: &str = parsed_url.username(); | ||
let password: &str = parsed_url.password().ok_or("Password not found in DATABASE_URL")?; | ||
let host: &str = parsed_url | ||
.host_str() | ||
.ok_or("Host not found in DATABASE_URL")?; | ||
let database_name = parsed_url.path().trim_start_matches('/'); | ||
if database_name.is_empty() { | ||
return Err("Database name not found in DATABASE_URL".into()); | ||
} | ||
|
||
// Commands to configure the database | ||
let commands = [ | ||
&format!("sudo mariadb -e \"CREATE DATABASE IF NOT EXISTS {database_name}\""), | ||
&format!( | ||
"sudo mariadb -e \"DROP USER IF EXISTS '{username}'@'{host}'\"" | ||
), | ||
&format!( | ||
"sudo mariadb -e \"CREATE USER IF NOT EXISTS '{username}'@'{host}' IDENTIFIED BY '{password}'\"" | ||
), | ||
&format!( | ||
"sudo mariadb -e \"GRANT ALL PRIVILEGES ON {database_name}.* TO '{username}'@'{host}'\"" | ||
), | ||
"sudo mariadb -e \"FLUSH PRIVILEGES\"", | ||
]; | ||
|
||
for cmd in &commands { | ||
let output = Command::new("sh") | ||
.arg("-c") | ||
.arg(cmd) | ||
.output() | ||
.map_err(|e| format!("Failed to execute command: {}. Error: {}", cmd, e))?; | ||
|
||
if !output.status.success() { | ||
return Err(format!( | ||
"Command failed: {}. Stderr: {}", | ||
cmd, | ||
String::from_utf8_lossy(&output.stderr) | ||
)); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
|
||
pub async fn verify_database_connection(pool: &MySqlPool) -> Result<(), String> { | ||
sqlx::query("SELECT 1") | ||
.fetch_one(pool) | ||
.await | ||
.map_err(|e| format!("Database connection test failed: {}", e))?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters