-
Notifications
You must be signed in to change notification settings - Fork 53
294 lines (252 loc) · 12.4 KB
/
_migrate-database.yml
File metadata and controls
294 lines (252 loc) · 12.4 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
name: Migrate Database
on:
workflow_call:
inputs:
azure_environment:
required: true
type: string
cluster_location_acronym:
required: true
type: string
service_principal_id:
required: true
type: string
subscription_id:
required: true
type: string
database_name:
required: true
type: string
relative_project_path:
description: "Relative path to the project containing the migrations"
required: true
type: string
relative_startup_project:
description: "Relative path to the startup project"
required: true
type: string
db_context:
required: true
type: string
apply_migrations:
required: true
type: boolean
outputs:
has_migrations_to_apply:
value: ${{ jobs.plan-migrations.outputs.has_migrations_to_apply }}
concurrency:
group: ${{ inputs.database_name }}-${{ inputs.azure_environment }}-migrations
cancel-in-progress: false
jobs:
plan-migrations:
name: Plan
runs-on: ubuntu-24.04
outputs:
has_migrations_to_apply: ${{ steps.generate-migration-script.outputs.has_migrations_to_apply }}
migration_json: ${{ steps.generate-migration-script.outputs.migration_json }}
env:
UNIQUE_PREFIX: ${{ vars.UNIQUE_PREFIX }}
TENANT_ID: ${{ vars.TENANT_ID }}
CLUSTER_RESOURCE_GROUP_NAME: ${{ vars.UNIQUE_PREFIX }}-${{ inputs.azure_environment }}-${{ inputs.cluster_location_acronym }}
POSTGRES_SERVER_NAME: ${{ vars.UNIQUE_PREFIX }}-${{ inputs.azure_environment }}-${{ inputs.cluster_location_acronym }}
POSTGRES_HOST: ${{ vars.UNIQUE_PREFIX }}-${{ inputs.azure_environment }}-${{ inputs.cluster_location_acronym }}.postgres.database.azure.com
steps:
- name: Checkout Code
uses: actions/checkout@v6
- name: Setup .NET Core SDK
uses: actions/setup-dotnet@v5
with:
global-json-file: application/global.json
- name: Restore .NET Tools
working-directory: application
run: dotnet tool restore
- name: Build Backend Solution
working-directory: application
run: dotnet build ${{ inputs.relative_startup_project }}
- name: Login to Azure
uses: azure/login@v3
with:
client-id: ${{ inputs.service_principal_id }}
tenant-id: ${{ env.TENANT_ID }}
subscription-id: ${{ inputs.subscription_id }}
- name: Open Firewall
working-directory: cloud-infrastructure/cluster
env:
CLUSTER_RESOURCE_GROUP_NAME: ${{ env.CLUSTER_RESOURCE_GROUP_NAME }}
POSTGRES_SERVER_NAME: ${{ env.POSTGRES_SERVER_NAME }}
DATABASE_NAME: ${{ inputs.database_name }}
run: bash ./firewall.sh open
- name: Generate Script for Pending Migrations
id: generate-migration-script
working-directory: application
run: |
ENTRA_USER=$(az postgres flexible-server microsoft-entra-admin list --resource-group ${{ env.CLUSTER_RESOURCE_GROUP_NAME }} --server-name ${{ env.POSTGRES_SERVER_NAME }} --query "[0].principalName" --output tsv)
CONNECTION_STRING="Host=${{ env.POSTGRES_HOST }};Database=${{ inputs.database_name }};Username=$ENTRA_USER;Password=$(az account get-access-token --resource-type oss-rdbms --query accessToken --output tsv);Ssl Mode=VerifyFull;"
echo "Checking for pending migrations..."
MIGRATION_INFO=$(dotnet ef migrations list \
--project ${{ inputs.relative_project_path }} \
--startup-project ${{ inputs.relative_startup_project }} \
--context ${{ inputs.db_context }} \
--connection "$CONNECTION_STRING" \
--no-build \
--json)
MIGRATION_JSON=$(echo "$MIGRATION_INFO" | sed -n '/^[{[]/,$p')
PENDING_MIGRATIONS_JSON=$(echo "$MIGRATION_JSON" | jq '[.[] | select(.applied == false)]')
PENDING_MIGRATIONS_COUNT=$(echo "$PENDING_MIGRATIONS_JSON" | jq '. | length')
LAST_APPLIED_MIGRATION=$(echo "$MIGRATION_JSON" | jq -r '[.[] | select(.applied == true) | .id] | sort | last // "0"')
if [ "$PENDING_MIGRATIONS_COUNT" -gt "0" ]; then
LAST_PENDING_MIGRATION=$(echo "$PENDING_MIGRATIONS_JSON" | jq -r '.[-1].id')
echo "$PENDING_MIGRATIONS_COUNT pending migration(s) detected:"
echo "$PENDING_MIGRATIONS_JSON"
echo "Generating migration script from $LAST_APPLIED_MIGRATION to $LAST_PENDING_MIGRATION..."
dotnet ef migrations script \
"$LAST_APPLIED_MIGRATION" "$LAST_PENDING_MIGRATION" \
--project ${{ inputs.relative_project_path }} \
--startup-project ${{ inputs.relative_startup_project }} \
--context ${{ inputs.db_context }} \
--idempotent \
--no-build \
--output migration.sql
echo "has_migrations_to_apply=true" >> $GITHUB_OUTPUT
echo "migration_script<<EOF" >> $GITHUB_OUTPUT
cat migration.sql >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
echo "migration_json<<EOF" >> $GITHUB_OUTPUT
echo "$PENDING_MIGRATIONS_JSON" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
else
echo "No pending migrations detected."
echo "Here is a list of all migrations:"
echo "$MIGRATION_JSON"
echo "has_migrations_to_apply=false" >> $GITHUB_OUTPUT
fi
- name: Close Firewall
if: always()
working-directory: cloud-infrastructure/cluster
env:
CLUSTER_RESOURCE_GROUP_NAME: ${{ env.CLUSTER_RESOURCE_GROUP_NAME }}
POSTGRES_SERVER_NAME: ${{ env.POSTGRES_SERVER_NAME }}
DATABASE_NAME: ${{ inputs.database_name }}
run: bash ./firewall.sh close
- name: Upload Migration Script
if: steps.generate-migration-script.outputs.has_migrations_to_apply == 'true'
uses: actions/upload-artifact@v7
with:
name: migration-script-${{ inputs.azure_environment }}-${{ inputs.cluster_location_acronym }}
path: application/migration.sql
- name: Generate Migration Information
id: migration-info
if: steps.generate-migration-script.outputs.has_migrations_to_apply == 'true'
uses: actions/github-script@v8
env:
MIGRATION_JSON: ${{ steps.generate-migration-script.outputs.migration_json }}
MIGRATION_SCRIPT: ${{ steps.generate-migration-script.outputs.migration_script }}
with:
script: |
const migrationJson = JSON.parse(process.env.MIGRATION_JSON);
const migrationsList = migrationJson.map(m => `- ${m.name} (${m.id})`).join('\n');
const migrationInfo = `## Approve Database Migration \`${{ inputs.database_name }}\` database on \`${{ inputs.azure_environment }}\`
The following pending migration(s) will be applied to the database when approved:
${migrationsList}
### Migration Script
\`\`\`sql
${process.env.MIGRATION_SCRIPT}
\`\`\`
`;
console.log(migrationInfo);
core.setOutput('markdown', migrationInfo);
- name: Add Migration Information to Pull Request
if: github.event_name == 'pull_request' && steps.generate-migration-script.outputs.has_migrations_to_apply == 'true'
uses: actions/github-script@v8
env:
MIGRATION_INFO: ${{ steps.migration-info.outputs.markdown }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// Delete previous migration info comments for this specific database
const comments = await github.rest.issues.listComments({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
per_page: 100
});
const MIGRATION_HEADER = '## Approve Database Migration `${{ inputs.database_name }}` database on `${{ inputs.azure_environment }}`';
for (const comment of comments.data) {
if (comment.body && comment.body.startsWith(MIGRATION_HEADER)) {
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: comment.id
});
}
}
// Add the new migration info comment
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: process.env.MIGRATION_INFO
});
- name: Add Migration Information to Summary
if: steps.generate-migration-script.outputs.has_migrations_to_apply == 'true' && inputs.azure_environment == 'prod'
uses: actions/github-script@v8
env:
MIGRATION_INFO: ${{ steps.migration-info.outputs.markdown }}
with:
script: core.summary.addRaw(process.env.MIGRATION_INFO).write();
apply-migrations:
name: Deploy
needs: plan-migrations
if: needs.plan-migrations.outputs.has_migrations_to_apply == 'true' && inputs.apply_migrations
runs-on: ubuntu-24.04
environment:
name: ${{ inputs.azure_environment == 'prod' && 'production' || 'staging' }}
url: ${{ format('{0}/{1}/actions/runs/{2}', github.server_url, github.repository, github.run_id) }}
env:
UNIQUE_PREFIX: ${{ vars.UNIQUE_PREFIX }}
TENANT_ID: ${{ vars.TENANT_ID }}
CLUSTER_RESOURCE_GROUP_NAME: ${{ vars.UNIQUE_PREFIX }}-${{ inputs.azure_environment }}-${{ inputs.cluster_location_acronym }}
POSTGRES_SERVER_NAME: ${{ vars.UNIQUE_PREFIX }}-${{ inputs.azure_environment }}-${{ inputs.cluster_location_acronym }}
POSTGRES_HOST: ${{ vars.UNIQUE_PREFIX }}-${{ inputs.azure_environment }}-${{ inputs.cluster_location_acronym }}.postgres.database.azure.com
steps:
- name: Checkout Code
uses: actions/checkout@v6
- name: Login to Azure
uses: azure/login@v3
with:
client-id: ${{ inputs.service_principal_id }}
tenant-id: ${{ env.TENANT_ID }}
subscription-id: ${{ inputs.subscription_id }}
- name: Download Migration Script
uses: actions/download-artifact@v8
with:
name: migration-script-${{ inputs.azure_environment }}-${{ inputs.cluster_location_acronym }}
path: .
- name: Install PostgreSQL Client
run: sudo apt-get update && sudo apt-get install -y postgresql-client
- name: Open Firewall
working-directory: cloud-infrastructure/cluster
env:
CLUSTER_RESOURCE_GROUP_NAME: ${{ env.CLUSTER_RESOURCE_GROUP_NAME }}
POSTGRES_SERVER_NAME: ${{ env.POSTGRES_SERVER_NAME }}
DATABASE_NAME: ${{ inputs.database_name }}
run: bash ./firewall.sh open
- name: Apply Migrations
run: |
echo "Applying migrations to ${{ inputs.database_name }} database on ${{ env.POSTGRES_HOST }}..."
ACCESS_TOKEN=$(az account get-access-token --resource-type oss-rdbms --query accessToken --output tsv)
ENTRA_USER=$(az postgres flexible-server microsoft-entra-admin list --resource-group ${{ env.CLUSTER_RESOURCE_GROUP_NAME }} --server-name ${{ env.POSTGRES_SERVER_NAME }} --query "[0].principalName" --output tsv)
PGPASSWORD=$ACCESS_TOKEN psql -v ON_ERROR_STOP=1 "host=${{ env.POSTGRES_HOST }} dbname=${{ inputs.database_name }} user='$ENTRA_USER' sslmode=verify-full sslrootcert=system" -f migration.sql
echo "Migrations applied successfully!"
- name: Display Migration Summary
uses: actions/github-script@v8
with:
script: core.summary.addRaw(`✅ Migrations successfully applied to \`${{ inputs.database_name }}\` database on \`${{ inputs.azure_environment }}\`.`).write();
- name: Close Firewall
if: always()
working-directory: cloud-infrastructure/cluster
env:
CLUSTER_RESOURCE_GROUP_NAME: ${{ env.CLUSTER_RESOURCE_GROUP_NAME }}
POSTGRES_SERVER_NAME: ${{ env.POSTGRES_SERVER_NAME }}
DATABASE_NAME: ${{ inputs.database_name }}
run: bash ./firewall.sh close