@workflow/world-postgres 5.0.0-beta.9 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/README.md +305 -34
  2. package/bin/setup-deprecated.js +20 -0
  3. package/dist/config.d.ts +14 -0
  4. package/dist/config.d.ts.map +1 -1
  5. package/dist/config.js +1 -1
  6. package/dist/drizzle/schema.d.ts +375 -5
  7. package/dist/drizzle/schema.d.ts.map +1 -1
  8. package/dist/drizzle/schema.js +91 -14
  9. package/dist/executor.d.ts +8 -0
  10. package/dist/executor.d.ts.map +1 -0
  11. package/dist/executor.js +79 -0
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +34 -12
  14. package/dist/invocation-notifications.d.ts +23 -0
  15. package/dist/invocation-notifications.d.ts.map +1 -0
  16. package/dist/invocation-notifications.js +156 -0
  17. package/dist/invocation-retention.d.ts +11 -0
  18. package/dist/invocation-retention.d.ts.map +1 -0
  19. package/dist/invocation-retention.js +14 -0
  20. package/dist/invocations.d.ts +24 -0
  21. package/dist/invocations.d.ts.map +1 -0
  22. package/dist/invocations.js +208 -0
  23. package/dist/message.d.ts +1 -0
  24. package/dist/message.d.ts.map +1 -1
  25. package/dist/message.js +5 -3
  26. package/dist/queue.d.ts +32 -8
  27. package/dist/queue.d.ts.map +1 -1
  28. package/dist/queue.js +467 -96
  29. package/dist/retention.d.ts +66 -0
  30. package/dist/retention.d.ts.map +1 -0
  31. package/dist/retention.js +152 -0
  32. package/dist/run-status.d.ts +54 -0
  33. package/dist/run-status.d.ts.map +1 -0
  34. package/dist/run-status.js +138 -0
  35. package/dist/storage.d.ts +8 -1
  36. package/dist/storage.d.ts.map +1 -1
  37. package/dist/storage.js +1613 -319
  38. package/dist/streamer.d.ts.map +1 -1
  39. package/dist/streamer.js +109 -41
  40. package/dist/zod.d.ts.map +1 -1
  41. package/dist/zod.js +3 -3
  42. package/package.json +17 -14
  43. package/src/drizzle/migrations/0014_add_attr_set_event_unique_index.sql +5 -0
  44. package/src/drizzle/migrations/0015_move_enums_to_workflow_schema.sql +75 -0
  45. package/src/drizzle/migrations/0016_add_encryption_public_key.sql +1 -0
  46. package/src/drizzle/migrations/0017_add_hook_resume_context.sql +1 -0
  47. package/src/drizzle/migrations/0018_add_hook_token_retention.sql +1 -0
  48. package/src/drizzle/migrations/0019_add_event_slots.sql +74 -0
  49. package/src/drizzle/migrations/0020_add_invocations.sql +13 -0
  50. package/src/drizzle/migrations/0021_invocation_identity_retention.sql +19 -0
  51. package/src/drizzle/migrations/0022_invocation_outcomes.sql +3 -0
  52. package/src/drizzle/migrations/0023_add_hook_claimed_from.sql +1 -0
  53. package/src/drizzle/migrations/0024_add_dynamic_workflow_code.sql +1 -0
  54. package/src/drizzle/migrations/meta/_journal.json +77 -0
