Added active_id increment in schema

This commit is contained in:
xavi 2024-09-08 16:33:54 -07:00
parent 586786ff6c
commit 09492950b8
2 changed files with 16 additions and 5 deletions

View File

@ -245,7 +245,7 @@ int update_task_status(sqlite3 *db, int argc, char** argv){
return 0;
}
checksqlerr(rc, "broken in add_new_task");
checksqlerr(rc, "broken in update_task_status");
return 1;
}
@ -265,7 +265,7 @@ int del_task(sqlite3 *db, int argc, char** argv){
return 0;
}
checksqlerr(rc, "broken in add_new_task");
checksqlerr(rc, "broken in del_task");
return 1;
}
@ -275,8 +275,8 @@ int del_task(sqlite3 *db, int argc, char** argv){
int add_new_task(sqlite3 *db, int argc, char** argv){
int rc = 0;
char* table = "tasks";
char* title;
char* due_date;
char* title = NULL;
char* due_date = NULL;
char* colnames = "(title, due_date)";
char values[100];
sqlite3_stmt* out_stmt;

View File

@ -1,6 +1,6 @@
CREATE TABLE tasks(
task_id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
title TEXT NOT NULL UNIQUE,
active_id INTEGER UNIQUE,
status TEXT NOT NULL DEFAULT 'backlog',
creation_date DATE DEFAULT (date('now')),
@ -11,6 +11,17 @@ CREATE TABLE tasks(
)
);
-- Basically when we add a new task the active id is set
-- but when a task is done we want it to be able to be null so we need to increment
-- COALESCE is a cool function that returns the first non-null argument which takes care of empty
-- active task list
CREATE TRIGGER autoinc_active_id
AFTER INSERT ON tasks
BEGIN
UPDATE tasks SET active_id = (SELECT COALESCE((SELECT MAX(active_id) FROM tasks) + 1, 1)) WHERE task_id=NEW.task_id;
END;
CREATE TABLE entries(
entry_id INTEGER PRIMARY KEY,
task_id INTEGER NOT NULL REFERENCES tasks(task_id),