Load a scheduled Apify web scrape into Postgres
Run an Apify actor on a schedule, fetch its dataset, reshape the records, and bulk-load them into Postgres with cost caps and Slack alerts.
Catch SurrealDB field record links pointing at deleted ids, alert Slack, then clear them to NONE behind an approval and re-verify.
A record link stored in a normal SurrealDB field is a pointer with nothing enforcing it.
UPDATE article:1 SET author = author:anna succeeds whether or not author:anna exists.
DELETE author:anna afterwards does not touch the article. The field keeps the id of a record
that is gone. The holding record still looks healthy, so SELECT * FROM article shows nothing
wrong. The breakage surfaces later: a reader gets NONE where it expected a record, a
FETCH author returns nothing at all. Joins quietly lose rows.
This is not the graph-edge case. An edge table built with RELATE is cascade-deleted when an
endpoint goes, so those links clean themselves up. A link in an ordinary field is not, which is
why it survives to be found later.
This blueprint tests each link against the target table's live ids. A link is dangling when the
field holds a record id that is not in SELECT VALUE id FROM <target table>, which covers both
a deleted target and a link written into the wrong table. It reports the exact ids, alerts
Slack, then behind a dry-run gate and an approval clears the field to NONE one record at a time
and re-reads the dangling set to prove the table is clean.
This blueprint was created by zkasuran.
| Finding | Rule | Fix |
|---|---|---|
| Dead id | the link is a record of the target table but that id is not live | clear the field to NONE behind an approval, then re-read |
| Wrong table | the link resolves to a record outside the target table | same, with the real table named in the report so the write path can be traced |
| Empty field | the field is NONE or absent | None. type::is_record keeps it out of the result |
| Live link | the id is in the target table's live ids | None |
type::is_record keeps the test on real links, so a field that is already NONE or absent or
holding a plain string is never reported and never touched.repair_action: delete_record behind a second input, confirm_delete_records. With that
input left false a delete_record run refuses and fails rather than silently clearing the
field instead, so the two repairs can never be confused.UPDATE article SET author = NONE would wipe the field on every row. This flow never
issues one. Remediation loops over the id set from detection and each statement is
UPDATE <one id> SET <field> = NONE WHERE <the dangling test>, so it names a single record
and repeats the guard. A link repaired between detection and remediation is left alone,
proven by a 0-row result on that id.auto_remediate true, dry_run false, a non-empty dangling set, then a human
answering the Pause. For delete_record, confirm_delete_records as well.concurrencyLimit: 1) because concurrent writes to one table race.reselect reads the dangling set again. If any link still points outside
the target table the run fails, so a cleanup that did not land is never reported as success.max_records caps how many records one run returns and repairs. A run that reaches the cap
says so and asks to be re-run, rather than loading an unbounded result.detect (io.kestra.plugin.surrealdb.Query, fetchType: FETCH) selects the records whose
link field holds an id outside SELECT VALUE id FROM <target table>, up to max_records.
It returns each holding id as a canonical string plus the link and the table it points at.
size is the dangling count.count_records reads the table total and how many records hold a link at all, so the report
states the share that is broken.assess (io.kestra.plugin.core.output.OutputValues) derives the id set, the counts, the
capped flag and the alert text once, so every branch reads the same values.report logs the summary. route (io.kestra.plugin.core.flow.If) stops on a clean table
and continues on a finding.alert_findings posts the dangling set to Slack and planned logs what a
remediation run would change, on every run including a dry one.remediate_gate opens only with auto_remediate true, dry_run false and a non-empty set.
hold (io.kestra.plugin.core.flow.Pause) waits for an answer.pick_action routes to remediate_clear (io.kestra.plugin.core.flow.Loop,
concurrencyLimit: 1), which sets the field to NONE on each id with its guard repeated. The
delete_record branch runs remediate_delete only when confirm_delete_records is true,
otherwise refuse_delete fails the run.reselect reads the dangling set again and verify fails the run if any link still dangles.
Without approval, declined records the decision and nothing changes.errors posts to Slack when the flow fails.author, owner, parent) where the
target can be deleted by another code path.FETCH come back empty after a cleanup job or a restore.SurrealDB has no scheduler, no approval step and no alerting of its own. Kestra runs the check
on a cadence, holds the write half behind auto_remediate, dry_run and a Pause with the
decision recorded on the execution, posts findings to Slack and keeps the read, the repair and
the re-read in one auditable run. The id set from detection is carried straight into the repair
loop, so remediation acts on what was found and nothing wider.
Verified on Kestra 2.0.5 OSS against a SurrealDB 3.3.3 container on the same Docker network,
with a local HTTP sink standing in for Slack. Every count below was read back from SurrealDB's
own surreal sql, not only from the Kestra log. The article table started with three
records: article:a1 holding author = author:anna, article:a2 holding the live
author = author:bob, plus article:a3 with no author field at all. Deleting author:anna
is what makes article:a1 dangle, with no statement ever touching article:a1 itself.
| Run | Execution | Inputs | Result |
|---|---|---|---|
| 1 | 5J7lMmAn5J0zQxwHt3epC1 |
defaults, every link live | 0 dangling of 2 linked, 3 records. clean logs, no Slack finding |
| 2 | 3u3nhfyYzC7PQstZLRiM5F |
defaults, after DELETE author:anna |
1 dangling of 2 linked, classified (dead id). Slack finding posted, planned logged. CLI after: still 1 dangling |
| 3 | 750fcnnF86FQE3jf4Ud5tR |
auto_remediate: true, dry_run: true |
gate held, hold never ran, nothing changed. CLI after: 1 dangling |
| 4 | KYESGcyZmFA9JBSVKISFO |
auto_remediate: true, dry_run: false, resumed approved: true |
article:a1 cleared, reselect found 0, SUCCESS. CLI before 3 records with 1 dangling, after 3 records with 0 dangling, article:a2 still on author:bob with article:a3 untouched |
| 5 | 39abEntAhJG1VQvY4pdabi |
the same, resumed approved: false |
declined, nothing changed. CLI after: 1 dangling |
| 6 | 5xQ8D5lvEFClCpoAsryGeE |
repair_action: delete_record, confirm_delete_records: false, resumed approved: true |
refuse_delete failed the run, errors posted to Slack. CLI after: 3 records with 1 dangling, nothing deleted |
| 7 | AUwAWx452jdeDEILLUWD5 |
repair_action: delete_record, confirm_delete_records: true, resumed approved: true |
article:a1 deleted, reselect found 0, SUCCESS. CLI before 3 records with 1 dangling, after 2 records with 0 dangling, article:a2 plus article:a3 intact |
| 8 | 6R3BqxPqYG3zFZ2fyc2K1D |
unreachable host | detect failed, errors posted to Slack, FAILED |
| 9 | 1BrlKreGck65yCjExfnNcE |
2 dangling seeded (article:a1, article:a4), max_records: 1, resumed approved: true |
capped at 1, cleared article:a1, reselect found article:a4 still dangling, verify failed the run, errors posted. CLI before 2 dangling, after 1 |
| 10 | vgq7JwyydYPLsTGS5ko8 |
article:a4 repointed at the live tag:x |
1 dangling, classified (wrong table tag), so a link into a table that is not the target is caught as well as a dead id |
Run 1 against run 2 isolates that the detector catches a real dangling link. Nothing changed
between them except DELETE author:anna, which touched no article. The report moved from 0
findings to 1. Run 9 proves the re-verify can fail: one dangling link was cleared by exact id
while the second was left outside the cap, so reselect still found one and the run failed
rather than reporting a clean table. Runs 4 and 7 both left article:a2 (live link) with
article:a3 (empty field) untouched, which is the precision claim. Run 6 proves the
destructive action cannot be reached by setting repair_action alone. A guarded
UPDATE article:a2 SET author = NONE WHERE <the dangling test> was also run directly in
surreal sql and returned 0 rows, which is the per-statement guard refusing to clear a link
that resolves.
SELECT,
UPDATE and, for the delete action, DELETE on the tables involved.plugin-surrealdb plugin, bundled with Kestra.SLACK_WEBHOOK_URL: used by alert_findings and the errors block. In Kestra OSS, provide
it as the base64-encoded environment variable SECRET_SLACK_WEBHOOK_URL.SURREALDB_PASSWORD: the database password, the default for the password input.| Input | Default | Purpose |
|---|---|---|
host |
surrealdb |
The SurrealDB host |
namespace, database |
test, test |
Namespace and database to check |
username, password |
root, secret |
Credentials |
source_table |
article |
The table holding the link field |
link_field |
author |
The field that holds a record link |
target_table |
author |
The table whose live ids are the allowed set |
max_records |
1000 |
Ceiling on records returned and repaired per run |
repair_action |
clear_link |
clear_link or delete_record |
confirm_delete_records |
false |
Second gate, required for delete_record |
approval_timeout |
PT2H |
How long the pause waits |
auto_remediate |
false |
Allow the repair |
dry_run |
true |
Must be false for a change to apply |
The port (8000) and TLS (off) are set on the Query tasks. For a different port or a TLS
endpoint, change port and useTls on each task.
SLACK_WEBHOOK_URL and SURREALDB_PASSWORD secrets.host, namespace, database, source_table, link_field and
target_table, then run it. A clean table logs and stops. A dangling link sends a Slack
alert and logs the id set.auto_remediate: true and dry_run: false, run it, then answer the pause.
The field is cleared to NONE on each detected id.reselect step. The run fails if any link still points outside the target table.daily trigger, which always runs in safe mode.Loop over a list of
table and field pairs. INFO FOR TABLE <name> reports the declared fields to drive it.UPDATE with one that sets the field to a tombstone
record, so readers can tell a repaired link from one that was never set.SCHEMAFULL table definition with an assert on the field, which stops new dangling
links being written while this flow cleans up the ones already stored.UPDATE: https://surrealdb.com/docs/surrealql/statements/update