@@ -0,0 +1,74 @@
1
+ -- Event ids become per-run slot positions (`evnt_` + a zero-padded decimal),
2
+ -- so an id is only unique together with its run. Runs created before this keep
3
+ -- their globally-unique ULIDs, which the composite key also admits.
4
+ --
5
+ -- LOCKING. Replacing a primary key takes ACCESS EXCLUSIVE on
6
+ -- `workflow.workflow_events`, which blocks reads as well as writes, and the
7
+ -- migrator runs every pending migration in one transaction, so the lock is held
8
+ -- until all of them commit. Building the new key's index under that lock is the
9
+ -- part that grows with the table. On an empty or modest table this is
10
+ -- instantaneous and needs no thought.
11
+ --
12
+ -- On a large existing table, build the index first, outside the migrator, and
13
+ -- this migration adopts it instead of building its own:
14
+ --
15
+ -- CREATE UNIQUE INDEX CONCURRENTLY "workflow_events_run_id_id_idx"
16
+ -- ON "workflow"."workflow_events" ("run_id", "id");
17
+ --
18
+ -- `CONCURRENTLY` cannot appear in this file: Postgres rejects it inside a
19
+ -- transaction block. Run it by hand, confirm the index came out valid, then
20
+ -- migrate. The branch below picks it up, and the exclusive lock then covers
21
+ -- only a catalog update rather than a full build. Adopting an index renames it
22
+ -- to the constraint's name, so it ends up as `workflow_events_run_id_id_pk`
23
+ -- either way and the two paths leave the same schema behind.
24
+ -- Bound the wait for that lock. A pending ACCESS EXCLUSIVE queues ahead of
25
+ -- every lock request that arrives after it, so waiting on one long-running
26
+ -- reader stalls all traffic to the table for as long as the wait lasts. Ten
27
+ -- seconds of that is a blip; an unbounded wait is an outage. Failing instead
28
+ -- leaves the migration unapplied and retryable. Raise it by hand for a
29
+ -- maintenance window.
30
+ --
31
+ -- `SET LOCAL` lasts for the transaction, and the migrator runs every pending
32
+ -- migration in one, so a migration that follows this one in the same batch
33
+ -- inherits the timeout.
34
+ SET LOCAL lock_timeout = '10s';--> statement-breakpoint
35
+ ALTER TABLE "workflow"."workflow_events" DROP CONSTRAINT "workflow_events_pkey";--> statement-breakpoint
36
+ DO $$
37
+ BEGIN
38
+ IF EXISTS (
39
+ SELECT 1
40
+ FROM pg_class c
41
+ JOIN pg_namespace n ON n.oid = c.relnamespace
42
+ JOIN pg_index i ON i.indexrelid = c.oid
43
+ WHERE n.nspname = 'workflow'
44
+ AND c.relname = 'workflow_events_run_id_id_idx'
45
+ AND c.relkind = 'i'
46
+ AND i.indisunique
47
+ AND i.indisvalid
48
+ ) THEN
49
+ ALTER TABLE "workflow"."workflow_events"
50
+ ADD CONSTRAINT "workflow_events_run_id_id_pk"
51
+ PRIMARY KEY USING INDEX "workflow_events_run_id_id_idx";
52
+ ELSE
53
+ -- A `CREATE UNIQUE INDEX CONCURRENTLY` that failed leaves an index of this
54
+ -- name behind marked invalid. The branch above rejects it, and the key
55
+ -- built here is a different index under a different name, so without this
56
+ -- the invalid one would survive the migration: never used by a plan, still
57
+ -- maintained on every insert. Dropping it also makes a second attempt at
58
+ -- the concurrent build possible without a manual cleanup first.
59
+ DROP INDEX IF EXISTS "workflow"."workflow_events_run_id_id_idx";
60
+ ALTER TABLE "workflow"."workflow_events"
61
+ ADD CONSTRAINT "workflow_events_run_id_id_pk" PRIMARY KEY ("run_id","id");
62
+ END IF;
63
+ END $$;--> statement-breakpoint
64
+ -- Redundant once the primary key leads with `run_id`: that index serves every
65
+ -- by-run lookup and range scan this one did, so keeping it only costs a second
66
+ -- write per event on the table's hottest path.
67
+ DROP INDEX IF EXISTS "workflow"."workflow_events_run_id_index";--> statement-breakpoint
68
+ -- One row per slot-numbered run. Its absence is the "this run predates slots"
69
+ -- signal, so no backfill: existing runs stay on ULIDs for the rest of their
70
+ -- lives. A marker only: positions are allocated by the insert that occupies
71
+ -- them, read from the event log itself.
72
+ CREATE TABLE IF NOT EXISTS "workflow"."workflow_event_slots" (
73
+ "run_id" varchar PRIMARY KEY NOT NULL
74
+ );
@@ -0,0 +1,13 @@
1
+ CREATE TABLE "workflow"."workflow_invocations" (
2
+ "sequence" bigserial NOT NULL,
3
+ "run_id" varchar NOT NULL,
4
+ "request_id" varchar NOT NULL,
5
+ "payload" bytea NOT NULL,
6
+ "fingerprint" varchar NOT NULL,
7
+ "result" bytea,
8
+ "created_at" timestamp DEFAULT now() NOT NULL,
9
+ "responded_at" timestamp,
10
+ PRIMARY KEY ("run_id", "request_id")
11
+ );
12
+ --> statement-breakpoint
13
+ CREATE INDEX "workflow_invocations_pending" ON "workflow"."workflow_invocations" ("run_id", "sequence") WHERE "responded_at" IS NULL;
@@ -0,0 +1,19 @@
1
+ ALTER TABLE "workflow"."workflow_events" ADD COLUMN "resume_id" varchar;
2
+ --> statement-breakpoint
3
+ ALTER TABLE "workflow"."workflow_events" ADD COLUMN "resume_payload_digest" varchar;
4
+ --> statement-breakpoint
5
+ CREATE UNIQUE INDEX "workflow_events_hook_resume_unique" ON "workflow"."workflow_events" ("run_id", "resume_id") WHERE "type" = 'hook_received' AND "resume_id" IS NOT NULL;
6
+ --> statement-breakpoint
7
+ ALTER TABLE "workflow"."workflow_invocations" ALTER COLUMN "payload" DROP NOT NULL;
8
+ --> statement-breakpoint
9
+ ALTER TABLE "workflow"."workflow_invocations" ALTER COLUMN "fingerprint" DROP NOT NULL;
10
+ --> statement-breakpoint
11
+ ALTER TABLE "workflow"."workflow_invocations" ADD COLUMN "expired_at" timestamp;
12
+ --> statement-breakpoint
13
+ UPDATE "workflow"."workflow_invocations" AS invocation
14
+ SET "payload" = NULL, "result" = NULL, "fingerprint" = NULL,
15
+ "expired_at" = coalesce(invocation.expired_at, run.expired_at, now())
16
+ FROM "workflow"."workflow_runs" AS run
17
+ WHERE invocation.run_id = run.id
18
+ AND (run.expired_at IS NOT NULL OR
19
+ (run.status IN ('completed', 'failed', 'cancelled') AND run.attributes -> '$retention' = '"0"'::jsonb));
@@ -0,0 +1,3 @@
1
+ -- Distinguish arbitrary legacy return values from versioned outcome envelopes.
2
+ ALTER TABLE "workflow"."workflow_invocations"
3
+ ADD COLUMN IF NOT EXISTS "result_version" integer NOT NULL DEFAULT 0;
@@ -0,0 +1 @@
1
+ ALTER TABLE "workflow"."workflow_hooks" ADD COLUMN "claimed_from" bytea;
@@ -0,0 +1 @@
1
+ ALTER TABLE "workflow"."workflow_runs" ADD COLUMN "dynamic_workflow_code_cbor" bytea;
@@ -99,6 +99,83 @@
99
99
  "when": 1779609600000,
