Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## [Unreleased]

* [PR-14](https://github.com/ITK-Leantime/data-api/pull/14)
* Added triggers to keep timesheets modified up to date on insert and update.

## [0.1.2] - 2026-03-06

* [PR-12](https://github.com/ITK-Leantime/data-api/pull/12)
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ During installation the following tables will be created to tracked deleted enti

3 triggers will also be installed that populate the tables when entities are deleted.

Two further triggers are installed on `zp_timesheets` that set `modified` to the current
time on insert and update. This ensures timesheet changes are picked up by sync even when
they are made through paths that do not maintain `modified` themselves.

NB! The triggers are removed on uninstall, but the tables are left alone to avoid data loss through install/uninstalls.

## Endpoints
Expand Down
28 changes: 22 additions & 6 deletions Services/APIData.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,43 @@ public function install(): void
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TRIGGER itk_projects_deleted_trigger
CREATE OR REPLACE TRIGGER itk_projects_deleted_trigger
AFTER DELETE ON zp_projects
FOR EACH ROW
BEGIN
INSERT INTO itk_projects_deleted(entryId)
VALUES (OLD.id);
END;

CREATE TRIGGER itk_tickets_deleted_trigger
CREATE OR REPLACE TRIGGER itk_tickets_deleted_trigger
AFTER DELETE ON zp_tickets
FOR EACH ROW
BEGIN
INSERT INTO itk_tickets_deleted(entryId, type)
VALUES (OLD.id, OLD.type);
END;

CREATE TRIGGER itk_timesheets_deleted_trigger
CREATE OR REPLACE TRIGGER itk_timesheets_deleted_trigger
AFTER DELETE ON zp_timesheets
FOR EACH ROW
BEGIN
INSERT INTO itk_timesheets_deleted(entryId)
VALUES (OLD.id);
END;

CREATE OR REPLACE TRIGGER itk_timesheets_modified_insert_trigger
BEFORE INSERT ON zp_timesheets
FOR EACH ROW
BEGIN
SET NEW.modified = NOW();
END;

CREATE OR REPLACE TRIGGER itk_timesheets_modified_update_trigger
BEFORE UPDATE ON zp_timesheets
FOR EACH ROW
BEGIN
SET NEW.modified = NOW();
END;
";

// Use PDO for multi-statement SQL with parameter binding
Expand All @@ -91,9 +105,11 @@ public function install(): void
public function uninstall(): void
{
$sql = "
DROP TRIGGER itk_projects_deleted_trigger;
DROP TRIGGER itk_tickets_deleted_trigger;
DROP TRIGGER itk_timesheets_deleted_trigger;
DROP TRIGGER IF EXISTS itk_projects_deleted_trigger;
DROP TRIGGER IF EXISTS itk_tickets_deleted_trigger;
DROP TRIGGER IF EXISTS itk_timesheets_deleted_trigger;
DROP TRIGGER IF EXISTS itk_timesheets_modified_insert_trigger;
DROP TRIGGER IF EXISTS itk_timesheets_modified_update_trigger;
";

// Tables are not remove, to preserve data through install/uninstalls.
Expand Down
Loading