From 10f33d8ea661846623ba824f9a8f485fc832d32b Mon Sep 17 00:00:00 2001 From: realaravinth Date: Tue, 24 May 2022 20:30:43 +0530 Subject: [PATCH] feat: sqlx sqlite create db file if not present --- db/db-sqlx-sqlite/src/lib.rs | 10 +++++++++- db/db-sqlx-sqlite/src/tests.rs | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/db/db-sqlx-sqlite/src/lib.rs b/db/db-sqlx-sqlite/src/lib.rs index 6819626..c705cc9 100644 --- a/db/db-sqlx-sqlite/src/lib.rs +++ b/db/db-sqlx-sqlite/src/lib.rs @@ -63,10 +63,18 @@ pub mod prelude { impl Connect for ConnectionOptions { type Pool = Database; async fn connect(self) -> DBResult { + use sqlx::sqlite::SqliteConnectOptions; + use std::str::FromStr; + let pool = match self { Self::Fresh(fresh) => fresh .pool_options - .connect(&fresh.url) + .connect_with( + SqliteConnectOptions::from_str(&fresh.url) + .unwrap() + .create_if_missing(true) + .read_only(false), + ) .await .map_err(|e| DBError::DBError(Box::new(e)))?, Self::Existing(conn) => conn.0, diff --git a/db/db-sqlx-sqlite/src/tests.rs b/db/db-sqlx-sqlite/src/tests.rs index 8c623d0..435c74c 100644 --- a/db/db-sqlx-sqlite/src/tests.rs +++ b/db/db-sqlx-sqlite/src/tests.rs @@ -61,6 +61,7 @@ async fn everything_works() { let pool_options = SqlitePoolOptions::new().max_connections(2); let connection_options = ConnectionOptions::Fresh(Fresh { pool_options, url }); let db = connection_options.connect().await.unwrap(); + db.migrate().await.unwrap(); let add_repo_msg = AddRepository { html_link: HTML_REPO_URL,