mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 22:52:22 +00:00
Outsource db pool creation in preparation for unit tests
This commit is contained in:
17
crates/store/migrations/20231006125208_init.sql
Normal file
17
crates/store/migrations/20231006125208_init.sql
Normal 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)
|
||||
);
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user