100
100
  "tag": "0013_add_attributes",
101
101
  "breakpoints": true
102
+ },
103
+ {
104
+ "idx": 14,
105
+ "version": "7",
106
+ "when": 1780185600000,
107
+ "tag": "0014_add_attr_set_event_unique_index",
108
+ "breakpoints": true
109
+ },
110
+ {
111
+ "idx": 15,
112
+ "version": "7",
113
+ "when": 1782691200000,
114
+ "tag": "0015_move_enums_to_workflow_schema",
115
+ "breakpoints": true
116
+ },
117
+ {
118
+ "idx": 16,
119
+ "version": "7",
120
+ "when": 1784937600000,
121
+ "tag": "0016_add_encryption_public_key",
122
+ "breakpoints": true
123
+ },
124
+ {
125
+ "idx": 17,
126
+ "version": "7",
127
+ "when": 1785283200000,
128
+ "tag": "0017_add_hook_resume_context",
129
+ "breakpoints": true
130
+ },
131
+ {
132
+ "idx": 18,
133
+ "version": "7",
134
+ "when": 1785619990000,
135
+ "tag": "0018_add_hook_token_retention",
136
+ "breakpoints": true
137
+ },
138
+ {
139
+ "idx": 19,
140
+ "version": "7",
141
+ "when": 1786060800000,
142
+ "tag": "0019_add_event_slots",
143
+ "breakpoints": true
144
+ },
145
+ {
146
+ "idx": 20,
147
+ "version": "7",
148
+ "when": 1789430400000,
149
+ "tag": "0020_add_invocations",
150
+ "breakpoints": true
151
+ },
152
+ {
153
+ "idx": 21,
154
+ "version": "7",
155
+ "when": 1789516800000,
156
+ "tag": "0021_invocation_identity_retention",
157
+ "breakpoints": true
158
+ },
159
+ {
160
+ "idx": 22,
161
+ "version": "7",
162
+ "when": 1789603200000,
163
+ "tag": "0022_invocation_outcomes",
164
+ "breakpoints": true
165
+ },
166
+ {
167
+ "idx": 23,
168
+ "version": "7",
169
+ "when": 1789689600000,
170
+ "tag": "0023_add_hook_claimed_from",
171
+ "breakpoints": true
172
+ },
173
+ {
174
+ "idx": 24,
175
+ "version": "7",
176
+ "when": 1789776000000,
177
+ "tag": "0024_add_dynamic_workflow_code",
178
+ "breakpoints": true
102
179
  }
103
180
  ]
104
181
  }