Skip to content

Commit 234d16c

Browse files
committed
Add GitHub Actions workflow to show database migration queries
1 parent e10abfa commit 234d16c

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
name: Show Migration Query
2+
on:
3+
push:
4+
workflow_dispatch:
5+
inputs:
6+
base_commit_hash:
7+
type: string
8+
required: true
9+
description: A base hash to compare to the latest shema.
10+
11+
jobs:
12+
run-atlas:
13+
runs-on: ubuntu-latest
14+
services:
15+
mariadb:
16+
image: mariadb:11.7.2@sha256:11706a6fd276c2eada52d0d69b1a2aa1f1484cbe78137678e02cca8f7a0ae502
17+
ports:
18+
- "3306:3306"
19+
env:
20+
MYSQL_DATABASE: ${{ env.MONITOR_DB_NAME }}
21+
MYSQL_ROOT_PASSWORD: ${{ env.MONITOR_DB_PASSWORD }}
22+
options: >-
23+
--health-cmd "healthcheck.sh --connect --innodb_initialized"
24+
--health-interval 10s
25+
--health-timeout 5s
26+
--health-retries 5
27+
env:
28+
MONITOR_DB_HOST: 127.0.0.1
29+
MONITOR_DB_PORT: 3306
30+
MONITOR_DB_USER: root
31+
MONITOR_DB_PASSWORD: monitor_pw
32+
MONITOR_DB_NAME: monitor_db
33+
steps:
34+
- name: Checkout base schema.sql
35+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
36+
with:
37+
ref: 79647d157d17e404adb9377a39faa6591ff9d4d6 # FIXME: ${{ inputs.base_commit_hash }}
38+
path: ./base
39+
sparse-checkout: |
40+
schema/database
41+
- name: Checkout latest schema.sql
42+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
43+
with:
44+
ref: main
45+
path: ./latest
46+
sparse-checkout: |
47+
schema/database
48+
- name: Create monitor_dev database
49+
run: |
50+
mysql -h ${{ env.MONITOR_DB_HOST }} -P ${{ env.MONITOR_DB_PORT }} -u ${{ env.MONITOR_DB_USER }} -p${{ env.MONITOR_DB_PASSWORD }} -e "CREATE DATABASE IF NOT EXISTS monitor_dev;"
51+
- name: Apply base schema to monitor_db
52+
run: |
53+
mysql -h ${{ env.MONITOR_DB_HOST }} -P ${{ env.MONITOR_DB_PORT }} -u ${{ env.MONITOR_DB_USER }} -p${{ env.MONITOR_DB_PASSWORD }} ${{ env.MONITOR_DB_NAME }} < ./base/schema/database/schema.sql
54+
- name: Install Atlas
55+
run: |
56+
curl -sSf https://atlasgo.sh | sh
57+
echo "$HOME/.atlas/bin" >> $GITHUB_PATH
58+
- name: Run atlas migrate diff
59+
run: |
60+
migration_query=$(atlas schema apply \
61+
--url "maria://${{ env.MONITOR_DB_USER }}:${{ env.MONITOR_DB_PASSWORD }}@${{ env.MONITOR_DB_HOST }}:${{ env.MONITOR_DB_PORT }}/${{ env.MONITOR_DB_NAME }}" \
62+
--to "file://./latest/schema/database/schema.sql" \
63+
--dev-url "maria://${{ env.MONITOR_DB_USER }}:${{ env.MONITOR_DB_PASSWORD }}@${{ env.MONITOR_DB_HOST }}:${{ env.MONITOR_DB_PORT }}/monitor_dev" \
64+
--dry-run \
65+
--format "{{ sql . }})
66+
67+
echo "MIGRATION_QUERY<<EOF" >> $GITHUB_ENV
68+
echo "$migration_query" >> $GITHUB_ENV
69+
echo "EOF" >> $GITHUB_ENV
70+
- name: Output migration query to GitHub Actions Summary
71+
run: |
72+
echo "## Migration Query" >> $GITHUB_STEP_SUMMARY
73+
echo '```sql' >> $GITHUB_STEP_SUMMARY
74+
echo "${{ env.MIGRATION_QUERY }}" >> $GITHUB_STEP_SUMMARY
75+
echo '```' >> $GITHUB_STEP_SUMMARY

0 commit comments

Comments
 (0)