Skip to content

Change history ​

Task: know who changed a page of a wiki, when, and what they changed.

history: true on the datasource is the whole setup:

yaml
# Recipe: who changed what, and when — recorded by every action's writes.
paths:
  components: ./components
  migrations: ./migrations

datasources:
  db:
    driver: sqlite
    database: ./data/wiki.db
    history: true
sql
CREATE TABLE pages (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title TEXT NOT NULL,
    body TEXT NOT NULL DEFAULT ''
);

INSERT INTO pages (title, body) VALUES ('Welcome', 'First draft.');

Every write an action makes is recorded in a quantum_history table of the same database, in the same transaction: when, the session's user, the action, the row, and the row before and after. ui:history lists a row's changes, newest first, with each changed column as old → new.

xml
<q:component name="Wiki">
  <!-- history: true on the datasource: each write below is recorded with
       the session's user, the action, and the row before and after. -->
  <q:action name="edit" method="POST">
    <q:param name="title" required="true" minlength="3" />
    <q:param name="body" default="" />
    <q:query name="saved" datasource="db">
      UPDATE pages SET title = :title, body = :body WHERE id = 1
      <q:param name="title" value="{title}" type="string" />
      <q:param name="body" value="{body}" type="string" />
    </q:query>
    <q:redirect url="/" flash="Saved." />
  </q:action>

  <q:query name="page" datasource="db">SELECT id, title, body FROM pages WHERE id = 1</q:query>

  <ui:window title="{page.title}">
    <ui:text>{page.body}</ui:text>
    <ui:form on-submit="edit" values="{page}" submit="Save" />
    <!-- Newest first: when, who, which action, and each changed column. -->
    <ui:history table="pages" key="{page.id}" datasource="db" />
  </ui:window>
</q:component>

test:as signs the test in; history= checks what was recorded. A write that is refused, or rolled back, leaves no history:

xml
<q:test name="an edit is recorded with who made it" page="/">
  <test:as user="ana" />
  <test:submit action="edit" title="Welcome!" body="Second draft." />
  <test:expect redirect="/" flash="Saved." />
  <test:expect history="pages" action="edit" op="update" user="ana" count="1" />
  <test:expect text="title: Welcome → Welcome!" />
  <test:expect text="body: First draft. → Second draft." />
</q:test>

<q:test name="a refused edit leaves no history" page="/">
  <test:as user="ana" />
  <test:submit action="edit" title="W" />
  <test:expect error="title" />
  <test:expect history="pages" count="0" />
</q:test>
text
tests/history.test.q
  PASS  an edit is recorded with who made it
  PASS  a refused edit leaves no history
2 passed, 0 failed

See DB-11.

MIT Licensed · Built with VitePress