From eb9e317e6fa1431b7121800725c1de1f1e2aca3f Mon Sep 17 00:00:00 2001 From: Lennart <18233294+lennart-k@users.noreply.github.com> Date: Sun, 8 Oct 2023 15:08:12 +0200 Subject: [PATCH] sqlite: create if not exists and run migrations --- migrations/20231006125208_init.sql | 16 ++++++++++++++++ src/main.rs | 9 ++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 migrations/20231006125208_init.sql diff --git a/migrations/20231006125208_init.sql b/migrations/20231006125208_init.sql new file mode 100644 index 0000000..7d780a4 --- /dev/null +++ b/migrations/20231006125208_init.sql @@ -0,0 +1,16 @@ +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, + PRIMARY KEY (cid, uid), + FOREIGN KEY (cid) REFERENCES calendars(uid) +); + diff --git a/src/main.rs b/src/main.rs index 43a28a7..9884ba7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ use rustical_caldav::{configure_dav, configure_well_known}; use rustical_store::calendar::CalendarStore; use rustical_store::sqlite_store::SqliteCalendarStore; use rustical_store::toml_store::TomlCalendarStore; +use sqlx::sqlite::SqliteConnectOptions; use sqlx::SqlitePool; use std::fs; use std::sync::Arc; @@ -39,7 +40,13 @@ async fn main() -> Result<()> { })) } CalendarStoreConfig::Sqlite(SqliteCalendarStoreConfig { db_url }) => { - let db = SqlitePool::connect(db_url).await?; + let db = SqlitePool::connect_with( + SqliteConnectOptions::new() + .filename(db_url) + .create_if_missing(true), + ) + .await?; + sqlx::migrate!("./migrations").run(&db).await?; Arc::new(RwLock::new(SqliteCalendarStore::new(Arc::new(db)))) } };