Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .changeset/32062-sync-in-delete-marking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
'@objectstack/spec': minor
'@objectstack/service-automation': minor
'@objectstack/runtime': minor
'@objectstack/objectql': patch
---

feat(service-automation)!: a sync-in pull marks the rows the source deleted, and an incremental source states how it learns of a delete

Clause-②: no (narrowing)
Metadata-protocol: narrowed
Skills-docs: none (no skills/ page names `deletePolicy`, `deletedField` or `_sync_deleted_at`; the guide that teaches them is `content/docs/automation/jobs.mdx`, updated here)

<!-- adr-0087: registered incremental-source-delete-policy-stated -->

ADR-0153 D3, delete marking (#32062, slice 6).

- **A pull marks a row the source deleted, and never deletes it.** Under `deletePolicy: 'mark'`, the reading when the key is absent, a pull stamps the platform column `_sync_deleted_at` on a row of a `synced` object that the source deleted, and clears it when a later pull reads the row again. `deletePolicy: 'ignore'` leaves every row as it is. Two detections:
- `deletedField`, on any pull that declares it: a read record whose value in that source field is truthy has its copy marked; every other read record's copy is cleared.
- Absence, on a full pull whose read was complete: every row of the mapping (`_sync_source`) the run did not read is marked. A `datasourceSource` with `full: true` is complete when its windows read to their end and no source row in the job's organization has a NULL watermark; it marks only rows whose copied watermark is at or above `watermark.start`. A `connectorSource` without a `watermark` is complete only under `paging: { style: 'none' }` (one response is the whole set); with no `paging`, or a paging style the pull does not follow yet, it marks nothing by absence. A refused or interrupted pull marks nothing by absence; a row the import runner refused was read, so it is not marked. A record whose `upsertKey` value is blank names no row, so one such record makes the run mark nothing by absence (`unjudged, rows refused`). A row pulled before the provenance columns existed carries no `_sync_source`, so it is no mapping's row: if the source has since deleted it, no pull marks it.
- Two mappings on one target: each marks and clears only the rows whose `_sync_source` is its own; a second mapping is not refused.
- **`sys_sync_run` records it.** New columns: `marked_deleted_count`, `deletes` (`judged`, `by deletedField`, `ignored`, or `unjudged, …` with the reason: `null watermark`, `no paging declared`, `pages not followed`, `rows refused`) and `skipped_null_watermark` (a datasource source's rows with a NULL watermark, counted under the organization predicate; through a plugin driver's row cap a count above the cap is recorded as the cap plus one). The end-of-pull log line says what the run marked and cleared. `job_run_id`'s and `trigger`'s descriptions now say when each is empty.
- **BREAKING: an incremental source states how it learns of a delete.** A `connectorSource` with a `watermark`, or a `datasourceSource` without `full: true`, never sees an absent row, so under `deletePolicy` absent or `'mark'` with no `deletedField` it is refused at parse (`MappingSchema`, so `defineStack` and `os validate`) and again when the pull runs (`400 VALIDATION_ERROR`, reason `delete_policy_unhonourable`). Fix: name the source's soft-delete field (`deletedField: 'is_deleted'`), or write `deletePolicy: 'ignore'`, which is what every pull did before this release. New export: `incrementalSourceDeleteRefusal` (`@objectstack/spec/data`), the rule both doors ask.
- **BREAKING: a `synced` target that withholds the provenance columns** (`systemFields: false`) is refused under `deletePolicy` absent or `'mark'` (`400 mapping_target_refused`): no row of it can carry the mark. Fix: let the platform add its columns, or write `deletePolicy: 'ignore'`. This arm is judged only when the pull runs: `os validate` and `defineStack` do not catch it, so a stack that validates green can still fail every scheduled pull. Migration entry `incremental-source-delete-policy-stated` names both arms.
- A datasource source's `deletedField` must name a field of the source object (`400 source_binding_invalid` otherwise). `deletedField` is read by truthiness: the text `'false'` or `'0'` marks the row, and on a connector source a field the records do not carry reads as falsy on every record and marks nothing.
- A stored `deletePolicy` outside `'mark'` / `'ignore'` (which the parse refuses) is refused by the pull too (`delete_policy_unhonourable`) instead of being read as `'mark'`.
- The liveness rows for `deletePolicy` and `deletedField` on both sources are `live`, and `os validate` no longer warns that nothing reads them. `paging` stays `planned`: the connector paging loop is a later change.
- A connector pull whose target definition cannot be read now says whether the object is not registered or the metadata read failed.
- A connector pull keeps the job run id and trigger only when the run record can hold them: a trigger other than `schedule` or `manual`, or a run id longer than 255 characters, is left out instead of failing the run record's open.
- `JobHandlerContext` (`@objectstack/runtime`) declares `trigger` and `runId`: the handler run form spreads the job service's context into the handler's, so every `handler` job already receives `trigger`, and `runId` under the database job adapter. The jobs guide lists both.
63 changes: 53 additions & 10 deletions content/docs/automation/jobs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,46 @@ export const OrdersPullJob = defineJob({
part-way keeps the position of its last finished window. The first run,
every run of a mapping that declares `full: true`, and a run whose mapping
now reads another source object or field, or writes another target, read
from `watermark.start`; a run never reads below it. The delete keys change
nothing yet, and `os validate` says so.
from `watermark.start`; a run never reads below it.
- **A row the source deleted is marked, never deleted.** Each source block
carries `deletePolicy`: `'mark'` (the reading when the key is absent) or
`'ignore'`, which leaves every row as it is. Under `'mark'` a pull stamps
`_sync_deleted_at` on a row the source deleted and clears it when a later pull
reads the row again; the row stays an ordinary row on every read path, so
filter on `_sync_deleted_at` where a deleted row should not show. A pull learns
of a delete two ways:
- **`deletedField`**, on any run that declares it: a read record whose value
in that source field is truthy (a soft-delete flag or a deleted-at column)
has its copy marked, and every other read record's copy is cleared.
Truthy is JavaScript's: the text `'false'` or `'0'` marks the row, so map a
real boolean or a deleted-at value; on a `connectorSource` a misspelt field
reads as falsy on every record and marks nothing, silently.
- **Absence**, on a full run whose read was complete: every row of the
mapping (its `_sync_source`) the run did not read is marked. A
`datasourceSource` run with `full: true` is complete when its windows read
to their end and no source row in the job's organization has a NULL
watermark (such a row is in no window and would read as absent); it marks
only rows whose copied watermark is at or above `watermark.start`, so
raising the start marks nothing below it. A `connectorSource` run without a
`watermark` is complete only when it declares
`paging: { style: 'none' }`, which says its one response is the whole set;
with no `paging`, or a paging style the pull does not follow yet, it marks
nothing by absence. A run that is refused or stops part-way, one whose
rows the import runner refused beyond a thousand, or one that read any
record whose `upsertKey` value is blank (it names no row) marks nothing by
absence (`deletes: unjudged, rows refused`). A row pulled before the
provenance columns existed has no `_sync_source`, so no pull marks it.
- **Two mappings on one target** each mark and clear only the rows whose
`_sync_source` is their own; a second mapping is not refused.

An incremental source (a `connectorSource` with a `watermark`, a
`datasourceSource` without `full: true`) never sees an absent row, so under
`'mark'` it must name `deletedField`, or declare `deletePolicy: 'ignore'`:
`os validate` refuses it otherwise, and so does the pull of a mapping no build
judged. A `synced` target that declares `systemFields: false` has no column to
mark, so its mapping declares `deletePolicy: 'ignore'`; `os validate` does not
catch this one, only the pull refuses it (`mapping_target_refused`), so a
stack that validates green still fails each run until it is fixed.
- **Every row says where it came from.** A `synced` object carries five
hidden, read-only columns the platform adds to it, beside the tenant and audit
columns, with an index on (`organization_id`, `_sync_source`,
Expand All @@ -323,14 +361,14 @@ export const OrdersPullJob = defineJob({
a key of several fields, a JSON array of their values in the order `upsertKey`
lists them, such as `["10","A-1"]`), `_sync_pulled_at` (when it was written)
and `_sync_run_id` (the pull's `sys_sync_run` row; not written when no run
record is kept). `_sync_deleted_at` is there but empty on every row: delete
marking has not landed yet. Nothing else writes these columns: a write that
record is kept). `_sync_deleted_at` is the delete mark (above). Nothing else writes these columns: a write that
names a `_sync_` column is refused with `400
ERR_SYNC_PROVENANCE_WRITE_REFUSED`, a system-context one included, and so is
a write whose `beforeInsert` or `beforeUpdate` hook assigns, changes or
removes one, the pull's own write included. A mapping whose `fieldMapping`
targets one is refused when it runs, and so is a connector pull whose target
object's definition cannot be read. A `synced`
object's definition cannot be read (the refusal says whether the object is not
registered or the metadata read failed). A `synced`
target that declares `systemFields: false` has none of these columns, and its
rows are written without them.
- **How a run is recorded.** A pull the platform refuses — the mapping is gone,
Expand All @@ -341,11 +379,14 @@ export const OrdersPullJob = defineJob({
would refuse the same rows. Otherwise the run is `success`, including a pull
that found nothing new. Every pull also leaves one `sys_sync_run` row in the
job's organization: the mapping, the job run that drove it (`job_run_id`, the
attempt's `sys_job_run` id, and `trigger`, `schedule` or `manual`; both empty
under a job service that keeps no run history, where only the trigger may be
kept), where it read from and from which position,
the rows read, created, updated and refused, the position it committed, and
how it ended (`completed`, `degraded` or `failed` with the refusal's reason).
attempt's `sys_job_run` id, empty under a job service that keeps no run
history; and `trigger`, `schedule` or `manual`. Both are empty for a pull no
job started), where it read from and from which position,
the rows read, created, updated, refused and marked deleted, what it did about
deletes (`deletes`: `judged`, `by deletedField`, `ignored`, or `unjudged, …`
with the reason), the source rows with a NULL watermark
(`skipped_null_watermark`, a datasource source only), the position it
committed, and how it ended (`completed`, `degraded` or `failed` with the refusal's reason).
A pull still `running` when the server stopped is closed `failed`
(`interrupted`) at the next start. Rows are kept 30 days, except the one
holding each mapping's read position.
Expand Down Expand Up @@ -440,6 +481,8 @@ At run time the handler is invoked with a `JobHandlerContext`
| `ql` | the live ObjectQL engine — the same handle `defineStack({ onEnable })` receives |
| `logger` | the platform logger, so a job's diagnostics are not `console` output |
| `executionContext` | the context the job runs as — `{ isSystem: true, tenantId }` for a job that declares an [`organization`](#the-organization-a-job-runs-as), else `{ isSystem: true }`. `ql` is the raw engine, so pass it as each call's `context` to write as that organization |
| `trigger` | why this run started — `schedule` or `manual`; absent under a job service that sends none |
| `runId` | the job service's run-history id for this attempt (a `sys_job_run` id under the database job adapter); absent under a job service that keeps no run history |

```typescript
import type { JobHandlerContext } from '@objectstack/runtime';
Expand Down
5 changes: 5 additions & 0 deletions packages/objectql/src/sync-provenance-write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ export function snapshotSyncKeys(row: unknown): ReadonlyMap<string, unknown> {
* caller judged clean the snapshot is empty, so any `_sync_` key is a hook's;
* on the pull's write it holds the pull's stamps, which must reach the driver
* as written.
*
* The comparison is by identity (`Object.is`), so a hook that replaced the
* payload with a deep copy would be refused if a stamp were held as an object
* such as a `Date`; the pull hands every stamp in as a string, and the error
* errs toward refusing, so nothing is stored wrongly.
*/
export function refuseHookSyncProvenanceWrite(
object: string,
Expand Down
15 changes: 14 additions & 1 deletion packages/runtime/src/job-handler-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
* unchanged byte for byte; no `IJobService` implementation grows a member.
*/

import type { IObjectQLEngine, Logger } from '@objectstack/spec/contracts';
import type { IObjectQLEngine, JobRunTrigger, Logger } from '@objectstack/spec/contracts';

/**
* What `AppPlugin` invokes a declarative job's `functions` entry with.
Expand Down Expand Up @@ -111,4 +111,17 @@ export interface JobHandlerContext {
* keeps compiling — the widening stays additive in both directions.
*/
executionContext?: { isSystem: true; tenantId?: string };
/**
* Why this run started: `'schedule'` on the job's schedule, `'manual'`
* when someone ran it now. Handed on from the job service's own context
* (`IJobService`'s handler context), so it is absent under a job service
* that sends none.
*/
trigger?: JobRunTrigger;
/**
* The job service's run-history id for this attempt: on the shipped
* adapters, the `sys_job_run` row `DbJobAdapter` wrote. Absent under a job
* service that keeps no run history.
*/
runId?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function incrementalMapping(input: Record<string, unknown>) {
connectorSource: {
connector: 'crm_api', action: 'list_contacts', input,
watermark: { field: 'updated_at', param: 'since' },
deletePolicy: 'ignore',
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const MAPPING = {
input: { method: 'GET', path: '/contacts' },
recordsPath: 'body.results',
watermark: { field: 'updated_at', param: 'since' },
deletePolicy: 'ignore',
},
};

Expand Down
Loading
Loading