create-protobase 0.100.21 → 0.100.23

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.
@@ -7,7 +7,7 @@ resources: the first change adds its first tables and resources.
7
7
 
8
8
  ## Database changes
9
9
 
10
- - Every schema change is a new migration file in `db/migrations/`, numbered after the last one (`012_customers.sql`
10
+ - Every schema change is a new migration file in `db/migrations/`, numbered after the last one (`013_customers.sql`
11
11
  after the auth, jobs and workflows migrations). Never edit or remove a migration that exists: it has already run on
12
12
  the live database.
13
13
  - Expand only, where possible: add tables, columns and indexes, and keep what the running app still reads. Rename or
@@ -0,0 +1,417 @@
1
+ -- The workflows, their versions, runs and steps, written by `protobase migration`.
2
+ -- Applied with the project's other migrations; edit or remove statements the project handles differently.
3
+
4
+ create schema if not exists "workflows";
5
+
6
+ do $$ begin
7
+ if exists (select 1 from information_schema.columns where table_schema = 'workflows' and table_name = 'workflows' and column_name = 'definition' and is_generated = 'NEVER') then
8
+ alter table "workflows"."workflows" add column "state_machine" jsonb, add column "trigger" jsonb,
9
+ add column "execution_order" text not null default 'sequence' check ("execution_order" in ('sequence', 'data')),
10
+ add column "connections" jsonb, add column "trace" text check ("trace" in ('all', 'failed', 'none'));
11
+ update "workflows"."workflows" set "state_machine" = "definition" -> 'stateMachine', "trigger" = "definition" -> 'trigger',
12
+ "execution_order" = coalesce("definition" ->> 'order', 'sequence'), "connections" = "definition" -> 'connections', "trace" = "definition" ->> 'trace';
13
+ alter table "workflows"."workflows" alter column "state_machine" set not null, alter column "trigger" set not null, drop column "definition";
14
+ end if;
15
+ end $$;
16
+ -- The definition the database once put together from the parts, which nothing reads now
17
+ alter table "workflows"."workflows" drop column if exists "definition";
18
+ do $$ begin
19
+ if exists (select 1 from information_schema.columns where table_schema = 'workflows' and table_name = 'versions' and column_name = 'definition' and is_generated = 'NEVER') then
20
+ alter table "workflows"."versions" add column "state_machine" jsonb, add column "trigger" jsonb,
21
+ add column "execution_order" text not null default 'sequence' check ("execution_order" in ('sequence', 'data')),
22
+ add column "connections" jsonb, add column "trace" text check ("trace" in ('all', 'failed', 'none'));
23
+ update "workflows"."versions" set "state_machine" = "definition" -> 'stateMachine', "trigger" = "definition" -> 'trigger',
24
+ "execution_order" = coalesce("definition" ->> 'order', 'sequence'), "connections" = "definition" -> 'connections', "trace" = "definition" ->> 'trace';
25
+ alter table "workflows"."versions" alter column "state_machine" set not null, alter column "trigger" set not null, drop column "definition";
26
+ end if;
27
+ end $$;
28
+ -- The definition the database once put together from the parts, which nothing reads now
29
+ alter table "workflows"."versions" drop column if exists "definition";
30
+
31
+ alter table "workflows"."workflows" add column if not exists "layout" jsonb not null default '{}'::jsonb;
32
+ alter table "workflows"."versions" add column if not exists "layout" jsonb not null default '{}'::jsonb;
33
+
34
+ create table if not exists "workflows"."checkpoints" (
35
+ "run_id" uuid not null references "workflows"."runs" ("id") on delete cascade,
36
+ "scope" text not null,
37
+ "seq" integer not null,
38
+ "follows" integer not null,
39
+ "state" text not null,
40
+ "type" text not null,
41
+ "status" text not null check ("status" in ('succeeded', 'retrying', 'caught')),
42
+ "fetched_at" timestamptz not null,
43
+ "next" text,
44
+ "value" jsonb,
45
+ "variables" jsonb not null,
46
+ "attempt" integer not null,
47
+ "failures" jsonb not null,
48
+ "visits" jsonb not null,
49
+ "resume_at" timestamptz,
50
+ "clock" timestamptz not null,
51
+ primary key ("run_id", "scope")
52
+ );
53
+ alter table "workflows"."runs" add column if not exists "trace" text check ("trace" in ('all', 'failed', 'none'));
54
+ alter table "workflows"."runs" add column if not exists "trace_file" text;
55
+
56
+ drop table if exists "workflows"."steps";
57
+
58
+ alter table "workflows"."workflows" add column if not exists "enabled_version" integer;
59
+ do $$ begin
60
+ if not exists (select 1 from pg_constraint where conname = 'workflows_enabled_version' and connamespace = 'workflows'::regnamespace) then
61
+ alter table "workflows"."workflows" add constraint "workflows_enabled_version" foreign key ("id", "enabled_version") references "workflows"."versions" ("workflow_id", "version") deferrable initially deferred;
62
+ end if;
63
+ end $$;
64
+ -- Who turned the version on, signed in as themselves: the consent its runs sign in through their connections with.
65
+ alter table "workflows"."versions" add column if not exists "enabled_by" text;
66
+ create table if not exists "workflows"."triggers" (
67
+ "id" uuid primary key default gen_random_uuid(),
68
+ "workflow_id" uuid references "workflows"."workflows" ("id") on delete set null,
69
+ "version" integer not null,
70
+ "tenant_id" text,
71
+ "type" text not null,
72
+ "input" jsonb not null,
73
+ "connection_id" text,
74
+ "status" text not null check ("status" in ('active', 'failing', 'needs_sign_in', 'stopping')),
75
+ "generation" integer not null,
76
+ "cursor" jsonb check (octet_length("cursor"::text) <= 65536),
77
+ "store" jsonb check (octet_length("store"::text) <= 65536),
78
+ "poll_job_id" text,
79
+ "poll_attempt" integer,
80
+ "consecutive_failures" integer not null,
81
+ "last_error" jsonb,
82
+ "next_poll_at" timestamptz,
83
+ "created_at" timestamptz not null default now(),
84
+ "updated_at" timestamptz not null default now()
85
+ );
86
+ alter table "workflows"."triggers" add column if not exists "poll_attempt" integer;
87
+ -- Who turned the trigger's version on: the only person whose account its stop job ends the subscription through.
88
+ alter table "workflows"."triggers" add column if not exists "enabled_by" text;
89
+ -- A deleted workflow's trigger stays, without its workflow, until its stop job unsubscribed it at its provider.
90
+ alter table "workflows"."triggers" alter column "workflow_id" drop not null;
91
+ do $$ begin
92
+ if exists (select 1 from pg_constraint where conname = 'triggers_workflow_id_fkey' and confdeltype = 'c' and connamespace = 'workflows'::regnamespace) then
93
+ alter table "workflows"."triggers" drop constraint "triggers_workflow_id_fkey";
94
+ alter table "workflows"."triggers" add constraint "triggers_workflow_id_fkey" foreign key ("workflow_id") references "workflows"."workflows" ("id") on delete set null;
95
+ end if;
96
+ end $$;
97
+ create unique index if not exists "triggers_workflow" on "workflows"."triggers" ("workflow_id") where "status" <> 'stopping';
98
+ create index if not exists "triggers_workflow_id" on "workflows"."triggers" ("workflow_id");
99
+ create index if not exists "triggers_updated_at" on "workflows"."triggers" ("updated_at");
100
+ create index if not exists "triggers_status_type" on "workflows"."triggers" ("status", "type");
101
+ create index if not exists "triggers_connection" on "workflows"."triggers" ("connection_id");
102
+ create index if not exists "triggers_tenant" on "workflows"."triggers" ("tenant_id");
103
+
104
+ create table if not exists "workflows"."effects" (
105
+ "run_id" uuid not null references "workflows"."runs" ("id") on delete cascade,
106
+ "key" text not null,
107
+ "result" jsonb,
108
+ primary key ("run_id", "key")
109
+ );
110
+
111
+ create table if not exists "workflows"."connection_revocations" (
112
+ "id" uuid primary key default gen_random_uuid(),
113
+ "provider" text not null,
114
+ "hint" text not null check ("hint" in ('refresh_token', 'access_token')),
115
+ "token" text not null,
116
+ "created_at" timestamptz not null default now()
117
+ );
118
+
119
+ create table if not exists "workflows"."hooks" (
120
+ "workflow_id" uuid primary key references "workflows"."workflows" ("id") on delete cascade,
121
+ "key_hash" text not null unique,
122
+ "secret" text,
123
+ "secret_hash" text not null,
124
+ "tenant_id" text,
125
+ "created_at" timestamptz not null default now()
126
+ );
127
+
128
+ create table if not exists "workflows"."step_cache" (
129
+ "type" text not null,
130
+ "key" text not null,
131
+ "who" jsonb not null,
132
+ "tenant_id" text,
133
+ "output" jsonb,
134
+ "secrets" jsonb not null,
135
+ "cached_at" timestamptz not null default now(),
136
+ "expires_at" timestamptz not null,
137
+ primary key ("type", "key", "who")
138
+ );
139
+ create index if not exists "step_cache_expires" on "workflows"."step_cache" ("type", "tenant_id", "expires_at");
140
+ create table if not exists "workflows"."step_cache_shapes" (
141
+ "type" text not null,
142
+ "shape" jsonb not null,
143
+ primary key ("type", "shape")
144
+ );
145
+
146
+ create extension if not exists pg_trgm;
147
+ create index if not exists "workflows_tenant" on "workflows"."workflows" ("tenant_id");
148
+ create index if not exists "workflows_name" on "workflows"."workflows" ("name");
149
+ create index if not exists "workflows_updated_at" on "workflows"."workflows" ("updated_at");
150
+ create index if not exists "workflows_execution_order" on "workflows"."workflows" ("execution_order");
151
+ create index if not exists "workflows_name_search" on "workflows"."workflows" using gin ("name" gin_trgm_ops);
152
+ create index if not exists "versions_tenant" on "workflows"."versions" ("tenant_id");
153
+ create index if not exists "versions_version" on "workflows"."versions" ("version");
154
+ alter table "workflows"."runs" add column if not exists "key" text;
155
+ create unique index if not exists "runs_key" on "workflows"."runs" ("workflow_id", "key");
156
+ alter table "workflows"."runs" add column if not exists "rerun_of" uuid references "workflows"."runs" ("id") on delete set null;
157
+ create index if not exists "runs_rerun_of" on "workflows"."runs" ("rerun_of");
158
+ alter table "workflows"."runs" add column if not exists "replay" boolean;
159
+ create index if not exists "runs_workflow" on "workflows"."runs" ("workflow_id", "created_at");
160
+ create index if not exists "runs_tenant" on "workflows"."runs" ("tenant_id");
161
+ create index if not exists "runs_status" on "workflows"."runs" ("status");
162
+ create index if not exists "runs_created_at" on "workflows"."runs" ("created_at");
163
+
164
+ alter table "workflows"."runs" add column if not exists "block_of" uuid references "workflows"."runs" ("id") on delete set null;
165
+ alter table "workflows"."runs" add column if not exists "block_state" text;
166
+ alter table "workflows"."runs" add column if not exists "block_visit" text;
167
+ alter table "workflows"."runs" add column if not exists "output_blanked" boolean;
168
+ create index if not exists "runs_block_of" on "workflows"."runs" ("block_of");
169
+ create unique index if not exists "runs_block_visit" on "workflows"."runs" ("block_of", "block_visit") where "block_of" is not null;
170
+ alter table "workflows"."runs" add column if not exists "block_key" text;
171
+ alter table "workflows"."runs" add column if not exists "block_who" text;
172
+ alter table "workflows"."runs" drop constraint if exists "runs_block_shared";
173
+ alter table "workflows"."runs" add constraint "runs_block_shared" check (("block_key" is null) = ("block_who" is null));
174
+ create unique index if not exists "runs_block_in_flight" on "workflows"."runs" ("block_key", "block_who") where "block_key" is not null and "block_who" is not null and "status" in ('queued', 'running', 'waiting');
175
+ create table if not exists "workflows"."block_waiters" (
176
+ "parent" uuid not null references "workflows"."runs" ("id") on delete cascade,
177
+ "visit" text not null,
178
+ "run_id" uuid not null references "workflows"."runs" ("id") on delete cascade,
179
+ primary key ("parent", "visit")
180
+ );
181
+ create index if not exists "block_waiters_run" on "workflows"."block_waiters" ("run_id");
182
+
183
+ create table if not exists "workflows"."task_tokens" (
184
+ "run_id" uuid not null references "workflows"."runs" ("id") on delete cascade,
185
+ "visit" text not null,
186
+ "state" text not null,
187
+ "token_hash" text not null unique,
188
+ "status" text not null default 'waiting' check ("status" in ('waiting', 'succeeded', 'failed')),
189
+ "started_at" timestamptz not null,
190
+ "handed_off_at" timestamptz,
191
+ "deadline" timestamptz not null,
192
+ "heartbeat_seconds" integer,
193
+ "heartbeat_at" timestamptz,
194
+ "input" jsonb,
195
+ "logs" jsonb not null default '[]'::jsonb,
196
+ "output" jsonb,
197
+ "error" text,
198
+ "cause" text,
199
+ "answered_at" timestamptz,
200
+ primary key ("run_id", "visit")
201
+ );
202
+
203
+ alter table "workflows"."runs" add column if not exists "test_step" text;
204
+ alter table "workflows"."runs" alter column "workflow_id" drop not null;
205
+ alter table "workflows"."runs" drop constraint if exists "runs_workflow_or_test";
206
+ alter table "workflows"."runs" add constraint "runs_workflow_or_test" check (("workflow_id" is null) = ("test_step" is not null));
207
+
208
+ alter table "workflows"."runs" add column if not exists "rerun_from" text;
209
+
210
+ alter table "workflows"."workflows" alter column "state_machine" set default '{"QueryLanguage":"JSONata","StartAt":"","States":{}}'::jsonb;
211
+ alter table "workflows"."workflows" alter column "trigger" set default '{"manual":{}}'::jsonb;
212
+
213
+ -- The layout a definition keeps: each view's own positions, Pins' and Simple's, of the views it knows alone. In each, a
214
+ -- position only under the trigger's key or a state's name as keys write it ('%', '.' and a leading '#' or '$' escaped),
215
+ -- and in Pins also before a dot, as a Break or a Make of that state is kept, so a state removed takes its positions
216
+ -- along; a layout or a view that is no object is none.
217
+ create or replace function "workflows"."kept_layout"(layout jsonb, machine jsonb) returns jsonb
218
+ language sql immutable as $$
219
+ with recursive machines(states) as (
220
+ select case when jsonb_typeof(machine -> 'States') = 'object' then machine -> 'States' else '{}'::jsonb end
221
+ union all
222
+ select case when jsonb_typeof(inner_machine -> 'States') = 'object' then inner_machine -> 'States' else '{}'::jsonb end
223
+ from machines, jsonb_each(machines.states) as state(name, value),
224
+ lateral (select case state.value ->> 'Type' when 'Map' then state.value -> 'ItemProcessor' else state.value -> 'Processor' end as inner_machine) as held
225
+ where jsonb_typeof(state.value) = 'object' and state.value ->> 'Type' in ('Map', 'Block')
226
+ ), names(name) as (
227
+ select regexp_replace(regexp_replace(replace(replace(state.name, '%', '%25'), '.', '%2E'), '^#', '%23'), '^\$', '%24') from machines, jsonb_object_keys(machines.states) as state(name)
228
+ union select '$trigger'
229
+ ), views(view, positions) as (
230
+ select key, case when jsonb_typeof(value) = 'object' then value else '{}'::jsonb end
231
+ from jsonb_each(case when jsonb_typeof(layout) = 'object' then layout else '{}'::jsonb end)
232
+ where key in ('pins', 'simple')
233
+ )
234
+ select coalesce(jsonb_object_agg(views.view, (
235
+ select coalesce(jsonb_object_agg(kept.key, kept.value), '{}'::jsonb)
236
+ from jsonb_each(views.positions) as kept
237
+ where exists (select from names where kept.key = names.name or (views.view = 'pins' and starts_with(kept.key, names.name || '.')))
238
+ )), '{}'::jsonb)
239
+ from views
240
+ $$;
241
+
242
+ create or replace function "workflows"."workflow_version"() returns trigger
243
+ language plpgsql as $$
244
+ begin
245
+ if tg_op = 'INSERT' then
246
+ new.version := 1;
247
+ new.layout := "workflows"."kept_layout"(new.layout, new.state_machine);
248
+ return new;
249
+ end if;
250
+ -- Where nodes were moved is no part of the definition: it makes no new version
251
+ if (new.layout, new.state_machine) is distinct from (old.layout, old.state_machine) then
252
+ new.layout := "workflows"."kept_layout"(new.layout, new.state_machine);
253
+ end if;
254
+ -- The write holds the row, so a save in another transaction numbers its version after this one.
255
+ new.version := case when (new.state_machine, new.trigger, new.execution_order, new.connections, new.trace) is distinct from (old.state_machine, old.trigger, old.execution_order, old.connections, old.trace) then coalesce(old.version, 0) + 1 else old.version end;
256
+ -- Its status, which runs ending and marks as resolved write, is no change to the workflow itself.
257
+ -- Otherwise never the same twice, nor now(): two writes in one transaction, as a batch that saves and publishes, are two ETags.
258
+ if to_jsonb(new) - array['run_status', 'status_run_id', 'status_run_at', 'unresolved_failures', 'resolved_at', 'resolved_by', 'updated_at'] is distinct from to_jsonb(old) - array['run_status', 'status_run_id', 'status_run_at', 'unresolved_failures', 'resolved_at', 'resolved_by', 'updated_at'] then
259
+ new.updated_at := greatest(clock_timestamp(), old.updated_at + interval '1 microsecond');
260
+ end if;
261
+ return new;
262
+ end
263
+ $$;
264
+ drop trigger if exists "workflow_version" on "workflows"."workflows";
265
+ create trigger "workflow_version" before insert or update on "workflows"."workflows" for each row execute function "workflows"."workflow_version"();
266
+
267
+ create or replace function "workflows"."run_version"() returns trigger
268
+ language plpgsql as $$
269
+ declare
270
+ found record;
271
+ begin
272
+ -- A custom step's test run, which only the engine starts, runs that step alone and has no workflow.
273
+ if new.test_step is not null then
274
+ return new;
275
+ end if;
276
+ select w.id, w.version, w.enabled_version, w.tenant_id into found from "workflows"."workflows" w where w.id = new.workflow_id;
277
+ -- Another tenant's workflow is refused as one that is not there, so a start tells nothing of other tenants.
278
+ if found.id is null or new.tenant_id is distinct from found.tenant_id then
279
+ raise exception 'There is no workflow % here', new.workflow_id using errcode = '23503';
280
+ end if;
281
+ if found.version is null then
282
+ raise exception 'Workflow % has no saved version', new.workflow_id using errcode = '23503';
283
+ end if;
284
+ if new.version is not null and not exists (select from "workflows"."versions" v where v.workflow_id = new.workflow_id and v.version = new.version) then
285
+ raise exception 'Workflow % has no version %', new.workflow_id, new.version using errcode = '23503';
286
+ end if;
287
+ -- A replay replays a run of this workflow and tenant.
288
+ if new.replay then
289
+ if not exists (select from "workflows"."runs" r
290
+ where r.id = new.rerun_of and r.workflow_id = new.workflow_id and r.tenant_id is not distinct from new.tenant_id) then
291
+ raise exception 'There is no run % of workflow % here to replay', new.rerun_of, new.workflow_id using errcode = '23503';
292
+ end if;
293
+ end if;
294
+ -- A re-run or a replay tries a fix, so it runs the version saved last. Any other start not naming its version runs
295
+ -- what is published, so a saved change stays out of production until it is, or the version saved last on a workflow that is off.
296
+ if new.version is null then
297
+ new.version := case when new.rerun_of is null then coalesce(found.enabled_version, found.version) else found.version end;
298
+ end if;
299
+ -- Checked on the version the run takes, which a re-run or a replay takes otherwise than a start
300
+ if exists (select from "workflows"."versions" v where v.workflow_id = new.workflow_id and v.version = new.version and v.state_machine -> 'States' = '{}'::jsonb) then
301
+ raise exception 'Workflow % has no steps to run', new.workflow_id using errcode = '23503';
302
+ end if;
303
+ -- A re-run or a replay of a block's run runs that Block's processor again, alone.
304
+ if new.rerun_of is not null then
305
+ select r.input, r.block_state into new.input, new.block_state from "workflows"."runs" r where r.id = new.rerun_of and r.workflow_id = new.workflow_id;
306
+ end if;
307
+ return new;
308
+ end
309
+ $$;
310
+ comment on function "workflows"."run_version"() is 'Protobase workflows 27';
311
+ drop trigger if exists "run_version" on "workflows"."runs";
312
+ create trigger "run_version" before insert on "workflows"."runs" for each row execute function "workflows"."run_version"();
313
+
314
+ alter table "workflows"."workflows" add column if not exists "run_status" text not null default 'no_runs';
315
+ alter table "workflows"."workflows" add column if not exists "status_run_id" uuid;
316
+ alter table "workflows"."workflows" add column if not exists "status_run_at" timestamptz;
317
+ alter table "workflows"."workflows" add column if not exists "unresolved_failures" integer not null default 0;
318
+ alter table "workflows"."workflows" add column if not exists "resolved_at" timestamptz;
319
+ alter table "workflows"."workflows" add column if not exists "resolved_by" text;
320
+ do $$ begin
321
+ if not exists (select 1 from pg_constraint where conname = 'workflows_run_status' and connamespace = 'workflows'::regnamespace) then
322
+ alter table "workflows"."workflows" add constraint "workflows_run_status" check ("run_status" in ('failing', 'unresolved', 'healthy', 'no_runs', 'resolved', 'new_version'));
323
+ end if;
324
+ end $$;
325
+ create table if not exists "workflows"."resolutions" (
326
+ "id" uuid primary key default gen_random_uuid(),
327
+ "workflow_id" uuid not null references "workflows"."workflows" ("id") on delete cascade,
328
+ "resolved_by" text,
329
+ "tenant_id" text,
330
+ "resolved_at" timestamptz not null default now()
331
+ );
332
+ create index if not exists "resolutions_workflow" on "workflows"."resolutions" ("workflow_id", "resolved_at");
333
+ create index if not exists "resolutions_tenant" on "workflows"."resolutions" ("tenant_id");
334
+ create index if not exists "resolutions_resolved_at" on "workflows"."resolutions" ("resolved_at");
335
+ create index if not exists "runs_ended" on "workflows"."runs" ("workflow_id", "finished_at" desc, "id" desc) where "status" in ('succeeded', 'failed');
336
+ -- The runs a workflow's status counts, so its status skips no blocks' runs, of which a run starts many, to find them.
337
+ create index if not exists "runs_ended_top" on "workflows"."runs" ("workflow_id", "finished_at" desc, "id" desc) where "status" in ('succeeded', 'failed') and "block_of" is null;
338
+
339
+ -- The status of a workflow at its version and last mark as resolved, from its last 10 ended runs, blocks' runs left out
340
+ -- (a block's failure is its run's): the run its
341
+ -- reason names is the latest for failing and healthy, the latest failure that counts for unresolved, none for the gray ones.
342
+ create or replace function "workflows"."workflow_status"(workflow uuid, current_version integer, resolved timestamptz)
343
+ returns table ("run_status" text, "status_run_id" uuid, "status_run_at" timestamptz, "unresolved_failures" integer)
344
+ language sql stable as $$
345
+ with ended as (
346
+ select r.id, r.finished_at, r.version, r.status = 'failed' as failed,
347
+ r.status = 'failed' and r.version = current_version and (resolved is null or r.finished_at > resolved) as counts,
348
+ row_number() over (order by r.finished_at desc, r.id desc) as n
349
+ from (select * from "workflows"."runs" where workflow_id = workflow and block_of is null and status in ('succeeded', 'failed') order by finished_at desc, id desc limit 10) r
350
+ ),
351
+ latest as (select * from ended where n = 1),
352
+ failure as (select * from ended where counts order by n limit 1),
353
+ shown as (
354
+ select case
355
+ when l.id is null then 'no_runs'
356
+ when l.counts then 'failing'
357
+ when l.failed and l.version is distinct from current_version then 'new_version'
358
+ when l.failed then 'resolved'
359
+ when f.id is not null then 'unresolved'
360
+ else 'healthy'
361
+ end as status, l.id as latest_id, l.finished_at as latest_at, f.id as failure_id, f.finished_at as failure_at
362
+ from (select) one left join latest l on true left join failure f on true
363
+ )
364
+ select status,
365
+ case status when 'unresolved' then failure_id when 'failing' then latest_id when 'healthy' then latest_id end,
366
+ case status when 'unresolved' then failure_at when 'failing' then latest_at when 'healthy' then latest_at end,
367
+ (select count(*)::integer from ended where counts)
368
+ from shown
369
+ $$;
370
+
371
+ -- A new version or a mark as resolved counts the failures again. Named to run after "workflow_version", which numbers the version.
372
+ create or replace function "workflows"."workflow_version_status"() returns trigger
373
+ language plpgsql as $$
374
+ begin
375
+ if new.version is distinct from old.version or new.resolved_at is distinct from old.resolved_at then
376
+ select st.run_status, st.status_run_id, st.status_run_at, st.unresolved_failures
377
+ into new.run_status, new.status_run_id, new.status_run_at, new.unresolved_failures
378
+ from "workflows"."workflow_status"(new.id, new.version, new.resolved_at) st;
379
+ end if;
380
+ return new;
381
+ end
382
+ $$;
383
+ drop trigger if exists "workflow_version_status" on "workflows"."workflows";
384
+ create trigger "workflow_version_status" before update on "workflows"."workflows" for each row execute function "workflows"."workflow_version_status"();
385
+
386
+ -- A run that ends computes its workflow's status again. The workflow is locked first, in a statement of its own, so a
387
+ -- run of it ending at the same time has committed before the update's snapshot is taken, and the status counts both.
388
+ create or replace function "workflows"."run_status"() returns trigger
389
+ language plpgsql as $$
390
+ begin
391
+ perform 1 from "workflows"."workflows" where id = new.workflow_id for no key update;
392
+ update "workflows"."workflows" w set ("run_status", "status_run_id", "status_run_at", "unresolved_failures") =
393
+ (select st.run_status, st.status_run_id, st.status_run_at, st.unresolved_failures from "workflows"."workflow_status"(w.id, w.version, w.resolved_at) st)
394
+ where w.id = new.workflow_id;
395
+ return null;
396
+ end
397
+ $$;
398
+ drop trigger if exists "run_status" on "workflows"."runs";
399
+ create trigger "run_status" after update of "status" on "workflows"."runs" for each row
400
+ when (new.status in ('succeeded', 'failed') and old.status is distinct from new.status and new.block_of is null) execute function "workflows"."run_status"();
401
+
402
+ -- Marking a workflow resolved: refused for another tenant's workflow as one that is not there, then copied onto it.
403
+ create or replace function "workflows"."resolution"() returns trigger
404
+ language plpgsql as $$
405
+ begin
406
+ if not exists (select from "workflows"."workflows" w where w.id = new.workflow_id and w.tenant_id is not distinct from new.tenant_id) then
407
+ raise exception 'There is no workflow % here', new.workflow_id using errcode = '23503';
408
+ end if;
409
+ update "workflows"."workflows" set "resolved_at" = new.resolved_at, "resolved_by" = new.resolved_by where id = new.workflow_id;
410
+ return null;
411
+ end
412
+ $$;
413
+ drop trigger if exists "resolution" on "workflows"."resolutions";
414
+ create trigger "resolution" after insert on "workflows"."resolutions" for each row execute function "workflows"."resolution"();
415
+
416
+ update "workflows"."workflows" w set ("run_status", "status_run_id", "status_run_at", "unresolved_failures") =
417
+ (select st.run_status, st.status_run_id, st.status_run_at, st.unresolved_failures from "workflows"."workflow_status"(w.id, w.version, w.resolved_at) st);
@@ -24,12 +24,12 @@
24
24
  "zod": "^4.6.5"
25
25
  },
