Skip to content

Commit 6f58898

Browse files
authored
feat(ci): guard migrations.targetVersion against silent skip (#2771)
## Summary - Add `hack/check-migrations-target.sh` that fails when the latest numbered file under `packages/core/platform/images/migrations/migrations/` is not matched by a bumped `migrations.targetVersion` in `packages/core/platform/values.yaml`. - Wire it into `unit-tests` via a new `migrations-target-check` Makefile target so PR CI catches the regression mechanically. `run-migrations.sh:34` iterates `seq $CURRENT_VERSION $((TARGET_VERSION - 1))`, so a migration whose number equals the configured `targetVersion` is silently skipped. The cost is state corruption on upgrade — a class of bug worth eliminating with a 10-line check rather than relying on reviewer vigilance. ## Test plan - [x] `make migrations-target-check` passes on `main` HEAD (`targetVersion=43`, latest migration `42`, `43 >= 43`). - [x] Synthetic failure: `touch packages/core/platform/images/migrations/migrations/99` makes the script exit 1 with a clear error pointing at `packages/core/platform/values.yaml` and the required bump. - [x] Tested on macOS (BSD `ls` + mikefarah `yq`). ```release-note NONE ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Chores** * Added a CI validation step to unit tests to ensure `migrations.targetVersion` is kept in sync with newly added database migrations, preventing upgrade scripts from skipping new migration steps. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents 9223f2e + 3380bb4 commit 6f58898

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

‎Makefile‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: manifests assets unit-tests helm-unit-tests bats-unit-tests rd-presets-check test-controllers preflight
1+
.PHONY: manifests assets unit-tests helm-unit-tests bats-unit-tests rd-presets-check migrations-target-check test-controllers preflight
22

33
include hack/common-envs.mk
44

@@ -107,7 +107,7 @@ test:
107107
make -C packages/core/testing apply
108108
make -C packages/core/testing e2e
109109

110-
unit-tests: helm-unit-tests bats-unit-tests go-unit-tests rd-presets-check test-check-readiness
110+
unit-tests: helm-unit-tests bats-unit-tests go-unit-tests rd-presets-check test-check-readiness migrations-target-check
111111

112112
helm-unit-tests:
113113
hack/helm-unit-tests.sh
@@ -119,6 +119,13 @@ helm-unit-tests:
119119
rd-presets-check:
120120
hack/check-rd-presets.sh
121121

122+
# Catch the off-by-one where migrations/<N> is added but
123+
# migrations.targetVersion in packages/core/platform/values.yaml is not
124+
# bumped to >= N+1. run-migrations.sh loops `seq $$CURRENT $$((TARGET-1))`,
125+
# so the new migration is silently skipped on every cluster upgrade.
126+
migrations-target-check:
127+
hack/check-migrations-target.sh
128+
122129
# Scoped go test over the cozystack-api surface that this repo owns. Kept
123130
# narrow intentionally - running `go test ./...` pulls in generated code
124131
# round-trip suites whose behavior depends on tool versions outside this

‎hack/check-migrations-target.sh‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env bash
2+
# Verify packages/core/platform/values.yaml `migrations.targetVersion`
3+
# is at least 1 greater than the highest-numbered file in
4+
# packages/core/platform/images/migrations/migrations/.
5+
#
6+
# Catches the regression where a PR adds a new migration script but
7+
# forgets to bump targetVersion: run-migrations.sh loops
8+
# `seq $CURRENT_VERSION $((TARGET_VERSION - 1))`, so migration N never
9+
# runs unless targetVersion is >= N+1.
10+
set -euo pipefail
11+
12+
MDIR="packages/core/platform/images/migrations/migrations"
13+
VALUES="packages/core/platform/values.yaml"
14+
15+
if [ ! -d "$MDIR" ]; then
16+
echo "ERROR: migrations dir not found: $MDIR" >&2
17+
exit 1
18+
fi
19+
if [ ! -f "$VALUES" ]; then
20+
echo "ERROR: values file not found: $VALUES" >&2
21+
exit 1
22+
fi
23+
24+
MAX_N=0
25+
for f in "$MDIR"/*; do
26+
name=${f##*/}
27+
[[ "$name" =~ ^[0-9]+$ ]] || continue
28+
[ "$name" -gt "$MAX_N" ] && MAX_N=$name
29+
done
30+
if [ "$MAX_N" -eq 0 ]; then
31+
echo "ERROR: no numbered migrations found under $MDIR" >&2
32+
exit 1
33+
fi
34+
REQUIRED=$((MAX_N + 1))
35+
36+
TARGET=$(yq -r '.migrations.targetVersion' "$VALUES")
37+
if [ -z "$TARGET" ] || [ "$TARGET" = "null" ]; then
38+
echo "ERROR: migrations.targetVersion is not set in $VALUES" >&2
39+
exit 1
40+
fi
41+
42+
if [ "$TARGET" -lt "$REQUIRED" ]; then
43+
cat >&2 <<EOF
44+
ERROR: latest migration is $MAX_N but $VALUES sets migrations.targetVersion=$TARGET (need >= $REQUIRED).
45+
46+
run-migrations.sh loops 'seq \$CURRENT_VERSION \$((TARGET_VERSION - 1))', so migration $MAX_N never runs unless targetVersion is bumped.
47+
48+
Fix: edit $VALUES and set migrations.targetVersion: $REQUIRED
49+
EOF
50+
exit 1
51+
fi
52+
53+
echo "OK: migrations.targetVersion=$TARGET >= $REQUIRED (latest migration $MAX_N)"

0 commit comments

Comments
 (0)