feat(events): schema, CRUD and the core action registry (Phase 1)
EVENTS_PLAN.md Phase 1. Six of the nine core tables — the ones that do not
depend on the module contract — plus definitions CRUD, publish, archive, and
the action registry with core as its first registrant.
**Nothing dispatches.** There is no runner until Phase 2, so a run row is
created and stays `scheduled`. That is this phase's correct answer and the
surface renders it verbatim rather than hiding it.
Schema (`db/schema.sql`, append-only):
event_series, event_definitions, event_versions, event_runs,
event_run_steps, event_run_log. The four that need a writer —
event_action_settings, event_run_budget, event_run_resources,
event_run_participants — arrive with the phases that give them one.
Registry (`modules/registries.js` + `config/coreEventActions.js`):
registerEventActions staging and commit, with its own id namespace, the
closed risk and reversibility sets, revert() required iff and only iff
reversible: 'ledger', a bounded budgetMs and a param shape whose every
entry needs a type and an example. perform/revert/cost are stripped from
everything the catalog serves. Core declares core.announce, core.wait and
core.cue through the same staging area a module will use.
It is reachable ONLY by registerCore(): loader.js builds its own api facade
and has no method that delegates here, so no module can call it and
MODULE_API_VERSION is untouched. Phase 7 adds the facade and the bump.
Surface (13 routes under /api/v1/admin/events):
Reads staff-wide; publish, archive and run creation admin-only from this
phase per EVENTS.md §N2, even though the switchboard they will consult does
not exist yet — a button that is admin-only later and open now is a gate
nobody notices was missing. The live run controls and `verify` are absent
rather than stubbed, because nothing is in flight yet.
Four things the build settled, all recorded in docs:
- event_definitions gained a `spec` column. A draft's working copy cannot
be an event_versions row: that table is immutable and a run pins one.
- The spec validator must accept its own output. It added `actionVersion`
and `dormant` and then refused them as unknown keys, which would have made
the second save of any definition — and publish's re-validation —
impossible. A test caught it; both are now accepted and recomputed.
- A param's `example` is required, optional params included, matching
registerEventTriggers. It is the authoring form's placeholder.
- Two routes the §API-surface table did not name: GET /admin/events/:id and
GET /admin/events/series.
Core's three perform() bodies answer { ok: false, retry: false } rather than
{ ok: true }: `ok: true` on an action that did nothing is a recorded world
change that did not occur, which is the exact mistake §F's failure default
exists to prevent.
`conditions.checkLiteral` is exported and reused for step-param type checking
— one switch over the six types, so "is this a datetime" has one answer.
Verified: 44 new tests, whole server suite, `npm run check:modules`, routes
manifest and swagger regenerated (the manifest diff is +13 routes, zero moved).
Docs: RunicGateway/docs#209
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
54
server/src/model/events/eventVersions.db.js
Normal file
54
server/src/model/events/eventVersions.db.js
Normal file
@@ -0,0 +1,54 @@
|
||||
// ── event_versions — SQL only ──────────────────────────────────────────────
|
||||
//
|
||||
// EVENTS.md §D. Immutable: there is an insert and there are reads, and there is
|
||||
// deliberately no update and no delete. A run pins a version, and that pin is
|
||||
// what makes the run reproducible and an audit answerable after the definition
|
||||
// has been edited underneath it.
|
||||
|
||||
const { query } = require('../../utils/db')
|
||||
const { parseJson } = require('./eventJson')
|
||||
|
||||
const hydrate = (row) => row && { ...row, spec: parseJson(row.spec, null) }
|
||||
|
||||
const listForDefinition = async (definitionId) =>
|
||||
(
|
||||
await query(
|
||||
`SELECT v.id, v.definition_id, v.version, v.published_at, v.published_by, u.username AS published_by_username
|
||||
FROM event_versions v
|
||||
LEFT JOIN users u ON u.id = v.published_by
|
||||
WHERE v.definition_id = ?
|
||||
ORDER BY v.version DESC`,
|
||||
[definitionId],
|
||||
)
|
||||
).map((row) => row)
|
||||
|
||||
const getById = async (id) => {
|
||||
const [row] = await query('SELECT * FROM event_versions WHERE id = ?', [id])
|
||||
return hydrate(row)
|
||||
}
|
||||
|
||||
/**
|
||||
* The next version number for a definition.
|
||||
*
|
||||
* Read separately and then INSERTed, which is a read-then-write — and it is safe
|
||||
* only because `UNIQUE (definition_id, version)` is behind it. Two publishes
|
||||
* racing for version 4 is one 1062 the caller reports, not two rows called 4.
|
||||
* The unique index is the mechanism; this query is the ergonomics.
|
||||
*/
|
||||
const nextVersion = async (definitionId) => {
|
||||
const [row] = await query(
|
||||
'SELECT COALESCE(MAX(version), 0) + 1 AS next FROM event_versions WHERE definition_id = ?',
|
||||
[definitionId],
|
||||
)
|
||||
return Number(row?.next || 1)
|
||||
}
|
||||
|
||||
const insert = async (definitionId, version, spec, userId) => {
|
||||
const result = await query(
|
||||
'INSERT INTO event_versions (definition_id, version, spec, published_by) VALUES (?, ?, ?, ?)',
|
||||
[definitionId, version, JSON.stringify(spec), userId],
|
||||
)
|
||||
return result.insertId
|
||||
}
|
||||
|
||||
module.exports = { listForDefinition, getById, nextVersion, insert }
|
||||
Reference in New Issue
Block a user