Ir al contenido

Historial de cambios ​

Traducción automática

Esta página se tradujo automáticamente del inglés y todavía no la revisó un hablante nativo; las correcciones son bienvenidas en GitHub. Si algo no coincide, vale el original en inglés.

Tarea: saber quién cambió una página de una wiki, cuándo, y qué cambió.

history: true en la fuente de datos es toda la configuración:

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.');

Cada escritura que hace una acción se registra en una tabla quantum_history de la misma base de datos, en la misma transacción: cuándo, el usuario de la sesión, la acción, la fila, y la fila antes y después. ui:history lista los cambios de una fila, del más reciente al más antiguo, con cada columna cambiada como 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 inicia la sesión de la prueba; history= verifica lo que se registró. Una escritura rechazada, o revertida, no deja historial:

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

Ver DB-11.

Licencia MIT · Hecho con VitePress