Pular para o conteúdo

Histórico de mudanças ​

Tradução automática

Esta página foi traduzida automaticamente do inglês e ainda não foi revisada por um falante nativo; correções são bem-vindas no GitHub. Se algo não bater, vale o original em inglês. O código e os resultados são os mesmos do original, importados dos arquivos testados.

Tarefa: saber quem mudou uma página de um wiki, quando, e o que mudou.

history: true na fonte de dados é toda a configuração:

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 escrita que uma ação faz é registrada numa tabela quantum_history do mesmo banco, na mesma transação: quando, o usuário da sessão, a ação, a linha, e a linha antes e depois. ui:history lista as mudanças de uma linha, as mais novas primeiro, com cada coluna alterada como antigo → novo.

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 faz o teste entrar; history= confere o que foi registrado. Uma escrita recusada, ou desfeita, não deixa histórico:

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

Veja DB-11.

Licença MIT · Feito com VitePress