Browse Source

Database definitions

main
Julio Biason 3 years ago
parent
commit
c340556756
  1. 29
      lib/migrations/20210823_create_table.sql
  2. 16
      lib/src/database.rs
  3. 2
      lib/src/lib.rs

29
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)
)

16
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<Pool<Sqlite>, 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)
}

2
lib/src/lib.rs

@ -1,3 +1,5 @@
mod database;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]

Loading…
Cancel
Save