From 1459ed8814e7163d03fadc6ebc5abf6adf20f93d Mon Sep 17 00:00:00 2001 From: "Florine W. Dekker" Date: Wed, 7 Dec 2022 20:00:07 +0100 Subject: [PATCH] Add tracking start date --- composer.json | 2 +- composer.lock | Bin 77292 -> 77292 bytes package-lock.json | Bin 226174 -> 226174 bytes package.json | 2 +- src/main/index.html | 1 + src/main/js/Main.ts | 4 ++ .../com/fwdekker/deathnotifier/Database.php | 39 +++++++++++++++++- .../deathnotifier/tracking/TrackingList.php | 4 +- 8 files changed, 47 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 497f71b..d2bdc31 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "fwdekker/death-notifier", "description": "Get notified when a famous person dies.", - "version": "0.17.4", "_comment_version": "Also update version in `package.json`!", + "version": "0.18.0", "_comment_version": "Also update version in `package.json`!", "type": "project", "license": "MIT", "homepage": "https://git.fwdekker.com/tools/death-notifier", diff --git a/composer.lock b/composer.lock index d700ae5ceab4c89b76bbfd958eb5948ef7ce93b2..68f772691663f28e86fbbdd6c059683c568e82c0 100644 GIT binary patch delta 51 zcmaEJo8`@ImJPy;3aJ)prsgT;21bVFsi~=nmIkRw7KW*5X{iRO7G`E9=FP^8+l?6+ Hcj^ED(OD2W delta 51 zcmaEJo8`@ImJPy;3dWYn#wnI2$;nA3hKZ&oDaom3hRKF0i58a0sitO0md(bD+l?6+ Hcj^ED(OM8# diff --git a/package-lock.json b/package-lock.json index 75e31f28d31823607e8ef27755ed720412b8fc19..a69945177aaf585c53c75fc9280219e790b3b4f6 100644 GIT binary patch delta 36 qcmezOjQ8I&-U-Ic7J3E~&8C2;$qRXf8y~bjU~GNBwDke=ydnS{I}cp| delta 36 qcmezOjQ8I&-U-Ic=6WU*&8C2;$qRXf8y~bjU~GNBwDke=ydnS{xDR6h diff --git a/package.json b/package.json index e11292c..bb6b198 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "death-notifier", - "version": "0.17.4", "_comment_version": "Also update version in `composer.json`!", + "version": "0.18.0", "_comment_version": "Also update version in `composer.json`!", "description": "Get notified when a famous person dies.", "author": "Florine W. Dekker", "browser": "dist/bundle.js", diff --git a/src/main/index.html b/src/main/index.html index ecd63ac..54799f7 100644 --- a/src/main/index.html +++ b/src/main/index.html @@ -233,6 +233,7 @@ Name Status + Added diff --git a/src/main/js/Main.ts b/src/main/js/Main.ts index e4508d1..b1a86b1 100644 --- a/src/main/js/Main.ts +++ b/src/main/js/Main.ts @@ -65,6 +65,10 @@ function refreshTrackings(): void { statusCell.innerText = statusText; row.append(statusCell); + const sinceCell = document.createElement("td"); + sinceCell.innerText = (new Date(tracking.since * 1000)).toLocaleDateString(); + row.append(sinceCell); + const deleteCell = document.createElement("td"); const deleteForm = document.createElement("form"); deleteForm.addEventListener("submit", (event: SubmitEvent) => { diff --git a/src/main/php/com/fwdekker/deathnotifier/Database.php b/src/main/php/com/fwdekker/deathnotifier/Database.php index 827fc32..307e6b2 100644 --- a/src/main/php/com/fwdekker/deathnotifier/Database.php +++ b/src/main/php/com/fwdekker/deathnotifier/Database.php @@ -108,6 +108,7 @@ class Database if (Comparator::lessThan($db_version, "0.8.0")) self::migrate_0_8_0(); if (Comparator::lessThan($db_version, "0.10.0")) self::migrate_0_10_0(); if (Comparator::lessThan($db_version, "0.16.0")) self::migrate_0_16_0(); + if (Comparator::lessThan($db_version, "0.18.0")) self::migrate_0_18_0(); // Update version $stmt = $this->conn->prepare("UPDATE meta SET v=:version WHERE k='version';"); @@ -148,7 +149,8 @@ class Database PRIMARY KEY (type, recipient, arg1));"); $this->conn->exec("INSERT INTO new_email_tasks (type, recipient, arg1) SELECT type, arg1, arg2 - FROM email_tasks WHERE arg1 NOT NULL;"); + FROM email_tasks + WHERE arg1 NOT NULL;"); $this->conn->exec("DROP TABLE email_tasks;"); $this->conn->exec("ALTER TABLE new_email_tasks RENAME TO email_tasks;"); } @@ -196,6 +198,41 @@ class Database PRIMARY KEY (type_key, recipient));"); } + /** + * Migrates the database from a previous version to one compatible with v0.18.0. + * + * @return void + * @noinspection SqlResolve Function necessarily refers to old schema which is not detected by tools + */ + private function migrate_0_18_0(): void + { + $this->logger->notice("Migrating to v0.18.0."); + + $this->conn->exec("CREATE TABLE new_trackings(user_uuid TEXT NOT NULL, + person_name TEXT NOT NULL, + since INT NOT NULL DEFAULT(unixepoch()), + PRIMARY KEY (user_uuid, person_name), + FOREIGN KEY (user_uuid) REFERENCES users (uuid) + ON DELETE CASCADE + ON UPDATE CASCADE, + FOREIGN KEY (person_name) REFERENCES people (name) + ON DELETE CASCADE + ON UPDATE CASCADE);"); + $this->conn->exec("INSERT INTO new_trackings (user_uuid, person_name) + SELECT user_uuid, person_name + FROM trackings;"); + $this->conn->exec("DROP TRIGGER people_cull_orphans;"); + $this->conn->exec("DROP TABLE trackings;"); + $this->conn->exec("ALTER TABLE new_trackings RENAME TO trackings;"); + $this->conn->exec("CREATE TRIGGER people_cull_orphans + AFTER DELETE ON trackings + FOR EACH ROW + WHEN (SELECT COUNT(*) FROM trackings WHERE person_name=OLD.person_name)=0 + BEGIN + DELETE FROM people WHERE name=OLD.person_name; + END;"); + } + /** * Executes {@see $lambda} within a transaction, allowing nesting. diff --git a/src/main/php/com/fwdekker/deathnotifier/tracking/TrackingList.php b/src/main/php/com/fwdekker/deathnotifier/tracking/TrackingList.php index 5bce8c8..0b76c5b 100644 --- a/src/main/php/com/fwdekker/deathnotifier/tracking/TrackingList.php +++ b/src/main/php/com/fwdekker/deathnotifier/tracking/TrackingList.php @@ -51,6 +51,7 @@ class TrackingList $conn = $this->database->conn; $conn->exec("CREATE TABLE trackings(user_uuid TEXT NOT NULL, person_name TEXT NOT NULL, + since INT NOT NULL DEFAULT(unixepoch()), PRIMARY KEY (user_uuid, person_name), FOREIGN KEY (user_uuid) REFERENCES users (uuid) ON DELETE CASCADE @@ -58,7 +59,6 @@ class TrackingList FOREIGN KEY (person_name) REFERENCES people (name) ON DELETE CASCADE ON UPDATE CASCADE);"); - // TODO: Add column for date since tracking $conn->exec("CREATE TABLE people(name TEXT NOT NULL UNIQUE PRIMARY KEY, status TEXT NOT NULL DEFAULT(''), is_deleted INT NOT NULL DEFAULT(0));"); @@ -156,7 +156,7 @@ class TrackingList */ public function list_trackings(string $user_uuid): array { - $stmt = $this->database->conn->prepare("SELECT people.name, people.status, people.is_deleted + $stmt = $this->database->conn->prepare("SELECT people.name, people.status, people.is_deleted, since FROM trackings INNER JOIN people ON trackings.user_uuid=:user_uuid