26
26
  "devDependencies": {
27
- "@protobase/cli": "^0.100.21",
28
- "@protobase/config": "^0.100.21",
29
- "@protobase/layout": "^0.100.21",
30
- "@protobase/schema": "^0.100.21",
31
- "@protobase/server": "^0.100.21",
32
- "@protobase/ui": "^0.100.21",
27
+ "@protobase/cli": "^0.100.23",
28
+ "@protobase/config": "^0.100.23",
29
+ "@protobase/layout": "^0.100.23",
30
+ "@protobase/schema": "^0.100.23",
31
+ "@protobase/server": "^0.100.23",
32
+ "@protobase/ui": "^0.100.23",
33
33
  "@types/node": "^22.20.5",
34
34
  "@types/pg": "^8.23.1",
35
35
  "@types/react": "^19.3.0",
@@ -27,12 +27,12 @@
27
27
  "zod": "^4.6.5"
28
28
  },
29
29
  "devDependencies": {
30
- "@protobase/cli": "^0.100.21",
31
- "@protobase/config": "^0.100.21",
32
- "@protobase/layout": "^0.100.21",
33
- "@protobase/schema": "^0.100.21",
34
- "@protobase/server": "^0.100.21",
35
- "@protobase/ui": "^0.100.21",
30
+ "@protobase/cli": "^0.100.23",
31
+ "@protobase/config": "^0.100.23",
32
+ "@protobase/layout": "^0.100.23",
33
+ "@protobase/schema": "^0.100.23",
34
+ "@protobase/server": "^0.100.23",
35
+ "@protobase/ui": "^0.100.23",
36
36
  "@types/node": "^22.20.5",
37
37
  "@types/pg": "^8.23.1",
38
38
  "@types/react": "^19.3.0",
@@ -27,12 +27,12 @@
27
27
  "zod": "^4.6.5"
28
28
  },
