issue: record row changes in journal
authorMischa POSLAWSKY <perl@shiar.org>
Thu, 7 Nov 2019 04:51:30 +0000 (05:51 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 9 Nov 2019 06:08:13 +0000 (07:08 +0100)
widget/comments.sql
widget/reply.php

index 8a0655826101a8147c5e3e26984008285fe60a1f..2a4993b056eb5062d7bfd0e7a10f7301493a394e 100644 (file)
@@ -18,3 +18,12 @@ CREATE TABLE comments (
        author     text,
        id         serial      NOT NULL PRIMARY KEY
 );
+
+CREATE TABLE journal (
+       comment_id integer     NOT NULL REFERENCES comments (id),
+       property   text        NOT NULL DEFAULT 'attr',
+       col        text        NOT NULL,
+       old_value  text,
+       value      text,
+       id         serial      NOT NULL PRIMARY KEY
+);
index 328a58fbe1a23f2cb8ba09d1b06f3a5480acd691..ab04137493b770a94e3fb5f59c948bac1d9aec64 100644 (file)
@@ -16,9 +16,10 @@ if ($_POST) {
                if (!$query->rowCount()) {
                        throw new Exception('Fout bij opslaan');
                }
+               $newcomment = $Db->dbh->lastInsertId('comments_id_seq');
 
                if (isset($Issue)) {
-                       $row = ['updated' => ['now()']];
+                       $row = [];
                        foreach (['assign'] as $col) {
                                if (!isset($_POST[$col])) continue;
                                $row[$col] = $_POST[$col] ?: NULL;
@@ -29,7 +30,22 @@ if ($_POST) {
                                        $row['closed'] = $reset ? ['now()'] : NULL;
                                }
                        }
-                       $Db->set('issues', $row, ['id = ?', $Issue->id]);
+                       $derived = ['updated' => ['now()']];
+                       $filter = ['id = ? RETURNING *', $Issue->id];
+                       $subquery = $Db->set('issues', $row + $derived, $filter);
+
+                       if ($updated = $subquery->fetch()) {
+                               foreach (array_keys($row) as $col) {
+                                       if ($updated->$col === $Issue->$col) continue; # unaltered
+                                       $Db->set('journal', [
+                                               'comment_id' => $newcomment,
+                                               'property'   => 'attr',
+                                               'col'        => $col,
+                                               'old_value'  => $Issue->$col,
+                                               'value'      => $updated->$col,
+                                       ]);
+                               }
+                       }
                }
                $_POST['reply'] = NULL;
        }