Trigger for created/updated date
This commit is contained in:
@@ -3,16 +3,32 @@
|
|||||||
-- Schema for pit in python / SQLite mode
|
-- Schema for pit in python / SQLite mode
|
||||||
-- v1
|
-- v1
|
||||||
|
|
||||||
|
--------------------------------
|
||||||
|
-- Project Table with trigger --
|
||||||
|
--------------------------------
|
||||||
CREATE TABLE project (
|
CREATE TABLE project (
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
username text, -- User the project belongs to.
|
username text, -- User the project belongs to.
|
||||||
name text, -- Project name.
|
name text, -- Project name.
|
||||||
status text, -- Project status.
|
status text, -- Project status.
|
||||||
--number_of_tasks integer, -- Number of tasks for the project.
|
|
||||||
created_at date, -- When the project was created?
|
created_at date, -- When the project was created?
|
||||||
updatet_at date -- When the project was last updated?
|
updated_at date -- When the project was last updated?
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE TRIGGER project_insert_created_at AFTER INSERT ON project
|
||||||
|
BEGIN
|
||||||
|
UPDATE project SET created_at = strftime('%Y-%m-%d %H:%M:%S.%s', 'now', 'localtime') WHERE id = new.id;
|
||||||
|
END;
|
||||||
|
|
||||||
|
CREATE TRIGGER project_update_updated_at AFTER UPDATE ON project
|
||||||
|
BEGIN
|
||||||
|
UPDATE project SET updated_at = strftime('%Y-%m-%d %H:%M:%S.%s', 'now', 'localtime') WHERE id = new.id;
|
||||||
|
END;
|
||||||
|
--------------------------------
|
||||||
|
|
||||||
|
--------------------------------
|
||||||
|
-- Task Table with trigger --
|
||||||
|
--------------------------------
|
||||||
CREATE TABLE task (
|
CREATE TABLE task (
|
||||||
id integer primary key, -- TODO When delete, counter go down ==> problem?? Use autoincrement?
|
id integer primary key, -- TODO When delete, counter go down ==> problem?? Use autoincrement?
|
||||||
project_id integer, -- Which project the task belongs to?
|
project_id integer, -- Which project the task belongs to?
|
||||||
@@ -22,21 +38,24 @@ CREATE TABLE task (
|
|||||||
priority text, -- Task priority.
|
priority text, -- Task priority.
|
||||||
date date, -- Generic date/time, ex: task deadline.
|
date date, -- Generic date/time, ex: task deadline.
|
||||||
time date, -- Generic time, ex: time spent on the task.
|
time date, -- Generic time, ex: time spent on the task.
|
||||||
-- number_of_notes integer, -- Number of notes for the task.
|
|
||||||
created_at date, -- When the task was created?
|
created_at date, -- When the task was created?
|
||||||
updated_at date -- When the task was last updated?
|
updated_at date -- When the task was last updated?
|
||||||
);
|
);
|
||||||
|
|
||||||
-- CREATE TRIGGER insert_task AFTER INSERT ON task
|
CREATE TRIGGER task_insert_created_at AFTER INSERT ON task
|
||||||
-- BEGIN
|
BEGIN
|
||||||
-- UPDATE project SET number_of_tasks = (SELECT count(*) FROM task WHERE project_id = new.project_id) WHERE id = new.project_id;
|
UPDATE task SET created_at = strftime('%Y-%m-%d %H:%M:%S.%s', 'now', 'localtime') WHERE id = new.id;
|
||||||
-- END;
|
END;
|
||||||
|
|
||||||
-- CREATE TRIGGER delete_task AFTER DELETE ON task
|
CREATE TRIGGER task_update_updated_at AFTER UPDATE ON task
|
||||||
-- BEGIN
|
BEGIN
|
||||||
-- UPDATE project SET number_of_tasks = (SELECT count(*) FROM task WHERE project_id = old.project_id) WHERE id = old.project_id;
|
UPDATE task SET updated_at = strftime('%Y-%m-%d %H:%M:%S.%s', 'now', 'localtime') WHERE id = new.id;
|
||||||
-- END;
|
END;
|
||||||
|
--------------------------------
|
||||||
|
|
||||||
|
--------------------------------
|
||||||
|
-- Note Table with trigger --
|
||||||
|
--------------------------------
|
||||||
CREATE TABLE note (
|
CREATE TABLE note (
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
project_id integer, -- Project the note belongs to (0 if belongs to task).
|
project_id integer, -- Project the note belongs to (0 if belongs to task).
|
||||||
@@ -47,6 +66,20 @@ CREATE TABLE note (
|
|||||||
updated_at date -- When the note was last updated?
|
updated_at date -- When the note was last updated?
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE TRIGGER note_insert_created_at AFTER INSERT ON note
|
||||||
|
BEGIN
|
||||||
|
UPDATE note SET created_at = strftime('%Y-%m-%d %H:%M:%S.%s', 'now', 'localtime') WHERE id = new.id;
|
||||||
|
END;
|
||||||
|
|
||||||
|
CREATE TRIGGER note_update_updated_at AFTER UPDATE ON note
|
||||||
|
BEGIN
|
||||||
|
UPDATE note SET updated_at = strftime('%Y-%m-%d %H:%M:%S.%s', 'now', 'localtime') WHERE id = new.id;
|
||||||
|
END;
|
||||||
|
--------------------------------
|
||||||
|
|
||||||
|
--------------------------------
|
||||||
|
-- Action Table with trigger --
|
||||||
|
--------------------------------
|
||||||
CREATE TABLE action (
|
CREATE TABLE action (
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
project_id integer, -- Project id (always set).
|
project_id integer, -- Project id (always set).
|
||||||
@@ -62,7 +95,11 @@ CREATE TRIGGER action_insert_created_at AFTER INSERT ON action
|
|||||||
BEGIN
|
BEGIN
|
||||||
UPDATE action SET created_at = strftime('%Y-%m-%d %H:%M:%S.%s', 'now', 'localtime') WHERE id = new.id;
|
UPDATE action SET created_at = strftime('%Y-%m-%d %H:%M:%S.%s', 'now', 'localtime') WHERE id = new.id;
|
||||||
END;
|
END;
|
||||||
|
--------------------------------
|
||||||
|
|
||||||
|
--------------------------------
|
||||||
|
-- Current table with trigger --
|
||||||
|
--------------------------------
|
||||||
CREATE TABLE current (
|
CREATE TABLE current (
|
||||||
id integer,
|
id integer,
|
||||||
project_id integer,
|
project_id integer,
|
||||||
@@ -71,6 +108,7 @@ CREATE TABLE current (
|
|||||||
);
|
);
|
||||||
|
|
||||||
INSERT INTO current VALUES (0,0,0,0);
|
INSERT INTO current VALUES (0,0,0,0);
|
||||||
|
|
||||||
-- Trigger after insert for current values
|
-- Trigger after insert for current values
|
||||||
CREATE TRIGGER current_project AFTER INSERT ON project
|
CREATE TRIGGER current_project AFTER INSERT ON project
|
||||||
BEGIN
|
BEGIN
|
||||||
@@ -102,5 +140,5 @@ CREATE TRIGGER update_note AFTER UPDATE ON note
|
|||||||
BEGIN
|
BEGIN
|
||||||
UPDATE current SET note_id = new.id WHERE id = 0;
|
UPDATE current SET note_id = new.id WHERE id = 0;
|
||||||
END;
|
END;
|
||||||
|
--------------------------------
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user