@sentry/junior 0.291.0 → 0.293.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.
- package/dist/{adapter-zphAlWE2.js → adapter-CMJfmHso.js} +1 -1
- package/dist/{admin-B3WR7Y6e.js → admin-uuecrKuD.js} +2 -2
- package/dist/{agent-runner-ke-hOiuv.js → agent-runner-DHq1pjUg.js} +283 -155
- package/dist/api/schema.d.ts +2 -2
- package/dist/api/schema.js +3 -3
- package/dist/api.d.ts +1 -1
- package/dist/api.js +43 -38
- package/dist/{app-C47PT68y.d.ts → app-fWzCHFIb.d.ts} +2 -2
- package/dist/app.d.ts +1 -1
- package/dist/app.js +422 -394
- package/dist/{assistant-reply-BV8SZaVd.js → assistant-reply-CIApSfor.js} +1 -1
- package/dist/{automation-CrsYnvdV.js → automation-BE3z19VN.js} +71 -22
- package/dist/{background-compaction-queue-QDH_JS3D.js → background-compaction-queue-D7ncdEa6.js} +4 -4
- package/dist/{briefs-Bdv9BG5J.js → briefs-NpgMEFh1.js} +2 -2
- package/dist/{catalog-runtime-yGnL9ywD.js → catalog-runtime-DwbTyNRD.js} +1 -1
- package/dist/cli/chat.js +15 -15
- package/dist/cli/check.js +3 -3
- package/dist/cli/main.js +3 -3
- package/dist/cli/plugins.js +4 -4
- package/dist/cli/snapshot-create.js +4 -4
- package/dist/cli/upgrade.d.ts +1 -1
- package/dist/cli/upgrade.js +4 -3
- package/dist/{config-UsaBOQgC.js → config-B4b-NXH5.js} +2 -2
- package/dist/{connections-CePSRCoR.js → connections-Dz_AQg5p.js} +6 -6
- package/dist/{conversation-pending-messages-Cd7ShwBs.js → conversation-pending-messages-1Kad1Mif.js} +4 -1
- package/dist/{conversation-privacy-B3wmawmz.js → conversation-privacy-DRsSgLGO.js} +1 -1
- package/dist/{credential-sync-LNRLXZCn.js → credential-sync-ClCnnGVj.js} +13 -13
- package/dist/{db-7glZCI18.d.ts → db-BbpttyKH.d.ts} +1 -1
- package/dist/{db-c__lw9GH.js → db-CoA7_Xqu.js} +3 -3
- package/dist/{executor-N8-D5Gvv.js → executor-SWyHhit0.js} +51 -15
- package/dist/instrumentation.js +2 -2
- package/dist/{logging-DbXHuhac.js → logging-BN5drSEZ.js} +1 -1
- package/dist/{message-D1MEh8iR.js → message-D4qjpW1C.js} +9 -9
- package/dist/nitro.d.ts +1 -1
- package/dist/nitro.js +2 -2
- package/dist/{oauth-callback-server-DUtqhYwP.js → oauth-callback-server-Xi-mTLOV.js} +2 -2
- package/dist/{projection-D7XadqRY.js → projection-DXl4ftyL.js} +140 -50
- package/dist/{prompt-CzEV4BF4.js → prompt-Dmx6IfWZ.js} +3 -3
- package/dist/{registry-CTtGtXZP.js → registry-DosVt9f9.js} +1 -1
- package/dist/{resolve-nUuQ8yyH.js → resolve-BDjWJC9s.js} +3 -3
- package/dist/{result-Cr9tUdfS.js → result-CaAFpf4B.js} +8 -8
- package/dist/{runner-D7NMbBim.js → runner-h96Mkss6.js} +7 -7
- package/dist/{sandbox-egress-signals-BJAyfzOD.js → sandbox-egress-signals-rK91BW7i.js} +1 -1
- package/dist/{schema-CC2RxDAV.d.ts → schema-CHsBmXIj.d.ts} +246 -45
- package/dist/{schema-CVV5r-qP.js → schema-C_v8wb80.js} +2 -2
- package/dist/{service-account-_ml5D4Tk.js → service-account-BpGpScaT.js} +3 -3
- package/dist/{service-account-CP1oBWTc.js → service-account-WUMVuqVL.js} +3 -3
- package/dist/{session-stop-BbsyIbK2.js → session-stop-xYK15HUu.js} +20 -41
- package/dist/{skills-CayJ8-Bj.js → skills-DHApyCjj.js} +10 -9
- package/dist/{spawn-CktAopv_.js → spawn-BQQrwGNe.js} +5 -5
- package/dist/{stop-BUzUJuN_.js → stop-CBeD6_Gs.js} +34 -28
- package/dist/{store-lotbeXq3.js → store-xtibD98L.js} +3 -3
- package/dist/{task-runner-BinM1l5g.js → task-runner-BYPN4BEO.js} +65 -90
- package/dist/{turn-failure-response-DH5nE6HJ.js → turn-failure-response-jNVl00MD.js} +7 -7
- package/dist/{validation-I0CqX5NC.js → validation-DGeB_moV.js} +1 -1
- package/dist/{version-C3sVz3bv.js → version-C6nqU3Qc.js} +1 -1
- package/dist/version.js +1 -1
- package/dist/{viewer-so1MPU10.js → viewer-9KLQibjn.js} +3 -3
- package/dist/{warmup-BH9HRBCI.js → warmup-7S6z068f.js} +56 -24
- package/dist/{work-Cw6Fzvfz.js → work-xwX6unUE.js} +11 -11
- package/dist/{worker-qEykG39-.js → worker-CiCCfwd4.js} +6 -6
- package/migrations/0095_web_linked_slack_identity_repair.sql +252 -0
- package/migrations/0096_automation_task_kind.sql +75 -0
- package/migrations/meta/0095_snapshot.json +4452 -0
- package/migrations/meta/0096_snapshot.json +4490 -0
- package/migrations/meta/_journal.json +14 -0
- package/package.json +5 -5
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
-- Before #3066, a dashboard post into a Slack thread copied the poster's
|
|
2
|
+
-- email onto the thread starter's Slack Identity when Slack had returned no
|
|
3
|
+
-- email for the starter. The copied email linked that Identity to the
|
|
4
|
+
-- poster's User. The link made the poster a Participant of every root that
|
|
5
|
+
-- the Identity started or sent a Turn in.
|
|
6
|
+
--
|
|
7
|
+
-- Evidence: a Slack instruction stores the Actor from the Slack profile in
|
|
8
|
+
-- `provenance.actor`. Slack is the only source that may give a Slack Identity
|
|
9
|
+
-- its email, so an instruction with the Identity's Slack user and email
|
|
10
|
+
-- proves that email. Nothing else proves it. In the same way, an instruction
|
|
11
|
+
-- with the Identity's Slack user and name proves that the name came from
|
|
12
|
+
-- Slack.
|
|
13
|
+
--
|
|
14
|
+
-- A candidate is a Slack user Identity when all of these are true:
|
|
15
|
+
-- - It has a User and a verified email, and no evidence proves that email.
|
|
16
|
+
-- - The User's web Identity, with that same email, wrote an event in a
|
|
17
|
+
-- Conversation of a root that the Slack Identity started.
|
|
18
|
+
--
|
|
19
|
+
-- Slack emails are unique in a workspace, so one User has at most one
|
|
20
|
+
-- correct Slack Identity in each team. Repair a candidate only when evidence
|
|
21
|
+
-- proves exactly one other Slack Identity of its User in its team. Leave
|
|
22
|
+
-- every other candidate as it is. Without that proof, the data cannot tell a
|
|
23
|
+
-- victim from a person whose own link came through this path, or whose
|
|
24
|
+
-- events retention removed.
|
|
25
|
+
--
|
|
26
|
+
-- One statement repairs every table, so each part reads the same rows. The
|
|
27
|
+
-- migration reports its counts with `RAISE INFO`, and `junior upgrade`
|
|
28
|
+
-- prints them. The report has no identifiers.
|
|
29
|
+
DO $$
|
|
30
|
+
DECLARE
|
|
31
|
+
"report" record;
|
|
32
|
+
BEGIN
|
|
33
|
+
-- junior:bounded-backfill: changes only rows of damaged Identities, their Users, and the roots they started or sent Turns in. #3065 found few of these.
|
|
34
|
+
WITH "slack_actors" AS (
|
|
35
|
+
SELECT DISTINCT
|
|
36
|
+
"e"."payload"->'provenance'->'actor'->>'userId' AS "subject",
|
|
37
|
+
lower(btrim("e"."payload"->'provenance'->'actor'->>'email')) AS "email",
|
|
38
|
+
btrim("e"."payload"->'provenance'->'actor'->>'fullName') AS "full_name"
|
|
39
|
+
FROM "junior_conversation_events" AS "e"
|
|
40
|
+
WHERE "e"."type" = 'user_message'
|
|
41
|
+
AND "e"."payload"->'provenance'->>'authority' = 'instruction'
|
|
42
|
+
AND "e"."payload"->'provenance'->'actor'->>'platform' = 'slack'
|
|
43
|
+
),
|
|
44
|
+
"slack_evidence" AS (
|
|
45
|
+
SELECT DISTINCT "subject", "email"
|
|
46
|
+
FROM "slack_actors"
|
|
47
|
+
WHERE "email" IS NOT NULL
|
|
48
|
+
),
|
|
49
|
+
"proven" AS (
|
|
50
|
+
SELECT "slack"."id", "slack"."user_id", "slack"."provider_tenant_id",
|
|
51
|
+
"slack"."display_name"
|
|
52
|
+
FROM "junior_identities" AS "slack"
|
|
53
|
+
INNER JOIN "slack_evidence" AS "ev"
|
|
54
|
+
ON "ev"."subject" = "slack"."provider_subject_id"
|
|
55
|
+
AND "ev"."email" = "slack"."email_normalized"
|
|
56
|
+
WHERE "slack"."kind" = 'user'
|
|
57
|
+
AND "slack"."provider" = 'slack'
|
|
58
|
+
AND "slack"."user_id" IS NOT NULL
|
|
59
|
+
),
|
|
60
|
+
"web_posts" AS (
|
|
61
|
+
SELECT DISTINCT "web"."user_id", "web"."email_normalized",
|
|
62
|
+
coalesce("c"."root_conversation_id", "c"."conversation_id") AS "root_id"
|
|
63
|
+
FROM "junior_identities" AS "web"
|
|
64
|
+
INNER JOIN "junior_conversation_events" AS "posted"
|
|
65
|
+
ON "posted"."actor_identity_id" = "web"."id"
|
|
66
|
+
INNER JOIN "junior_conversations" AS "c"
|
|
67
|
+
ON "c"."conversation_id" = "posted"."conversation_id"
|
|
68
|
+
WHERE "web"."kind" = 'user'
|
|
69
|
+
AND "web"."provider" = 'junior'
|
|
70
|
+
AND "web"."user_id" IS NOT NULL
|
|
71
|
+
),
|
|
72
|
+
"candidates" AS (
|
|
73
|
+
SELECT DISTINCT "slack"."id" AS "identity_id", "slack"."user_id",
|
|
74
|
+
"slack"."provider_tenant_id"
|
|
75
|
+
FROM "junior_identities" AS "slack"
|
|
76
|
+
INNER JOIN "junior_conversations" AS "root"
|
|
77
|
+
ON "root"."actor_identity_id" = "slack"."id"
|
|
78
|
+
AND "root"."parent_conversation_id" IS NULL
|
|
79
|
+
INNER JOIN "web_posts" AS "w"
|
|
80
|
+
ON "w"."root_id" = "root"."conversation_id"
|
|
81
|
+
AND "w"."user_id" = "slack"."user_id"
|
|
82
|
+
AND "w"."email_normalized" = "slack"."email_normalized"
|
|
83
|
+
WHERE "slack"."kind" = 'user'
|
|
84
|
+
AND "slack"."provider" = 'slack'
|
|
85
|
+
AND "slack"."user_id" IS NOT NULL
|
|
86
|
+
AND "slack"."email_verified"
|
|
87
|
+
AND "slack"."id" NOT IN (SELECT "id" FROM "proven")
|
|
88
|
+
),
|
|
89
|
+
"damaged" AS (
|
|
90
|
+
SELECT "candidate"."identity_id", "candidate"."user_id"
|
|
91
|
+
FROM "candidates" AS "candidate"
|
|
92
|
+
WHERE (
|
|
93
|
+
SELECT count(*)
|
|
94
|
+
FROM "proven" AS "kept"
|
|
95
|
+
WHERE "kept"."user_id" = "candidate"."user_id"
|
|
96
|
+
AND "kept"."provider_tenant_id" = "candidate"."provider_tenant_id"
|
|
97
|
+
) = 1
|
|
98
|
+
),
|
|
99
|
+
-- Roots where the link made the poster a Participant: roots that the damaged
|
|
100
|
+
-- Identity started, and roots where it sent a Turn.
|
|
101
|
+
"affected_roots" AS (
|
|
102
|
+
SELECT "d"."user_id", "root"."conversation_id" AS "root_id"
|
|
103
|
+
FROM "damaged" AS "d"
|
|
104
|
+
INNER JOIN "junior_conversations" AS "root"
|
|
105
|
+
ON "root"."actor_identity_id" = "d"."identity_id"
|
|
106
|
+
AND "root"."parent_conversation_id" IS NULL
|
|
107
|
+
UNION
|
|
108
|
+
SELECT "d"."user_id",
|
|
109
|
+
coalesce("c"."root_conversation_id", "c"."conversation_id") AS "root_id"
|
|
110
|
+
FROM "damaged" AS "d"
|
|
111
|
+
INNER JOIN "junior_conversation_events" AS "e"
|
|
112
|
+
ON "e"."actor_identity_id" = "d"."identity_id"
|
|
113
|
+
INNER JOIN "junior_conversations" AS "c"
|
|
114
|
+
ON "c"."conversation_id" = "e"."conversation_id"
|
|
115
|
+
),
|
|
116
|
+
-- Keep a row when the User started the root or wrote in it through an
|
|
117
|
+
-- Identity that is not damaged.
|
|
118
|
+
"removed_participants" AS (
|
|
119
|
+
DELETE FROM "junior_conversation_participants" AS "p"
|
|
120
|
+
USING "affected_roots" AS "a"
|
|
121
|
+
WHERE "p"."user_id" = "a"."user_id"
|
|
122
|
+
AND "p"."root_conversation_id" = "a"."root_id"
|
|
123
|
+
AND NOT EXISTS (
|
|
124
|
+
SELECT 1
|
|
125
|
+
FROM "junior_conversations" AS "root"
|
|
126
|
+
INNER JOIN "junior_identities" AS "starter"
|
|
127
|
+
ON "starter"."id" = "root"."actor_identity_id"
|
|
128
|
+
WHERE "root"."conversation_id" = "a"."root_id"
|
|
129
|
+
AND "starter"."user_id" = "a"."user_id"
|
|
130
|
+
AND "starter"."id" NOT IN (SELECT "identity_id" FROM "damaged")
|
|
131
|
+
)
|
|
132
|
+
AND NOT EXISTS (
|
|
133
|
+
SELECT 1
|
|
134
|
+
FROM "junior_conversations" AS "c"
|
|
135
|
+
INNER JOIN "junior_conversation_events" AS "e"
|
|
136
|
+
ON "e"."conversation_id" = "c"."conversation_id"
|
|
137
|
+
INNER JOIN "junior_identities" AS "author"
|
|
138
|
+
ON "author"."id" = "e"."actor_identity_id"
|
|
139
|
+
WHERE coalesce("c"."root_conversation_id", "c"."conversation_id") = "a"."root_id"
|
|
140
|
+
AND "author"."user_id" = "a"."user_id"
|
|
141
|
+
AND "author"."id" NOT IN (SELECT "identity_id" FROM "damaged")
|
|
142
|
+
)
|
|
143
|
+
RETURNING 1
|
|
144
|
+
),
|
|
145
|
+
-- The link gave the poster's User the starter's name when the User had none.
|
|
146
|
+
-- Later dashboard posts copy the User name to the poster's web Identity, and
|
|
147
|
+
-- the next save of that Identity copies it back to the User. So the web
|
|
148
|
+
-- Identity name is not evidence. A name is the starter's when Slack evidence
|
|
149
|
+
-- proves it for the damaged Identity and no proven Slack Identity of the User
|
|
150
|
+
-- has it. Clear it on the User and on the User's web Identities. The next
|
|
151
|
+
-- save of a kept Identity gives the User a name again.
|
|
152
|
+
"stolen_names" AS (
|
|
153
|
+
SELECT DISTINCT "u"."id" AS "user_id", "u"."display_name"
|
|
154
|
+
FROM "damaged" AS "d"
|
|
155
|
+
INNER JOIN "junior_identities" AS "slack" ON "slack"."id" = "d"."identity_id"
|
|
156
|
+
INNER JOIN "junior_users" AS "u" ON "u"."id" = "d"."user_id"
|
|
157
|
+
WHERE "u"."display_name" = "slack"."display_name"
|
|
158
|
+
AND EXISTS (
|
|
159
|
+
SELECT 1
|
|
160
|
+
FROM "slack_actors" AS "a"
|
|
161
|
+
WHERE "a"."subject" = "slack"."provider_subject_id"
|
|
162
|
+
AND "a"."full_name" = "slack"."display_name"
|
|
163
|
+
)
|
|
164
|
+
AND NOT EXISTS (
|
|
165
|
+
SELECT 1
|
|
166
|
+
FROM "proven" AS "kept"
|
|
167
|
+
WHERE "kept"."user_id" = "d"."user_id"
|
|
168
|
+
AND "kept"."display_name" = "u"."display_name"
|
|
169
|
+
)
|
|
170
|
+
),
|
|
171
|
+
"renamed_users" AS (
|
|
172
|
+
UPDATE "junior_users" AS "u"
|
|
173
|
+
SET "display_name" = NULL,
|
|
174
|
+
"updated_at" = now()
|
|
175
|
+
FROM "stolen_names" AS "n"
|
|
176
|
+
WHERE "u"."id" = "n"."user_id"
|
|
177
|
+
RETURNING 1
|
|
178
|
+
),
|
|
179
|
+
"renamed_web_identities" AS (
|
|
180
|
+
UPDATE "junior_identities" AS "web"
|
|
181
|
+
SET "display_name" = NULL,
|
|
182
|
+
"updated_at" = now()
|
|
183
|
+
FROM "stolen_names" AS "n"
|
|
184
|
+
WHERE "web"."user_id" = "n"."user_id"
|
|
185
|
+
AND "web"."kind" = 'user'
|
|
186
|
+
AND "web"."provider" = 'junior'
|
|
187
|
+
AND "web"."display_name" = "n"."display_name"
|
|
188
|
+
RETURNING 1
|
|
189
|
+
),
|
|
190
|
+
-- The detail report of these roots shows the root Actor and Participants.
|
|
191
|
+
"touched_roots" AS (
|
|
192
|
+
UPDATE "junior_conversations" AS "root"
|
|
193
|
+
SET "detail_version" = "root"."detail_version" + 1
|
|
194
|
+
WHERE "root"."conversation_id" IN (SELECT "root_id" FROM "affected_roots")
|
|
195
|
+
RETURNING 1
|
|
196
|
+
),
|
|
197
|
+
-- Put the Identity back to its state before the link. Slack gave it no
|
|
198
|
+
-- email, so a later Slack Turn cannot link it again. The same write copied
|
|
199
|
+
-- the poster's name when Slack gave none, so clear a name that matches the
|
|
200
|
+
-- poster's web Identity unless Slack evidence proves it. The next Slack Turn
|
|
201
|
+
-- of the Identity saves its own name again.
|
|
202
|
+
"unlinked" AS (
|
|
203
|
+
UPDATE "junior_identities" AS "i"
|
|
204
|
+
SET "user_id" = NULL,
|
|
205
|
+
"email" = NULL,
|
|
206
|
+
"email_normalized" = NULL,
|
|
207
|
+
"email_verified" = false,
|
|
208
|
+
"display_name" = CASE
|
|
209
|
+
WHEN EXISTS (
|
|
210
|
+
SELECT 1
|
|
211
|
+
FROM "junior_identities" AS "web"
|
|
212
|
+
WHERE "web"."user_id" = "d"."user_id"
|
|
213
|
+
AND "web"."provider" = 'junior'
|
|
214
|
+
AND "web"."display_name" = "i"."display_name"
|
|
215
|
+
) AND NOT EXISTS (
|
|
216
|
+
SELECT 1
|
|
217
|
+
FROM "slack_actors" AS "a"
|
|
218
|
+
WHERE "a"."subject" = "i"."provider_subject_id"
|
|
219
|
+
AND "a"."full_name" = "i"."display_name"
|
|
220
|
+
) THEN NULL
|
|
221
|
+
ELSE "i"."display_name"
|
|
222
|
+
END,
|
|
223
|
+
"updated_at" = now()
|
|
224
|
+
FROM "damaged" AS "d"
|
|
225
|
+
WHERE "i"."id" = "d"."identity_id"
|
|
226
|
+
RETURNING 1
|
|
227
|
+
)
|
|
228
|
+
SELECT
|
|
229
|
+
(SELECT count(*) FROM "candidates") AS "candidates",
|
|
230
|
+
(SELECT count(*) FROM "unlinked") AS "repaired",
|
|
231
|
+
(SELECT count(*) FROM "candidates" AS "c"
|
|
232
|
+
WHERE NOT EXISTS (
|
|
233
|
+
SELECT 1 FROM "proven" AS "p"
|
|
234
|
+
WHERE "p"."user_id" = "c"."user_id"
|
|
235
|
+
AND "p"."provider_tenant_id" = "c"."provider_tenant_id"
|
|
236
|
+
)) AS "skipped_unproven",
|
|
237
|
+
(SELECT count(*) FROM "candidates" AS "c"
|
|
238
|
+
WHERE (
|
|
239
|
+
SELECT count(*) FROM "proven" AS "p"
|
|
240
|
+
WHERE "p"."user_id" = "c"."user_id"
|
|
241
|
+
AND "p"."provider_tenant_id" = "c"."provider_tenant_id"
|
|
242
|
+
) > 1) AS "skipped_many_proven",
|
|
243
|
+
(SELECT count(*) FROM "removed_participants") AS "participants",
|
|
244
|
+
(SELECT count(*) FROM "touched_roots") AS "roots",
|
|
245
|
+
(SELECT count(*) FROM "renamed_users") AS "users_renamed",
|
|
246
|
+
(SELECT count(*) FROM "renamed_web_identities") AS "web_identities_renamed"
|
|
247
|
+
INTO "report";
|
|
248
|
+
RAISE INFO '0095 Slack Identity repair: % candidates, % repaired, % skipped with no proven Slack Identity, % skipped with more than one, % Participant rows removed, % roots updated, % User names and % web Identity names cleared',
|
|
249
|
+
"report"."candidates", "report"."repaired", "report"."skipped_unproven",
|
|
250
|
+
"report"."skipped_many_proven", "report"."participants", "report"."roots",
|
|
251
|
+
"report"."users_renamed", "report"."web_identities_renamed";
|
|
252
|
+
END $$;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
-- An Automation task is an instruction or a skill. The `instruction` column
|
|
2
|
+
-- holds only an instruction, and the new `skill` column holds only a skill.
|
|
3
|
+
ALTER TABLE "junior_automations" ALTER COLUMN "instruction" DROP NOT NULL;--> statement-breakpoint
|
|
4
|
+
ALTER TABLE "junior_automations" ADD COLUMN "skill" text;--> statement-breakpoint
|
|
5
|
+
-- An Automation with a blank instruction could not run. Block it until its
|
|
6
|
+
-- creator writes an instruction or chooses a skill.
|
|
7
|
+
-- junior:bounded-backfill: changes only active or paused Automations with a blank instruction, which cannot run, so few rows match.
|
|
8
|
+
UPDATE "junior_automations"
|
|
9
|
+
SET
|
|
10
|
+
"status" = 'blocked',
|
|
11
|
+
"status_reason" = 'This automation has no instruction. Write one or choose a skill, then resume it.'
|
|
12
|
+
WHERE "record"->'task' ? 'text'
|
|
13
|
+
AND btrim("record"->'task'->>'text', E' \t\r\n') = ''
|
|
14
|
+
AND "status" IN ('active', 'paused');--> statement-breakpoint
|
|
15
|
+
-- Rewrite the old `{ text }` task as an instruction task. A blank
|
|
16
|
+
-- instruction gets a placeholder, because an instruction cannot be empty.
|
|
17
|
+
-- junior:bounded-backfill: runs once over the Automations of one install, which are few, and each row changes once.
|
|
18
|
+
WITH "old" AS (
|
|
19
|
+
SELECT
|
|
20
|
+
"id",
|
|
21
|
+
coalesce(
|
|
22
|
+
nullif(btrim("record"->'task'->>'text', E' \t\r\n'), ''),
|
|
23
|
+
'No instruction was saved.'
|
|
24
|
+
) AS "instruction"
|
|
25
|
+
FROM "junior_automations"
|
|
26
|
+
WHERE "record"->'task' ? 'text'
|
|
27
|
+
)
|
|
28
|
+
UPDATE "junior_automations"
|
|
29
|
+
SET
|
|
30
|
+
"instruction" = "old"."instruction",
|
|
31
|
+
"record" = jsonb_set(
|
|
32
|
+
"record",
|
|
33
|
+
'{task}',
|
|
34
|
+
jsonb_build_object('kind', 'instruction', 'instruction', "old"."instruction")
|
|
35
|
+
)
|
|
36
|
+
FROM "old"
|
|
37
|
+
WHERE "junior_automations"."id" = "old"."id";--> statement-breakpoint
|
|
38
|
+
-- Saved versions use the same `task` object, so history and diffs have one shape.
|
|
39
|
+
-- junior:bounded-backfill: runs once over the saved versions of one install, which are few, and each row changes once.
|
|
40
|
+
UPDATE "junior_automation_versions"
|
|
41
|
+
SET "definition" = ("definition" - 'instruction') || jsonb_build_object(
|
|
42
|
+
'task',
|
|
43
|
+
jsonb_build_object(
|
|
44
|
+
'kind', 'instruction',
|
|
45
|
+
'instruction', coalesce(
|
|
46
|
+
nullif(btrim("definition"->>'instruction', E' \t\r\n'), ''),
|
|
47
|
+
'No instruction was saved.'
|
|
48
|
+
)
|
|
49
|
+
)
|
|
50
|
+
)
|
|
51
|
+
WHERE "definition" ? 'instruction';--> statement-breakpoint
|
|
52
|
+
-- Finds the Automations that run a skill. Before this migration runs,
|
|
53
|
+
-- junior upgrade builds the marked index with CREATE INDEX CONCURRENTLY, so
|
|
54
|
+
-- the build below does not block writes.
|
|
55
|
+
DO $$
|
|
56
|
+
BEGIN
|
|
57
|
+
-- Even CREATE INDEX IF NOT EXISTS takes a write-blocking table lock.
|
|
58
|
+
IF to_regclass('junior_automations_skill_idx') IS NULL THEN
|
|
59
|
+
-- junior:concurrent-index
|
|
60
|
+
CREATE INDEX "junior_automations_skill_idx" ON "junior_automations" USING btree ("skill","created_at_ms","id") WHERE "junior_automations"."status" <> 'deleted' AND "junior_automations"."skill" IS NOT NULL;
|
|
61
|
+
END IF;
|
|
62
|
+
|
|
63
|
+
-- A failed concurrent build can leave an index that exists but cannot be used.
|
|
64
|
+
IF NOT EXISTS (
|
|
65
|
+
SELECT 1 FROM pg_index
|
|
66
|
+
WHERE indexrelid = 'junior_automations_skill_idx'::regclass
|
|
67
|
+
AND indrelid = 'junior_automations'::regclass
|
|
68
|
+
AND indisvalid AND indisready
|
|
69
|
+
) THEN
|
|
70
|
+
RAISE EXCEPTION 'The Automation skill index is not valid and ready. Repair the concurrent build before running junior upgrade.';
|
|
71
|
+
END IF;
|
|
72
|
+
END $$;--> statement-breakpoint
|
|
73
|
+
-- The backfill above makes every row pass. NOT VALID skips the scan under
|
|
74
|
+
-- lock and still checks every new write. A later migration validates it.
|
|
75
|
+
ALTER TABLE "junior_automations" ADD CONSTRAINT "junior_automations_task_check" CHECK (("junior_automations"."instruction" is null) <> ("junior_automations"."skill" is null)) NOT VALID;
|