-
Notifications
You must be signed in to change notification settings - Fork 1
257 lines (240 loc) · 10.8 KB
/
Copy pathlambda-tests.yml
File metadata and controls
257 lines (240 loc) · 10.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
name: Lambda Tests
on:
push:
branches: [main, develop]
pull_request:
branches: [main, develop]
merge_group:
branches: [main]
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_USER: branch_dev
POSTGRES_PASSWORD: password
POSTGRES_DB: branch_db
ports:
- 5432:5432
options: >-
--health-cmd="pg_isready"
--health-interval=10s
--health-timeout=5s
--health-retries=5
strategy:
fail-fast: false
matrix:
lambda:
- apps/backend/lambdas/auth/
- apps/backend/lambdas/donors/
- apps/backend/lambdas/expenditures/
- apps/backend/lambdas/projects/
- apps/backend/lambdas/reports/
- apps/backend/lambdas/users/
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
# Build the schema the same way production does -- by applying
# db/migrations -- then load the dev seed rows the tests expect. The tests
# also call ensureSchema()/resetData() from db/testkit.ts themselves, so
# this doubles as a per-PR smoke test that migrations apply to a virgin DB.
- name: Apply migrations and seed
working-directory: apps/backend/db
run: npm ci --no-audit --no-fund && npm run migrate && npm run seed
env:
DATABASE_URL: postgres://branch_dev:password@localhost:5432/branch_db
- name: Build shared packages
uses: ./.github/actions/build-shared-packages
- name: Install dependencies
working-directory: ${{ matrix.lambda }}
run: npm ci --legacy-peer-deps
- name: Run tests
working-directory: ${{ matrix.lambda }}
run: |
LAMBDA_NAME=$(basename ${{ matrix.lambda }})
npm run dev &
sleep 5
curl --fail --retry 5 --retry-delay 2 http://localhost:3000/${LAMBDA_NAME}/health
npm test
env:
# No ?options=-csearch_path: the lambdas schema-qualify every query via
# the generated "branch.*" DB keys, and the migrator sets search_path on
# its own connections.
DATABASE_URL: postgres://branch_dev:password@localhost:5432/branch_db
COGNITO_USER_POOL_ID: ${{ secrets.COGNITO_USER_POOL_ID }}
COGNITO_CLIENT_ID: ${{ secrets.COGNITO_CLIENT_ID }}
AWS_REGION: us-east-2
# Cheap checks that do not earn their own runners: migration guards
# (git only), shared-package tests, and a from-scratch migrate+types
# drift check. Sequential on one runner; the lambda matrix stays parallel.
checks:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_USER: branch_dev
POSTGRES_PASSWORD: password
POSTGRES_DB: branch_db
ports:
- 5432:5432
options: >-
--health-cmd="pg_isready"
--health-interval=10s
--health-timeout=5s
--health-retries=5
env:
DATABASE_URL: postgres://branch_dev:password@localhost:5432/branch_db
DIR: apps/backend/db/migrations
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
# push-to-main has nothing meaningful to compare against; resolving to ""
# makes the guards no-op WITHOUT skipping the job, so the lambda-tests gate
# (which requires result == 'success') still passes.
- name: Resolve base sha
id: base
run: |
case "${{ github.event_name }}" in
pull_request) base='${{ github.event.pull_request.base.sha }}' ;;
merge_group) base='${{ github.event.merge_group.base_sha }}' ;;
*) base='' ;;
esac
echo "sha=$base" >> "$GITHUB_OUTPUT"
- name: Applied migrations are immutable
if: steps.base.outputs.sha != ''
env:
BASE: ${{ steps.base.outputs.sha }}
run: |
set -euo pipefail
MB=$(git merge-base "$BASE" HEAD)
# Anything but A(dded) under migrations/ is a violation: M modified,
# D deleted, R renamed, C copied, T typechange. --find-renames is
# explicit so a rename reports R (not D+A) regardless of the runner's
# diff.renames default.
violations=$(git diff --name-status --find-renames "$MB" HEAD -- "$DIR" | grep -vE '^A[[:space:]]' || true)
if [ -n "$violations" ]; then
echo "::error::Migrations already on main can never be modified, renamed or deleted -- someone has already run them. Add a NEW migration that fixes the old one."
printf '%s\n' "$violations"
exit 1
fi
- name: New migrations must be named correctly
if: steps.base.outputs.sha != ''
env:
BASE: ${{ steps.base.outputs.sha }}
run: |
set -euo pipefail
export LC_ALL=C
MB=$(git merge-base "$BASE" HEAD)
added=$(git diff --diff-filter=A --name-only "$MB" HEAD -- "$DIR" | xargs -r -n1 basename | sort || true)
last=$(git ls-tree -r --name-only "$MB" -- "$DIR" | xargs -r -n1 basename | sort | tail -1 || true)
fail=0
for f in $added; do
if ! echo "$f" | grep -qE '^(0000_baseline_schema|[0-9]{14}_[a-z0-9_]+)\.sql$'; then
echo "::error file=$DIR/$f::name must be YYYYMMDDHHMMSS_lower_snake_case.sql -- use 'make new-migration NAME=...' so it is generated for you"
fail=1
fi
# Out-of-order merges are allowed at runtime (the migrator sets
# allowUnorderedMigrations), so this is advice, not a failure.
if [ -n "${last:-}" ] && [ "$f" \< "$last" ]; then
echo "::warning file=$DIR/$f::sorts before $last, which is already on main, so it will be applied out of order. Harmless, but renaming it to a current timestamp keeps the history readable."
fi
done
exit $fail
- name: No statements that would break the deployed code
if: steps.base.outputs.sha != ''
env:
BASE: ${{ steps.base.outputs.sha }}
run: |
set -euo pipefail
MB=$(git merge-base "$BASE" HEAD)
files=$(git diff --diff-filter=A --name-only "$MB" HEAD -- "$DIR" || true)
fail=0
for f in $files; do
[ -f "$f" ] || continue
# '-- allow-destructive: <reason>' opts a file out, deliberately loudly.
# Read from the raw file: this opt-out is itself a comment.
if grep -qi '^-- allow-destructive:' "$f"; then
echo "::warning file=$f::opted out of the destructive-SQL check"
continue
fi
# Scan SQL, not prose. `make new-migration` scaffolds a header that
# says "CREATE INDEX CONCURRENTLY / VACUUM will not work here", and
# matching that failed every generated migration. Blanking comments
# rather than dropping the lines keeps grep -n line numbers honest.
sql=$(sed 's/--.*//' "$f")
if printf '%s\n' "$sql" | grep -inE '\b(DROP[[:space:]]+SCHEMA|DROP[[:space:]]+DATABASE|TRUNCATE|DROP[[:space:]]+TABLE|DROP[[:space:]]+COLUMN)\b'; then
echo "::error file=$f::destructive statement. Migrations run against production BEFORE the new lambda code deploys, so the currently deployed code would hit the changed schema. See the expand/contract rules in apps/backend/db/README.md. Add '-- allow-destructive: <reason>' only for a contract-phase migration whose expand phase is already live."
fail=1
fi
if printf '%s\n' "$sql" | grep -inE 'CONCURRENTLY'; then
echo "::error file=$f::CONCURRENTLY cannot run inside a transaction, and the migrator wraps the run in one. This database is tiny -- use a plain CREATE INDEX."
fail=1
fi
if printf '%s\n' "$sql" | grep -inE '^[[:space:]]*INSERT[[:space:]]+INTO[[:space:]]+(branch\.)?users\b'; then
echo "::warning file=$f::inserting users in a migration puts rows in PRODUCTION. Seeded users with a NULL cognito_sub are claimable by POST /auth/register, so this can hand someone an account. Dev seed rows belong in apps/backend/db/seed.sql."
fi
done
exit $fail
- name: Build and test shared packages
uses: ./.github/actions/build-shared-packages
with:
test: true
- name: Install db tooling
run: npm ci --prefix apps/backend/db --no-audit --no-fund
- name: Apply every migration from scratch
run: npm run migrate --prefix apps/backend/db
- name: Ledger must be clean afterwards
run: |
set -o pipefail
npm run migrate:status --prefix apps/backend/db | tee status.log
if grep -q 'PENDING' status.log; then
echo "::error::migrations still pending after a successful migrate run"
exit 1
fi
- name: Re-running migrations must be a no-op
run: npm run migrate --prefix apps/backend/db
- name: Seeds must apply on top of the migrated schema
run: npm run seed --prefix apps/backend/db
# Drift check: the committed types must be what these migrations produce.
- name: Committed types must match the migrations
run: |
cp shared/types/db-types.d.ts /tmp/committed.d.ts
npm run types --prefix apps/backend/db
if ! diff -u /tmp/committed.d.ts shared/types/db-types.d.ts; then
echo "::error::shared/types/db-types.d.ts is stale. The Schema Change Checks workflow pushes a fix to this PR automatically -- wait for its commit, then this check goes green. Or run 'make types' in apps/backend and commit the result."
exit 1
fi
# NOTE: do not rename this job. infrastructure/github/main.tf lists
# "lambda-tests" as a required status check on main; renaming it here would
# silently disable the gate rather than fail loudly.
lambda-tests:
name: lambda-tests
needs: [test, checks]
if: always()
runs-on: ubuntu-latest
steps:
- name: Check test results
run: |
if [ "${{ needs.test.result }}" != "success" ]; then
echo "Lambda tests failed or were cancelled"
exit 1
fi
if [ "${{ needs.checks.result }}" != "success" ]; then
echo "Shared package, migration, or guard checks failed or were cancelled"
exit 1
fi
echo "All lambda and migration checks passed!"