Repository navigation
Commit 9a4d01c
authored
fix(migrations): opt-in dirty auto-heal, raise timeout default, failure alarm (#1124)
* fix(migrations): opt-in dirty auto-heal, raise timeout default, failure alarm
Harden the lazy-on-connect migration path that caused a prod outage: a slow
migration (index build on a growing table) blew the 20s timeout mid-run,
golang-migrate left schema_migrations dirty, and since dirty is terminal every
later cold start fail-opened and served 500s on any query needing the
unapplied columns (070-073 never applied).
Three changes:
1. Default-on dirty auto-heal (internal/database/postgres/migrations/migrate.go).
New maybeAutoHealDirty runs after maybeForceMigrationVersion and before
Up(): when the DB is dirty it Force()s the CURRENT recorded version to clear
the flag, then Up() re-applies the pending tail, so a cold start self-recovers
instead of staying broken until a manual force (the multi-hour outage shape).
Enabled by default; CUDLY_MIGRATION_AUTOHEAL=false is the escape hatch.
Forces the current version, NEVER lower: forcing below already-applied seed
migrations re-runs guards that raise on a second run (e.g. 000059 "Purchaser
already exists"). The app ALWAYS starts: if auto-heal still can't reach head,
the error propagates to ensureDB which fail-opens (existing behavior) and the
alarm fires -- never a crash-loop. CUDLY_FORCE_MIGRATION_VERSION still takes
precedence. Relies on migrations being idempotent (documented + tested).
2. Raise defaultMigrationsTimeout 20s -> 120s (internal/server/app.go), still
well under the 300s Lambda limit, so a normal index build can't be killed
mid-run. CUDLY_MIGRATION_TIMEOUT override unchanged.
3. CloudWatch migration-failure alarm (terraform/modules/compute/aws/lambda).
Log metric filter on the "Migration failed" line + alarm, since the app
fail-opens and AWS/Lambda Errors stays clean during a broken migration.
Notification target optional via alarm_sns_topic_arn (default []); reuses
the existing alarm/metric-filter patterns, no new SNS infra.
Fix latent migration bug surfaced by the new idempotency test: migration
000067 widened savings_snapshots.account_id BEFORE dropping the materialized
views that depend on it, so the full stack failed to reach head on a fresh DB
("cannot alter type of a column used by a view"). Moved the view DROPs ahead
of the ALTER COLUMN. This moved the migrations integration suite from 4/37 to
28/42 passing; the remaining failures are pre-existing and unrelated (users
role column, savings-plans split assertions).
Tests:
- TestMigrations_FullStackIdempotent (integration): migrate to head twice,
second run is a clean no-op and not dirty.
- TestMigrations_AutoHealDirty (integration): dirty DB self-heals by default
and with =true; CUDLY_MIGRATION_AUTOHEAL=false disables it so a dirty DB
errors with the dirty flag intact.
- TestResolveMigrationsTimeout (unit): default is 120s, honors the override,
falls back on invalid/non-positive input.
Spec specs/migration-resilience.md updated for the new default, the auto-heal
flag, precedence, and the alarm.
* fix(migrations): extract migrator recovery hooks to fix gocyclo
RunMigrations hit cyclomatic complexity 11 (gocyclo budget is 10) after
the default-on dirty auto-heal added a second pre-Up error branch, failing
the pre-commit CI hook. Extract migrator creation plus the operator-force
and auto-heal hooks into newMigratorWithRecovery, dropping RunMigrations to
9 while preserving hook ordering and error propagation. The helper closes
the migrator on its own error paths so callers never receive a
half-initialized migrator.
* test(migrations): enforce all migrations are transactional + address CR
Add a static guard test that reads every *.up.sql / *.down.sql under the
migrations dir and fails if any file contains a statement PostgreSQL refuses
to run inside a transaction block (CREATE/DROP INDEX CONCURRENTLY, REINDEX,
VACUUM, ALTER SYSTEM, CREATE/DROP DATABASE, CREATE TABLESPACE, REFRESH
MATERIALIZED VIEW CONCURRENTLY).
golang-migrate runs each migration file in one implicit transaction, so
forbidding non-transactional statements guarantees a failed migration rolls
back atomically with no partial schema. This is orthogonal to the dirty-flag
auto-heal in this PR; it closes the partial-apply hole.
The scanner strips SQL comments and dollar-quoted function/DO bodies before
matching, so CONCURRENTLY inside a stored function (e.g. migration 000003's
refresh_savings_materialized_views) is correctly treated as benign. Files may
opt out via the marker comment "-- migrate:no-transaction"; such files are
skipped with a loud warning and MUST be applied via the deploy-time runner
(issue #1125), never the in-request auto-migrate path.
Also address the CodeRabbit nitpick on specs/migration-resilience.md:
clarify that alarm_sns_topic_arn is a list of SNS topic ARNs.1 parent e97187a commit 9a4d01c
11 files changed
Lines changed: 671 additions & 47 deletions
File tree
- internal
- database/postgres/migrations
- server
- specs
- terraform/modules/compute/aws/lambda
Lines changed: 16 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
38 | 52 | | |
39 | 53 | | |
40 | 54 | | |
| |||
71 | 85 | | |
72 | 86 | | |
73 | 87 | | |
74 | | - | |
| 88 | + | |
| 89 | + | |
75 | 90 | | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | 91 | | |
81 | 92 | | |
82 | 93 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
42 | 38 | | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | 39 | | |
54 | 40 | | |
| 41 | + | |
55 | 42 | | |
56 | 43 | | |
57 | 44 | | |
| |||
85 | 72 | | |
86 | 73 | | |
87 | 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 | + | |
88 | 121 | | |
89 | 122 | | |
90 | 123 | | |
| |||
292 | 325 | | |
293 | 326 | | |
294 | 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 | + | |
295 | 415 | | |
296 | 416 | | |
297 | 417 | | |
| |||
Lines changed: 116 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
Lines changed: 53 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
0 commit comments