diff --git a/lib/migrations/20210823_create_table.sql b/lib/migrations/20210823_create_table.sql new file mode 100644 index 0000000..5038398 --- /dev/null +++ b/lib/migrations/20210823_create_table.sql @@ -0,0 +1,29 @@ +-- Projects +CREATE TABLE project ( + id INTEGER NOT NULL PRIMARY KEY, + name TEXT NOT NULL +); +CREATE UNIQUE INDEX project_name ON project (name); + +CREATE TABLE tag ( + id INTEGER NOT NULL PRIMARY KEY, + label TEXT NOT NULL +) +CREATE UNIQUE INDEX tag_label ON tag (label); + +CREATE TABLE entry ( + id INTEGER NOT NULL PRIMARY KEY, + start_ts DATETIME NOT NULL, + stop_ts DATETIME, + project_id INTEGER NOT NULL, + FOREIGN KEY (project_id) REFERENCES project (id) +); + +CREATE TABLE entry_tags ( + id INTEGER NOT NULL PRIMARY KEY, + entry_id INTEGER NOT NULL, + tag_id INTEGER NOT NULL, + CONSTRAINT UNIQUE (entry_id, tag_id), + FOREIGN KEY (entry_id) REFERENCES entry (id), + FOREIGN KEY (tag_id) REFERENCES tag (id) +) diff --git a/lib/src/database.rs b/lib/src/database.rs new file mode 100644 index 0000000..97fcbe7 --- /dev/null +++ b/lib/src/database.rs @@ -0,0 +1,16 @@ +use sqlx::sqlite::Sqlite; +use sqlx::sqlite::SqliteConnectOptions; +use sqlx::sqlite::SqlitePoolOptions; +use sqlx::Pool; + +async fn connect() -> Result, sqlx::Error> { + let pool = SqlitePoolOptions::new() + .connect_with( + SqliteConnectOptions::new() + .filename("testing.sqlite") + .create_if_missing(true), + ) + .await?; + sqlx::migrate("migrations").run(&pool).await?; + Ok(pool) +} diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 31e1bb2..4d0f362 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -1,3 +1,5 @@ +mod database; + #[cfg(test)] mod tests { #[test]