Repository navigation
415 lines (392 loc) · 19.8 KB
/
Copy pathden-db-migration-test.yml
File metadata and controls
415 lines (392 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
name: Den DB migration test
# Tests a pull request's Drizzle migrations against a short-lived PlanetScale
# copy of production, seeded (Data Branching) from the latest backup, then
# deletes the copy. Production itself is still migrated only by
# den-db-migrate.yml after merge: deploy requests carry schema, not the data
# statements in our migrations or the __drizzle_migrations journal, so this
# workflow never opens one.
#
# Flow per run (every push to the PR restarts it):
# 1. Delete this PR's previous copy, if one is left over.
# 2. Seed migration-test-pr-<number> from the latest backup of main
# (about 22 minutes; the whole run takes about 25).
# 3. Run db:migrate and db:reconcile-permissions, exactly as production does.
# 4. On a migration failure: fail the check and post the (redacted) error to
# Slack.
# 5. Delete the copy, also when the run fails or is cancelled. Closing the PR
# deletes any leftover.
#
# The copy is not shrunk. PlanetScale creates seeded copies at production's
# size whatever --cluster-size says, and resizing one from Metal takes longer
# than the copy lives (over 20 minutes; measured on #7113), so it would only
# add time. A run costs well under a dollar at full size.
#
# Safety:
# - Organization members' (MEMBER/OWNER) same-repository pull requests only,
# and only when the author triggered the run. Forks, outside collaborators
# and bots are skipped, or fail before any secret is read when someone other
# than the author pushed.
# - Migration SQL from the PR runs with write access to a copy of production
# data. Give the den-db-migration-test environment required reviewers so a
# maintainer reads the migration before it runs, as prod-data-dry-run does.
# - This repository is public, so logs are public. The migration output is
# never printed; the log shows only MySQL error codes. The full error, with
# quoted values redacted, goes to Slack.
# - The PlanetScale token reaches only the steps that call pscale; the branch
# password only reaches the migrate step. Dependencies are installed before
# any credential exists. Every action is pinned to a commit SHA.
#
# Needs, on the den-db-migration-test environment:
# secrets PLANETSCALE_MIGRATION_TEST_TOKEN_ID and
# PLANETSCALE_MIGRATION_TEST_TOKEN: a service token limited to the Den
# database with read_database, read_branch, create_branch, delete_branch,
# delete_production_branch, read_backups, restore_production_branch_backup,
# connect_branch and connect_production_branch (the seeded copy counts as
# production). No write_database: the workflow never changes database
# settings.
# secret SLACK_BOT_TOKEN (chat:write) and variable
# SLACK_DEN_DB_ALERTS_CHANNEL_ID.
# variables PLANETSCALE_MIGRATION_TEST_ORG and
# PLANETSCALE_MIGRATION_TEST_DATABASE.
# main must be deletion-protected (verified before each privileged step) and
# Data Branching must be enabled on the database.
on:
pull_request:
types: [opened, synchronize, reopened, closed]
paths:
- "ee/packages/den-db/drizzle/**"
- ".github/workflows/den-db-migration-test.yml"
- "scripts/dry-run-branch-guard.sh"
- "scripts/dry-run-branch-guard.test.sh"
permissions:
contents: read
# A new push, or closing or merging the PR, cancels the run in progress; its
# always() cleanup still deletes the copy, and the closed-PR job is a backstop.
concurrency:
group: den-db-migration-test-${{ github.event.pull_request.number }}
cancel-in-progress: true
env:
PLANETSCALE_ORG: ${{ vars.PLANETSCALE_MIGRATION_TEST_ORG }}
DATABASE: ${{ vars.PLANETSCALE_MIGRATION_TEST_DATABASE }}
SOURCE_BRANCH: main
BRANCH: migration-test-pr-${{ github.event.pull_request.number }}
COPY_BRANCH_PATTERN: ^migration-test-pr-[0-9]+$
jobs:
migration-test:
name: Test migrations on a production copy
# Organization members' own pull requests only, with the same identity
# checks as pr-proof.yml. author_association comes from GitHub, not the PR:
# repository write access alone does not make an outside collaborator a
# MEMBER. Everyone else is skipped before any secret is read.
if: >-
github.repository == 'different-ai/openwork' &&
github.event.action != 'closed' &&
github.event.repository.owner.type == 'Organization' &&
github.event.pull_request.head.repo.full_name == github.repository &&
github.event.pull_request.base.repo.full_name == github.repository &&
github.event.pull_request.head.repo.id == github.event.repository.id &&
github.event.pull_request.base.repo.id == github.event.repository.id &&
github.event.pull_request.head.repo.fork == false &&
github.event.pull_request.base.repo.fork == false &&
github.event.pull_request.user.type == 'User' &&
contains(fromJSON('["MEMBER", "OWNER"]'), github.event.pull_request.author_association)
runs-on: blacksmith-4vcpu-ubuntu-2204
# Up to 45 minutes of seeding plus migrations, with room to spare.
timeout-minutes: 75
environment:
name: den-db-migration-test
deployment: false
steps:
# The author check above says who opened the PR, not who pushed this
# commit. Someone else's push (or re-run) must not run their SQL under
# the member's name, so only the author can trigger it.
- name: Check who triggered the run
env:
PR_AUTHOR: ${{ github.event.pull_request.user.login }}
TRIGGERING_ACTOR: ${{ github.triggering_actor }}
run: |
set -euo pipefail
if [ "$TRIGGERING_ACTOR" != "$PR_AUTHOR" ]; then
echo "::error::Migrations run on a production copy only for the PR author's own pushes. $TRIGGERING_ACTOR triggered this run; $PR_AUTHOR can re-run it after reviewing the change."
exit 1
fi
echo "Triggered by the PR author, an organization member."
- name: Check configuration
env:
TOKEN_ID: ${{ secrets.PLANETSCALE_MIGRATION_TEST_TOKEN_ID }}
TOKEN: ${{ secrets.PLANETSCALE_MIGRATION_TEST_TOKEN }}
run: |
set -euo pipefail
missing=""
[ -z "$TOKEN_ID" ] && missing="$missing PLANETSCALE_MIGRATION_TEST_TOKEN_ID"
[ -z "$TOKEN" ] && missing="$missing PLANETSCALE_MIGRATION_TEST_TOKEN"
[ -z "$PLANETSCALE_ORG" ] && missing="$missing PLANETSCALE_MIGRATION_TEST_ORG"
[ -z "$DATABASE" ] && missing="$missing PLANETSCALE_MIGRATION_TEST_DATABASE"
if [ -n "$missing" ]; then
echo "::error::Missing on the den-db-migration-test environment:$missing"
exit 1
fi
- name: Checkout
uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6
with:
persist-credentials: false
# Install before any database credential exists, so dependency lifecycle
# scripts never see the branch password or the PlanetScale token.
- name: Setup Node
uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6
with:
node-version: 24.22.1
- name: Setup pnpm
uses: pnpm/action-setup@b906affcce14559ad1aafd4ab0e942779e9f58b1 # v4
with:
version: 11.4.0
- name: Install dependencies
run: pnpm install --frozen-lockfile --filter "@openwork-ee/den-db..."
- name: Test branch safety guards
run: bash scripts/dry-run-branch-guard.test.sh
- name: Setup pscale CLI
uses: planetscale/setup-pscale-action@b6a50ee45b4b24944e1d8de6e57b3a5f6476a1af # v1
- name: Delete this PR's previous copy
env:
PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_MIGRATION_TEST_TOKEN_ID }}
PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_MIGRATION_TEST_TOKEN }}
run: |
set -euo pipefail
source scripts/dry-run-branch-guard.sh
assert_dry_run_name "$BRANCH"
# Listing must succeed: an auth/network failure is not proof that
# the copy is absent.
branches="$(pscale branch list "$DATABASE" --org "$PLANETSCALE_ORG" --format json)"
if printf '%s' "$branches" | jq -e --arg name "$BRANCH" 'any(.[]; .name == $name)' > /dev/null; then
delete_dry_run_branch "$BRANCH"
else
echo "No previous copy"
fi
- name: Seed a copy from the latest backup
env:
PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_MIGRATION_TEST_TOKEN_ID }}
PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_MIGRATION_TEST_TOKEN }}
run: |
set -euo pipefail
source scripts/dry-run-branch-guard.sh
assert_dry_run_name "$BRANCH"
assert_source_protected
echo "Seeding $BRANCH from the latest backup of $SOURCE_BRANCH"
# pscale's --wait gives up after 10 minutes, and seeding production
# takes longer, so poll the branch's ready flag instead.
pscale branch create "$DATABASE" "$BRANCH" --org "$PLANETSCALE_ORG" \
--from "$SOURCE_BRANCH" --seed-data > /dev/null
for attempt in $(seq 1 90); do
copy="$(pscale api "organizations/$PLANETSCALE_ORG/databases/$DATABASE/branches/$BRANCH")" || copy='{}'
if jq -e '.ready == true' > /dev/null <<<"$copy"; then
echo "Copy ready after about $((attempt / 2)) minutes: $(jq -r '"\(.cluster_name) metal=\(.metal) production=\(.production)"' <<<"$copy")"
assert_copy_branch "$BRANCH"
exit 0
fi
[ $((attempt % 4)) -eq 0 ] && echo "Still seeding (state: $(jq -r '.state // "unknown"' <<<"$copy"))"
sleep 30
done
echo "::error::$BRANCH was not ready after 45 minutes."
exit 1
# Production runs migrations with safe migrations briefly off. A new copy
# normally has it off already; turning it off needs write_database, which
# could also turn it off on main, so the token only needs that permission
# if a copy ever inherits the setting.
- name: Allow direct DDL on the copy
env:
PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_MIGRATION_TEST_TOKEN_ID }}
PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_MIGRATION_TEST_TOKEN }}
run: |
set -euo pipefail
source scripts/dry-run-branch-guard.sh
assert_copy_branch "$BRANCH"
copy="$(pscale api "organizations/$PLANETSCALE_ORG/databases/$DATABASE/branches/$BRANCH")"
if jq -e '.safe_migrations != true' > /dev/null <<<"$copy"; then
echo "Safe migrations already off on $BRANCH"
exit 0
fi
if ! state="$(pscale branch safe-migrations disable "$DATABASE" "$BRANCH" --org "$PLANETSCALE_ORG" --format json)"; then
echo "::error::$BRANCH inherited safe migrations and the token cannot turn it off (needs write_database)."
exit 1
fi
if ! jq -e '.safe_migrations == false' > /dev/null <<<"$state"; then
echo "::error::PlanetScale did not confirm safe migrations are off on $BRANCH."
exit 1
fi
echo "Safe migrations off on $BRANCH"
- name: Create a branch password
env:
PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_MIGRATION_TEST_TOKEN_ID }}
PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_MIGRATION_TEST_TOKEN }}
PASSWORD_NAME: migration-test-${{ github.run_id }}
run: |
set -euo pipefail
source scripts/dry-run-branch-guard.sh
assert_source_protected
assert_copy_branch "$BRANCH"
json="$(pscale password create "$DATABASE" "$BRANCH" "$PASSWORD_NAME" --org "$PLANETSCALE_ORG" --role admin --ttl 2h --format json)"
host="$(printf '%s' "$json" | jq -r '.access_host_url // .database_branch.access_host_url')"
username="$(printf '%s' "$json" | jq -r '.username')"
password="$(printf '%s' "$json" | jq -r '.plain_text')"
echo "::add-mask::$username"
echo "::add-mask::$password"
# Vitess connections name the keyspace, not the PlanetScale database
# (they differ for Den). Use the copy's default keyspace, or its only
# one.
keyspaces="$(pscale api "organizations/$PLANETSCALE_ORG/databases/$DATABASE/branches/$BRANCH/keyspaces")"
keyspace="$(jq -r '(.data // .) | (map(select(.default == true)) + (if length == 1 then . else [] end)) | .[0].name // empty' <<<"$keyspaces")"
if [ -z "$keyspace" ]; then
echo "::error::Could not pick the keyspace to migrate on $BRANCH (no default and more than one keyspace)."
exit 1
fi
{
echo "COPY_DATABASE_HOST=$host"
echo "COPY_DATABASE_USERNAME=$username"
echo "COPY_DATABASE_PASSWORD=$password"
echo "COPY_DATABASE_KEYSPACE=$keyspace"
} >> "$GITHUB_ENV"
echo "Password created for $BRANCH"
- name: Run migrations on the copy
id: migrate
env:
DATABASE_HOST: ${{ env.COPY_DATABASE_HOST }}
DATABASE_USERNAME: ${{ env.COPY_DATABASE_USERNAME }}
DATABASE_PASSWORD: ${{ env.COPY_DATABASE_PASSWORD }}
DATABASE_NAME: ${{ env.COPY_DATABASE_KEYSPACE }}
MIGRATE_LOG: ${{ runner.temp }}/migrate.log
run: |
set -uo pipefail
: > "$MIGRATE_LOG"
# Output can contain production values, so it goes to a file, never
# to this public log. Retry only while the safe-migrations change is
# still reaching every gateway.
run_quietly() {
local attempt
for attempt in $(seq 1 8); do
echo "\$ $*" >> "$MIGRATE_LOG"
if "$@" >> "$MIGRATE_LOG" 2>&1; then
return 0
fi
if ! tail -n 50 "$MIGRATE_LOG" | grep -q "direct DDL is disabled"; then
return 1
fi
echo "DDL not allowed everywhere yet; retrying (attempt $attempt)"
sleep 15
done
return 1
}
if run_quietly pnpm --filter @openwork-ee/den-db db:migrate \
&& run_quietly pnpm --filter @openwork-ee/den-db db:reconcile-permissions; then
echo "Migrations and permission reconciliation succeeded on $BRANCH." | tee -a "$GITHUB_STEP_SUMMARY"
exit 0
fi
codes="$(grep -oE '(errno [0-9]+|sqlstate [0-9A-Z]+|ER_[A-Z_]+|code: .?[A-Z_]{3,}.?)' "$MIGRATE_LOG" | sort -u | tr '\n' ' ')"
echo "::error::Migrations failed on a copy of production. Error codes: ${codes:-none found}. The redacted error was posted to Slack."
{
echo "Migrations failed on a copy of production data."
echo ""
echo "Error codes: ${codes:-none found}"
} >> "$GITHUB_STEP_SUMMARY"
exit 1
- name: Post the failure to Slack
if: failure() && steps.migrate.outcome == 'failure'
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
SLACK_CHANNEL: ${{ vars.SLACK_DEN_DB_ALERTS_CHANNEL_ID }}
MIGRATE_LOG: ${{ runner.temp }}/migrate.log
PR_URL: ${{ github.event.pull_request.html_url }}
PR_TITLE: ${{ github.event.pull_request.title }}
PR_AUTHOR: ${{ github.event.pull_request.user.login }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
run: |
set -euo pipefail
if [ -z "$SLACK_BOT_TOKEN" ] || [ -z "$SLACK_CHANNEL" ]; then
echo "::warning::SLACK_BOT_TOKEN or SLACK_DEN_DB_ALERTS_CHANNEL_ID is not set; no Slack alert sent."
exit 0
fi
# Redact quoted literals (values from rows) and keep the last part of
# the output, where the error is.
excerpt="$(sed -E "s/'[^']*'/'…'/g; s/\"[^\"]*\"/\"…\"/g" "$MIGRATE_LOG" | tail -c 2500)"
payload="$(jq -n \
--arg channel "$SLACK_CHANNEL" \
--arg pr_url "$PR_URL" \
--arg pr_title "$PR_TITLE" \
--arg author "$PR_AUTHOR" \
--arg run_url "$RUN_URL" \
--arg excerpt "$excerpt" \
'{
channel: $channel,
text: ("Den migration failed on a production copy: " + $pr_title),
blocks: [
{ type: "section", text: { type: "mrkdwn", text: (":rotating_light: *Den migration failed on a copy of production*\n<" + $pr_url + "|" + $pr_title + "> by " + $author + " · <" + $run_url + "|workflow run>") } },
{ type: "section", text: { type: "mrkdwn", text: ("```" + $excerpt + "```") } }
]
}')"
response="$(curl -sS -X POST https://slack.com/api/chat.postMessage \
-H "Authorization: Bearer $SLACK_BOT_TOKEN" \
-H "Content-Type: application/json; charset=utf-8" \
--data "$payload")"
if ! jq -e '.ok == true' > /dev/null <<<"$response"; then
echo "::warning::Slack did not accept the alert: $(jq -r '.error // "unknown error"' <<<"$response")"
fi
- name: Delete the copy
if: always()
env:
PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_MIGRATION_TEST_TOKEN_ID }}
PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_MIGRATION_TEST_TOKEN }}
run: |
set -euo pipefail
if ! command -v pscale > /dev/null; then
echo "pscale was never set up, so no copy was created"
exit 0
fi
source scripts/dry-run-branch-guard.sh
assert_dry_run_name "$BRANCH"
branches="$(pscale branch list "$DATABASE" --org "$PLANETSCALE_ORG" --format json)"
if printf '%s' "$branches" | jq -e --arg name "$BRANCH" 'any(.[]; .name == $name)' > /dev/null; then
delete_dry_run_branch "$BRANCH"
else
echo "No copy to delete"
fi
# Backstop for a copy left behind by a run that could not clean up.
cleanup:
name: Delete the copy when the PR closes
# Same identity checks as migration-test: only members' PRs ever get a copy.
if: >-
github.repository == 'different-ai/openwork' &&
github.event.action == 'closed' &&
github.event.repository.owner.type == 'Organization' &&
github.event.pull_request.head.repo.full_name == github.repository &&
github.event.pull_request.base.repo.full_name == github.repository &&
github.event.pull_request.head.repo.id == github.event.repository.id &&
github.event.pull_request.base.repo.id == github.event.repository.id &&
github.event.pull_request.head.repo.fork == false &&
github.event.pull_request.base.repo.fork == false &&
github.event.pull_request.user.type == 'User' &&
contains(fromJSON('["MEMBER", "OWNER"]'), github.event.pull_request.author_association)
runs-on: blacksmith-4vcpu-ubuntu-2204
timeout-minutes: 15
environment:
name: den-db-migration-test
deployment: false
steps:
- name: Checkout
uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6
with:
persist-credentials: false
- name: Setup pscale CLI
uses: planetscale/setup-pscale-action@b6a50ee45b4b24944e1d8de6e57b3a5f6476a1af # v1
- name: Delete the copy
env:
PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_MIGRATION_TEST_TOKEN_ID }}
PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_MIGRATION_TEST_TOKEN }}
run: |
set -euo pipefail
source scripts/dry-run-branch-guard.sh
assert_dry_run_name "$BRANCH"
branches="$(pscale branch list "$DATABASE" --org "$PLANETSCALE_ORG" --format json)"
if printf '%s' "$branches" | jq -e --arg name "$BRANCH" 'any(.[]; .name == $name)' > /dev/null; then
delete_dry_run_branch "$BRANCH"
else
echo "No copy to delete"
fi