1pub mod projects;
2mod schema;
3pub mod tags;
4pub mod tasks;
5pub mod time_entries;
6
7use rusqlite::Connection;
8use schema::*;
9
10pub fn initialize(path: &str) -> Result<Connection, rusqlite::Error> {
11 let conn = Connection::open(path)?;
12
13 conn.execute_batch(&format!(
14 "{CREATE_PROJECTS_TABLE};
15 {CREATE_TASKS_TABLE};
16 {CREATE_TAGS_TABLE};
17 {CREATE_TASK_TAGS_TABLE};
18 {CREATE_TIME_ENTRIES_TABLE};
19 "
20 ))?;
21
22 run_migrations(&conn)?;
23
24 Ok(conn)
25}
26
27fn run_migrations(conn: &Connection) -> Result<(), rusqlite::Error> {
28 let has_branch: bool = conn
30 .prepare("SELECT COUNT(*) FROM pragma_table_info('tasks') WHERE name = 'branch'")?
31 .query_row((), |row| row.get::<_, i64>(0))
32 .map(|count| count > 0)?;
33
34 if !has_branch {
35 conn.execute_batch("ALTER TABLE tasks ADD COLUMN branch TEXT")?;
36 }
37
38 Ok(())
39}