29
29
  "devDependencies": {
30
- "@protobase/cli": "^0.100.21",
31
- "@protobase/config": "^0.100.21",
32
- "@protobase/layout": "^0.100.21",
33
- "@protobase/schema": "^0.100.21",
34
- "@protobase/server": "^0.100.21",
35
- "@protobase/ui": "^0.100.21",
30
+ "@protobase/cli": "^0.100.23",
31
+ "@protobase/config": "^0.100.23",
32
+ "@protobase/layout": "^0.100.23",
33
+ "@protobase/schema": "^0.100.23",
34
+ "@protobase/server": "^0.100.23",
35
+ "@protobase/ui": "^0.100.23",
36
36
  "@types/node": "^22.20.5",
37
37
  "@types/pg": "^8.23.1",
38
38
  "@types/react": "^19.3.0",
@@ -27,12 +27,12 @@
27
27
  "zod": "^4.6.5"
28
28
  },
29
29
  "devDependencies": {
30
- "@protobase/cli": "^0.100.21",
31
- "@protobase/config": "^0.100.21",
32
- "@protobase/layout": "^0.100.21",
33
- "@protobase/schema": "^0.100.21",
34
- "@protobase/server": "^0.100.21",
35
- "@protobase/ui": "^0.100.21",
30
+ "@protobase/cli": "^0.100.23",
31
+ "@protobase/config": "^0.100.23",
32
+ "@protobase/layout": "^0.100.23",
33
+ "@protobase/schema": "^0.100.23",
34
+ "@protobase/server": "^0.100.23",
35
+ "@protobase/ui": "^0.100.23",
36
36
  "@types/node": "^22.20.5",
37
37
  "@types/pg": "^8.23.1",
38
38
  "@types/react": "^19.3.0",
@@ -24,12 +24,12 @@
24
24
  "zod": "^4.6.5"
25
25
  },
