Skip to content

Commit fa63232

Browse files
committed
chore: update github actions
1 parent 8badacb commit fa63232

File tree

5 files changed

+154
-30
lines changed

5 files changed

+154
-30
lines changed

.github/workflows/pgschema-multifile-apply.yml

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ jobs:
4848
- name: Install pgschema
4949
run: go install github.com/pgschema/pgschema@latest
5050

51+
- name: Download plan.json artifact
52+
uses: dawidd6/action-download-artifact@v6
53+
with:
54+
workflow: pgschema-multifile-plan.yml
55+
pr: ${{ github.event.pull_request.number }}
56+
name: pgschema-plan-${{ github.event.pull_request.number }}
57+
path: .
58+
59+
- name: Validate plan.json
60+
run: |
61+
if [ ! -f "plan.json" ]; then
62+
echo "❌ Error: plan.json not found. Make sure the plan workflow completed successfully."
63+
exit 1
64+
fi
65+
66+
echo "✅ plan.json found and ready for apply"
67+
echo "Plan file size: $(stat -f%z plan.json 2>/dev/null || stat -c%s plan.json) bytes"
68+
5169
- name: Load baseline schema
5270
run: |
5371
echo "::group::Loading baseline schema to emulate remote database"
@@ -57,23 +75,23 @@ jobs:
5775
- name: Run pgschema apply
5876
id: apply
5977
run: |
60-
echo "::group::Applying schema changes"
61-
echo "Running pgschema apply with detailed logging..."
78+
echo "::group::Applying schema changes using plan.json"
79+
echo "Running pgschema apply with pre-generated plan..."
6280
6381
# Enable detailed error reporting
6482
set -x # Show commands as they execute
6583
66-
# Run pgschema apply with auto-approve
84+
# Run pgschema apply using the plan.json file
6785
APPLY_OUTPUT=$(pgschema apply \
68-
--auto-approve \
6986
--debug \
7087
--host localhost \
7188
--port 5432 \
7289
--db testdb \
7390
--user postgres \
74-
--file "${{ github.workspace }}/multifile/main.sql" \
91+
--plan plan.json \
7592
--lock-timeout "30s" \
7693
--application-name "pgschema-github-action-apply" \
94+
--auto-approve \
7795
2>&1)
7896
7997
APPLY_EXIT_CODE=$?
@@ -128,6 +146,8 @@ jobs:
128146
if (wasSuccessful) {
129147
commentBody = `## ✅ Schema Changes Applied Successfully!
130148
149+
📋 **Applied using plan:** \`pgschema-plan-${{ github.event.pull_request.number }}\`
150+
131151
<details>
132152
<summary>📋 Applied Changes</summary>
133153
@@ -144,7 +164,14 @@ jobs:
144164
} else {
145165
commentBody = `## ❌ Schema Migration Failed!
146166
147-
The multi-file schema migration failed after merging this PR. Please review the error details below:
167+
The multi-file schema migration failed after merging this PR using plan \`pgschema-plan-${{ github.event.pull_request.number }}\`.
168+
169+
This could indicate:
170+
- Database schema changed between plan and apply (fingerprint mismatch)
171+
- Invalid plan.json file
172+
- Database connectivity issues
173+
174+
Please review the error details below:
148175
149176
<details>
150177
<summary>🔍 Error Details</summary>

.github/workflows/pgschema-multifile-plan.yml

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
types: [opened, synchronize, reopened]
66
paths:
77
- "multifile/**"
8-
- ".github/workflows/pgschema-plan-multi.yml"
8+
- ".github/workflows/pgschema-multifile-plan.yml"
99

1010
permissions:
1111
contents: read
@@ -55,27 +55,44 @@ jobs:
5555
id: plan
5656
run: |
5757
echo "Running pgschema plan with detailed logging..."
58-
58+
5959
# Enable detailed error reporting
6060
set -x # Show commands as they execute
61-
62-
# Run pgschema plan
61+
62+
# Run pgschema plan and generate both text and JSON output
6363
PLAN_OUTPUT=$(pgschema plan \
6464
--debug \
6565
--host localhost \
6666
--port 5432 \
6767
--db testdb \
6868
--user postgres \
6969
--file "${{ github.workspace }}/multifile/main.sql" \
70+
--output-json plan.json \
71+
--output-human stdout \
7072
2>&1)
71-
73+
7274
set +x # Disable command tracing
7375
74-
# Set output
76+
# Verify plan.json was created
77+
if [ ! -f "plan.json" ]; then
78+
echo "❌ Error: plan.json was not generated"
79+
exit 1
80+
fi
81+
82+
echo "✅ plan.json generated successfully"
83+
84+
# Set output for PR comment
7585
echo "plan<<EOF" >> $GITHUB_OUTPUT
7686
echo "$PLAN_OUTPUT" >> $GITHUB_OUTPUT
7787
echo "EOF" >> $GITHUB_OUTPUT
7888
89+
- name: Upload plan.json artifact
90+
uses: actions/upload-artifact@v4
91+
with:
92+
name: pgschema-plan-${{ github.event.pull_request.number }}
93+
path: plan.json
94+
retention-days: 30
95+
7996
- name: Comment PR
8097
uses: actions/github-script@v7
8198
with:
@@ -87,6 +104,10 @@ jobs:
87104
88105
const body = `## pgschema Plan Output
89106
107+
📋 **Plan artifact created:** \`pgschema-plan-${{ github.event.pull_request.number }}\`
108+
109+
This plan will be used automatically when the PR is merged.
110+
90111
<details>
91112
<summary>Click to expand plan details</summary>
92113

.github/workflows/pgschema-singlefile-apply.yml

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ jobs:
4848
- name: Install pgschema
4949
run: go install github.com/pgschema/pgschema@latest
5050

51+
- name: Download plan.json artifact
52+
uses: dawidd6/action-download-artifact@v6
53+
with:
54+
workflow: pgschema-singlefile-plan.yml
55+
pr: ${{ github.event.pull_request.number }}
56+
name: pgschema-singlefile-plan-${{ github.event.pull_request.number }}
57+
path: .
58+
59+
- name: Validate plan.json
60+
run: |
61+
if [ ! -f "plan.json" ]; then
62+
echo "❌ Error: plan.json not found. Make sure the plan workflow completed successfully."
63+
exit 1
64+
fi
65+
66+
echo "✅ plan.json found and ready for apply"
67+
echo "Plan file size: $(stat -f%z plan.json 2>/dev/null || stat -c%s plan.json) bytes"
68+
5169
- name: Load baseline schema
5270
run: |
5371
echo "::group::Loading baseline schema to emulate remote database"
@@ -57,23 +75,23 @@ jobs:
5775
- name: Run pgschema apply
5876
id: apply
5977
run: |
60-
echo "::group::Applying schema changes"
61-
echo "Running pgschema apply with detailed logging..."
78+
echo "::group::Applying schema changes using plan.json"
79+
echo "Running pgschema apply with pre-generated plan..."
6280
6381
# Enable detailed error reporting
6482
set -x # Show commands as they execute
6583
66-
# Run pgschema apply with auto-approve
84+
# Run pgschema apply using the plan.json file
6785
APPLY_OUTPUT=$(pgschema apply \
68-
--auto-approve \
6986
--debug \
7087
--host localhost \
7188
--port 5432 \
7289
--db testdb \
7390
--user postgres \
74-
--file "${{ github.workspace }}/singlefile/schema.sql" \
91+
--plan plan.json \
7592
--lock-timeout "30s" \
7693
--application-name "pgschema-github-action-apply" \
94+
--auto-approve \
7795
2>&1)
7896
7997
APPLY_EXIT_CODE=$?
@@ -93,7 +111,6 @@ jobs:
93111
94112
echo "exit_code=$APPLY_EXIT_CODE" >> $GITHUB_OUTPUT
95113
96-
97114
# Exit with the same code as pgschema
98115
exit $APPLY_EXIT_CODE
99116
@@ -129,6 +146,8 @@ jobs:
129146
if (wasSuccessful) {
130147
commentBody = `## ✅ Schema Changes Applied Successfully!
131148
149+
📋 **Applied using plan:** \`pgschema-singlefile-plan-${{ github.event.pull_request.number }}\`
150+
132151
<details>
133152
<summary>📋 Applied Changes</summary>
134153
@@ -145,7 +164,14 @@ jobs:
145164
} else {
146165
commentBody = `## ❌ Schema Migration Failed!
147166
148-
The single-file schema migration failed after merging this PR. Please review the error details below:
167+
The single-file schema migration failed after merging this PR using plan \`pgschema-singlefile-plan-${{ github.event.pull_request.number }}\`.
168+
169+
This could indicate:
170+
- Database schema changed between plan and apply (fingerprint mismatch)
171+
- Invalid plan.json file
172+
- Database connectivity issues
173+
174+
Please review the error details below:
149175
150176
<details>
151177
<summary>🔍 Error Details</summary>

.github/workflows/pgschema-singlefile-plan.yml

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
types: [opened, synchronize, reopened]
66
paths:
77
- "singlefile/**"
8-
- ".github/workflows/pgschema-plan-single.yml"
8+
- ".github/workflows/pgschema-singlefile-plan.yml"
99

1010
permissions:
1111
contents: read
@@ -55,27 +55,44 @@ jobs:
5555
id: plan
5656
run: |
5757
echo "Running pgschema plan with detailed logging..."
58-
58+
5959
# Enable detailed error reporting
6060
set -x # Show commands as they execute
61-
62-
# Run pgschema plan
61+
62+
# Run pgschema plan and generate both text and JSON output
6363
PLAN_OUTPUT=$(pgschema plan \
6464
--debug \
6565
--host localhost \
6666
--port 5432 \
6767
--db testdb \
6868
--user postgres \
6969
--file "${{ github.workspace }}/singlefile/schema.sql" \
70+
--output-json plan.json \
71+
--output-human stdout \
7072
2>&1)
71-
73+
7274
set +x # Disable command tracing
7375
74-
# Set output
76+
# Verify plan.json was created
77+
if [ ! -f "plan.json" ]; then
78+
echo "❌ Error: plan.json was not generated"
79+
exit 1
80+
fi
81+
82+
echo "✅ plan.json generated successfully"
83+
84+
# Set output for PR comment
7585
echo "plan<<EOF" >> $GITHUB_OUTPUT
7686
echo "$PLAN_OUTPUT" >> $GITHUB_OUTPUT
7787
echo "EOF" >> $GITHUB_OUTPUT
7888
89+
- name: Upload plan.json artifact
90+
uses: actions/upload-artifact@v4
91+
with:
92+
name: pgschema-singlefile-plan-${{ github.event.pull_request.number }}
93+
path: plan.json
94+
retention-days: 30
95+
7996
- name: Comment PR
8097
uses: actions/github-script@v7
8198
with:
@@ -87,6 +104,10 @@ jobs:
87104
88105
const body = `## pgschema Plan Output
89106
107+
📋 **Plan artifact created:** \`pgschema-singlefile-plan-${{ github.event.pull_request.number }}\`
108+
109+
This plan will be used automatically when the PR is merged.
110+
90111
<details>
91112
<summary>Click to expand plan details</summary>
92113

README.md

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# pgschema GitHub Actions Example
22

3-
This repository demonstrates how to use [pgschema](https://www.pgschema.com/) with GitHub Actions to automatically run schema migration plans on pull requests. It includes examples for both single-file and multi-file schema approaches.
3+
This repository demonstrates how to use [pgschema](https://www.pgschema.com/) with GitHub Actions to implement the **plan-review-apply workflow pattern** for safe database schema migrations. It includes examples for both single-file and multi-file schema approaches.
44

55
## Overview
66

@@ -11,8 +11,10 @@ This repository demonstrates how to use [pgschema](https://www.pgschema.com/) wi
1111

1212
Plan workflows automatically:
1313

14-
- Run `pgschema plan` when a PR modifies schema files
15-
- Post the migration plan as a comment on the PR
14+
- Run `pgschema plan --output-human stdout --output-json plan.json` when a PR modifies schema files
15+
- Generate both human-readable output for PR comments and plan.json artifact for deployment
16+
- Post the migration plan as a comment on the PR for team review
17+
- Upload plan.json as a GitHub artifact for the apply workflow
1618
- Update the comment if the PR is synchronized with new changes
1719

1820
### Apply Workflows (Merged Pull Requests)
@@ -22,11 +24,36 @@ Plan workflows automatically:
2224

2325
Apply workflows automatically:
2426

25-
- Run `pgschema apply` when pull requests are merged to main branch
26-
- Use `--auto-approve` flag for automated deployment
27+
- Download the plan.json artifact generated during the plan phase using `dawidd6/action-download-artifact`
28+
- Run `pgschema apply --plan plan.json --auto-approve` using the pre-approved plan
29+
- Validate database fingerprint to ensure no concurrent schema changes occurred
2730
- Apply changes to a test PostgreSQL 17 container
2831
- Comment on the PR with success or failure results and detailed logs
2932

33+
## Plan-Review-Apply Workflow Pattern
34+
35+
This implementation follows the [pgschema plan-review-apply pattern](https://www.pgschema.com/workflow/plan-review-apply) for safe database migrations:
36+
37+
### 1. Plan Phase (Pull Request)
38+
39+
- Generates detailed migration plan with `pgschema plan`
40+
- Creates both human-readable output and plan.json artifact
41+
- Team reviews the proposed changes in PR comments
42+
- Plan.json is stored as GitHub artifact for later use
43+
44+
### 2. Review Phase (Pull Request Review)
45+
46+
- Team examines the migration plan for correctness and safety
47+
- Considers business impact and potential risks
48+
- Approves or requests changes before merging
49+
50+
### 3. Apply Phase (Merge to Main)
51+
52+
- Downloads the exact plan.json that was reviewed using `dawidd6/action-download-artifact`
53+
- Applies using `pgschema apply --plan plan.json --auto-approve`
54+
- Fingerprint validation prevents concurrent schema changes
55+
- Ensures exactly what was planned is what gets applied
56+
3057
## Setup
3158

3259
### PostgreSQL 17 Test Container Setup
@@ -47,6 +74,8 @@ This approach ensures that:
4774
- Migration plans show realistic diffs against existing schema
4875
- Apply operations work against a database with existing data structure
4976
- Tests validate changes in a production-like environment
77+
- Plans are generated and validated against the actual target database state
78+
- Fingerprint validation catches any concurrent schema modifications
5079

5180
### GitHub Secrets
5281

0 commit comments

Comments
 (0)