feat: sqlx sqlite create db file if not present

This commit is contained in:
Aravinth Manivannan 2022-05-24 20:30:43 +05:30
parent 450557e3f8
commit 10f33d8ea6
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
2 changed files with 10 additions and 1 deletions

View file

@ -63,10 +63,18 @@ pub mod prelude {
impl Connect for ConnectionOptions { impl Connect for ConnectionOptions {
type Pool = Database; type Pool = Database;
async fn connect(self) -> DBResult<Self::Pool> { async fn connect(self) -> DBResult<Self::Pool> {
use sqlx::sqlite::SqliteConnectOptions;
use std::str::FromStr;
let pool = match self { let pool = match self {
Self::Fresh(fresh) => fresh Self::Fresh(fresh) => fresh
.pool_options .pool_options
.connect(&fresh.url) .connect_with(
SqliteConnectOptions::from_str(&fresh.url)
.unwrap()
.create_if_missing(true)
.read_only(false),
)
.await .await
.map_err(|e| DBError::DBError(Box::new(e)))?, .map_err(|e| DBError::DBError(Box::new(e)))?,
Self::Existing(conn) => conn.0, Self::Existing(conn) => conn.0,

View file

@ -61,6 +61,7 @@ async fn everything_works() {
let pool_options = SqlitePoolOptions::new().max_connections(2); let pool_options = SqlitePoolOptions::new().max_connections(2);
let connection_options = ConnectionOptions::Fresh(Fresh { pool_options, url }); let connection_options = ConnectionOptions::Fresh(Fresh { pool_options, url });
let db = connection_options.connect().await.unwrap(); let db = connection_options.connect().await.unwrap();
db.migrate().await.unwrap();
let add_repo_msg = AddRepository { let add_repo_msg = AddRepository {
html_link: HTML_REPO_URL, html_link: HTML_REPO_URL,