Outsource db pool creation in preparation for unit tests

This commit is contained in:
Lennart
2024-02-25 12:50:19 +01:00
parent 76c879b9f7
commit 509f8f7aeb
3 changed files with 17 additions and 14 deletions

View File

@@ -0,0 +1,17 @@
CREATE TABLE calendars (
id TEXT PRIMARY KEY NOT NULL,
name TEXT NOT NULL,
owner TEXT NOT NULL,
description TEXT,
color TEXT,
timezone TEXT NOT NULL
);
CREATE TABLE events (
uid TEXT NOT NULL,
cid TEXT NOT NULL,
ics TEXT NOT NULL,
PRIMARY KEY (cid, uid),
FOREIGN KEY (cid) REFERENCES calendars(id)
);

View File

@@ -1,6 +1,6 @@
use anyhow::Result;
use async_trait::async_trait;
use sqlx::SqlitePool;
use sqlx::{sqlite::SqliteConnectOptions, Pool, Sqlite, SqlitePool};
use crate::{
calendar::{Calendar, CalendarStore},
@@ -111,3 +111,17 @@ impl CalendarStore for SqliteCalendarStore {
Ok(())
}
}
pub async fn create_db_pool(db_url: &str, migrate: bool) -> anyhow::Result<Pool<Sqlite>>{
let db = SqlitePool::connect_with(
SqliteConnectOptions::new()
.filename(db_url)
.create_if_missing(true),
)
.await?;
if migrate {
println!("Running database migrations");
sqlx::migrate!("./migrations").run(&db).await?;
}
Ok(db)
}