26
26
  "devDependencies": {
27
- "@protobase/cli": "^0.100.21",
28
- "@protobase/config": "^0.100.21",
29
- "@protobase/layout": "^0.100.21",
30
- "@protobase/schema": "^0.100.21",
31
- "@protobase/server": "^0.100.21",
32
- "@protobase/ui": "^0.100.21",
27
+ "@protobase/cli": "^0.100.23",
28
+ "@protobase/config": "^0.100.23",
29
+ "@protobase/layout": "^0.100.23",
30
+ "@protobase/schema": "^0.100.23",
31
+ "@protobase/server": "^0.100.23",
32
+ "@protobase/ui": "^0.100.23",
33
33
  "@types/node": "^22.20.5",
34
34
  "@types/pg": "^8.23.1",
35
35
  "@types/react": "^19.3.0",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-protobase",
3
- "version": "0.100.21",
3
+ "version": "0.100.23",
4
4
  "description": "npm create protobase: starts a new Protobase project from one of its presets, or on an existing Postgres database.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -28,7 +28,7 @@
28
28
  "postgres": "^3.4.9"
29
29
  },
30
30
  "devDependencies": {
31
- "@protobase/cli": "0.100.21"
31
+ "@protobase/cli": "0.100.23"
32
32
  },
33
33
  "publishConfig": {
34
34
  "access": "public"