Use SqlitePool instead of SqliteConnection

This commit is contained in:
Elias Schriefer 2021-07-19 15:28:33 +00:00
parent 998b6f93bc
commit 80fc6548d0
2 changed files with 12 additions and 14 deletions

View File

@ -3,7 +3,6 @@ use std::{
boxed::Box,
error::Error,
};
use tokio::sync::RwLock;
use crate::{
cli::Options,
config::Config,
@ -36,10 +35,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
dbg!(&config);
#[cfg(feature = "sqlite")]
let mut sqlite_connection = RwLock::new(sqlite::connect(&config.database_path).await?);
let sqlite_connection = sqlite::connect(&config.database_path).await?;
#[cfg(feature = "sqlite")]
sqlite::load_template(sqlite_connection.get_mut()).await?;
sqlite::load_template(&sqlite_connection).await?;
Ok(())
}

View File

@ -1,23 +1,22 @@
use std::path::PathBuf;
use sqlx::{
prelude::*,
sqlite::{
SqliteConnection,
SqliteConnectOptions,
SqliteQueryResult,
},
use sqlx::sqlite::{
SqlitePool,
SqliteConnectOptions,
SqliteQueryResult,
SqliteSynchronous,
};
/// Prepare sqlite database connection
pub async fn connect(path: &PathBuf) -> sqlx::Result<SqliteConnection> {
pub async fn connect(path: &PathBuf) -> sqlx::Result<SqlitePool> {
let connect_options = SqliteConnectOptions::new()
.filename(&path)
.create_if_missing(true);
SqliteConnection::connect_with(&connect_options).await
.create_if_missing(true)
.synchronous(SqliteSynchronous::Normal);
SqlitePool::connect_with(connect_options).await
}
/// Create tables from template
pub async fn load_template(sqlite_handle: &mut SqliteConnection) -> sqlx::Result<SqliteQueryResult> {
pub async fn load_template(sqlite_handle: &SqlitePool) -> sqlx::Result<SqliteQueryResult> {
sqlx::query(concat!(
include_str!("users.sql"),
include_str!("security_preferences.sql"),