-
Notifications
You must be signed in to change notification settings - Fork 94
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
1d57809
commit 07ec7e9
Showing
6 changed files
with
43 additions
and
34 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
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
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
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,36 +1,39 @@ | ||
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions}; | ||
use sqlx::{Pool, Sqlite}; | ||
use tokio::fs::DirBuilder; | ||
use uuid::Uuid; | ||
#[cfg(test)] | ||
pub mod utils { | ||
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions}; | ||
use sqlx::{Pool, Sqlite}; | ||
use tokio::fs::DirBuilder; | ||
use uuid::Uuid; | ||
|
||
/// Sets up a temporary SQLite database for testing purposes. | ||
pub async fn setup_db(run_migrations: bool) -> Pool<Sqlite> { | ||
let path = std::env::temp_dir().join(Uuid::new_v4().to_string()); | ||
/// Sets up a temporary SQLite database for testing purposes. | ||
pub async fn setup_db(run_migrations: bool) -> Pool<Sqlite> { | ||
let path = std::env::temp_dir().join(Uuid::new_v4().to_string()); | ||
|
||
if let Some(parent) = path.parent() { | ||
if !parent.as_os_str().is_empty() && !parent.exists() { | ||
relay_log::debug!("creating directory for spooling file: {}", parent.display()); | ||
DirBuilder::new() | ||
.recursive(true) | ||
.create(&parent) | ||
.await | ||
.unwrap(); | ||
if let Some(parent) = path.parent() { | ||
if !parent.as_os_str().is_empty() && !parent.exists() { | ||
relay_log::debug!("creating directory for spooling file: {}", parent.display()); | ||
DirBuilder::new() | ||
.recursive(true) | ||
.create(&parent) | ||
.await | ||
.unwrap(); | ||
} | ||
} | ||
} | ||
|
||
let options = SqliteConnectOptions::new() | ||
.filename(&path) | ||
.journal_mode(SqliteJournalMode::Wal) | ||
.create_if_missing(true); | ||
let options = SqliteConnectOptions::new() | ||
.filename(&path) | ||
.journal_mode(SqliteJournalMode::Wal) | ||
.create_if_missing(true); | ||
|
||
let db = SqlitePoolOptions::new() | ||
.connect_with(options) | ||
.await | ||
.unwrap(); | ||
let db = SqlitePoolOptions::new() | ||
.connect_with(options) | ||
.await | ||
.unwrap(); | ||
|
||
if run_migrations { | ||
sqlx::migrate!("../migrations").run(&db).await.unwrap(); | ||
} | ||
if run_migrations { | ||
sqlx::migrate!("../migrations").run(&db).await.unwrap(); | ||
} | ||
|
||
db | ||
db | ||
} | ||
} |