forked from ceejaylaboratory/AnchorPoint
-
Notifications
You must be signed in to change notification settings - Fork 0
219 lines (193 loc) · 7.03 KB
/
Copy pathmigration-integrity.yml
File metadata and controls
219 lines (193 loc) · 7.03 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
name: Database Migration Integrity Check
on:
pull_request:
paths:
- 'backend/prisma/**'
- 'backend/scripts/migration-integrity-checker.ts'
- '.github/workflows/migration-integrity.yml'
push:
branches:
- main
- staging
paths:
- 'backend/prisma/**'
permissions:
contents: read
issues: write
pull-requests: write
jobs:
migration-integrity:
name: Check Migration Integrity
runs-on: ubuntu-latest
services:
postgres:
image: postgres:15
env:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: prisma_password
POSTGRES_DB: testdb
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for migration comparison
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: 'package-lock.json'
- name: Install dependencies
working-directory: backend
env:
DATABASE_URL: "postgresql://prisma:prisma_password@localhost:5432/testdb"
run: |
npm install --ignore-scripts
npm install @prisma/debug @prisma/engines-version --no-save
npx prisma generate
- name: Setup test database
working-directory: backend
env:
DATABASE_URL: "postgresql://prisma:prisma_password@localhost:5432/testdb"
run: |
npx prisma migrate deploy
- name: Run migration integrity checks
working-directory: backend
env:
DATABASE_URL: "postgresql://prisma:prisma_password@localhost:5432/testdb"
run: npx ts-node scripts/migration-integrity-checker.ts
- name: Check for schema drift
working-directory: backend
env:
DATABASE_URL: "postgresql://prisma:prisma_password@localhost:5432/testdb"
run: |
# Use migrate status to detect drift between applied migrations and schema.prisma.
# If there are unapplied migrations or drift, it exits with a non-zero code.
echo "Checking for schema drift..."
npx prisma migrate status 2>&1 | tee migrate-status.txt
if grep -q "drift" migrate-status.txt || grep -q "have not yet been applied" migrate-status.txt; then
echo "⚠️ Schema drift or unapplied migrations detected!"
exit 1
else
echo "✅ No schema drift detected"
fi
- name: Validate migration files
working-directory: backend
run: |
# Check that all migrations have corresponding SQL files
for dir in prisma/migrations/*/; do
if [ ! -f "${dir}migration.sql" ]; then
echo "❌ Missing migration.sql in ${dir}"
exit 1
fi
done
echo "✅ All migration files are valid"
- name: Generate migration report
if: always()
working-directory: backend
env:
DATABASE_URL: "postgresql://prisma:prisma_password@localhost:5432/testdb"
run: |
echo "## Migration Integrity Report" > migration-report.md
echo "" >> migration-report.md
echo "### Recent Migrations" >> migration-report.md
ls -la prisma/migrations/ >> migration-report.md || true
echo "" >> migration-report.md
echo "### Schema Status" >> migration-report.md
npx prisma migrate status >> migration-report.md || true
- name: Upload migration report
if: always()
uses: actions/upload-artifact@v4
with:
name: migration-report
path: backend/migration-report.md
retention-days: 30
- name: Comment PR with results
if: github.event_name == 'pull_request'
continue-on-error: true
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const report = fs.readFileSync('backend/migration-report.md', 'utf8');
try {
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: report
});
} catch (error) {
core.warning(`Unable to comment migration report on PR: ${error.message}`);
}
- name: Cleanup test artifacts
if: always()
run: rm -f backend/migrate-status.txt backend/migration-report.md
staging-migration-test:
name: Test Migration on Staging Clone
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/staging' || github.base_ref == 'staging'
services:
postgres:
image: postgres:15
env:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: prisma_password
POSTGRES_DB: staging_testdb
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: 'package-lock.json'
- name: Install dependencies
working-directory: backend
env:
DATABASE_URL: "postgresql://prisma:prisma_password@localhost:5432/staging_testdb"
run: |
npm install --ignore-scripts
npm install @prisma/debug @prisma/engines-version --no-save
npx prisma generate
- name: Create staging database clone
working-directory: backend
env:
DATABASE_URL: "postgresql://prisma:prisma_password@localhost:5432/staging_testdb"
run: |
# Create a test database with production-like schema
npx prisma migrate deploy
# Seed with test data if needed
echo "✅ Staging database clone created"
- name: Test migration rollback
working-directory: backend
run: |
# Get the latest migration
LATEST_MIGRATION=$(ls -t prisma/migrations | head -1)
echo "Testing rollback for: $LATEST_MIGRATION"
# Note: Prisma doesn't have built-in rollback, but we can test the process
echo "⚠️ Manual rollback testing required for production"
- name: Verify data integrity
working-directory: backend
env:
DATABASE_URL: "postgresql://prisma:prisma_password@localhost:5432/staging_testdb"
run: |
# Run queries to verify data integrity after migration
npx prisma studio --browser none &
sleep 5
echo "✅ Data integrity check completed"