Skip to content

Commit 7a97462

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

File tree

5 files changed

+146
-28
lines changed

5 files changed

+146
-28
lines changed

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

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

51+
- name: Download plan.json artifact
52+
uses: actions/download-artifact@v4
53+
with:
54+
name: pgschema-plan-${{ github.event.pull_request.number }}
55+
path: .
56+
57+
- name: Validate plan.json
58+
run: |
59+
if [ ! -f "plan.json" ]; then
60+
echo "❌ Error: plan.json not found. Make sure the plan workflow completed successfully."
61+
exit 1
62+
fi
63+
64+
echo "✅ plan.json found and ready for apply"
65+
echo "Plan file size: $(stat -f%z plan.json 2>/dev/null || stat -c%s plan.json) bytes"
66+
5167
- name: Load baseline schema
5268
run: |
5369
echo "::group::Loading baseline schema to emulate remote database"
@@ -57,21 +73,20 @@ jobs:
5773
- name: Run pgschema apply
5874
id: apply
5975
run: |
60-
echo "::group::Applying schema changes"
61-
echo "Running pgschema apply with detailed logging..."
76+
echo "::group::Applying schema changes using plan.json"
77+
echo "Running pgschema apply with pre-generated plan..."
6278
6379
# Enable detailed error reporting
6480
set -x # Show commands as they execute
6581
66-
# Run pgschema apply with auto-approve
82+
# Run pgschema apply using the plan.json file
6783
APPLY_OUTPUT=$(pgschema apply \
68-
--auto-approve \
6984
--debug \
7085
--host localhost \
7186
--port 5432 \
7287
--db testdb \
7388
--user postgres \
74-
--file "${{ github.workspace }}/multifile/main.sql" \
89+
--plan plan.json \
7590
--lock-timeout "30s" \
7691
--application-name "pgschema-github-action-apply" \
7792
2>&1)
@@ -128,6 +143,8 @@ jobs:
128143
if (wasSuccessful) {
129144
commentBody = `## ✅ Schema Changes Applied Successfully!
130145
146+
📋 **Applied using plan:** \`pgschema-plan-${{ github.event.pull_request.number }}\`
147+
131148
<details>
132149
<summary>📋 Applied Changes</summary>
133150
@@ -144,7 +161,14 @@ jobs:
144161
} else {
145162
commentBody = `## ❌ Schema Migration Failed!
146163
147-
The multi-file schema migration failed after merging this PR. Please review the error details below:
164+
The multi-file schema migration failed after merging this PR using plan \`pgschema-plan-${{ github.event.pull_request.number }}\`.
165+
166+
This could indicate:
167+
- Database schema changed between plan and apply (fingerprint mismatch)
168+
- Invalid plan.json file
169+
- Database connectivity issues
170+
171+
Please review the error details below:
148172
149173
<details>
150174
<summary>🔍 Error Details</summary>

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ jobs:
4848
- name: Install pgschema
4949
run: go install github.com/pgschema/pgschema@latest
5050

51+
- name: Download plan.json artifact
52+
uses: actions/download-artifact@v4
53+
with:
54+
name: pgschema-singlefile-plan-${{ github.event.pull_request.number }}
55+
path: .
56+
57+
- name: Validate plan.json
58+
run: |
59+
if [ ! -f "plan.json" ]; then
60+
echo "❌ Error: plan.json not found. Make sure the plan workflow completed successfully."
61+
exit 1
62+
fi
63+
64+
echo "✅ plan.json found and ready for apply"
65+
echo "Plan file size: $(stat -f%z plan.json 2>/dev/null || stat -c%s plan.json) bytes"
66+
5167
- name: Load baseline schema
5268
run: |
5369
echo "::group::Loading baseline schema to emulate remote database"
@@ -57,21 +73,20 @@ jobs:
5773
- name: Run pgschema apply
5874
id: apply
5975
run: |
60-
echo "::group::Applying schema changes"
61-
echo "Running pgschema apply with detailed logging..."
76+
echo "::group::Applying schema changes using plan.json"
77+
echo "Running pgschema apply with pre-generated plan..."
6278
6379
# Enable detailed error reporting
6480
set -x # Show commands as they execute
6581
66-
# Run pgschema apply with auto-approve
82+
# Run pgschema apply using the plan.json file
6783
APPLY_OUTPUT=$(pgschema apply \
68-
--auto-approve \
6984
--debug \
7085
--host localhost \
7186
--port 5432 \
7287
--db testdb \
7388
--user postgres \
74-
--file "${{ github.workspace }}/singlefile/schema.sql" \
89+
--plan plan.json \
7590
--lock-timeout "30s" \
7691
--application-name "pgschema-github-action-apply" \
7792
2>&1)
@@ -93,7 +108,6 @@ jobs:
93108
94109
echo "exit_code=$APPLY_EXIT_CODE" >> $GITHUB_OUTPUT
95110
96-
97111
# Exit with the same code as pgschema
98112
exit $APPLY_EXIT_CODE
99113
@@ -129,6 +143,8 @@ jobs:
129143
if (wasSuccessful) {
130144
commentBody = `## ✅ Schema Changes Applied Successfully!
131145
146+
📋 **Applied using plan:** \`pgschema-singlefile-plan-${{ github.event.pull_request.number }}\`
147+
132148
<details>
133149
<summary>📋 Applied Changes</summary>
134150
@@ -145,7 +161,14 @@ jobs:
145161
} else {
146162
commentBody = `## ❌ Schema Migration Failed!
147163
148-
The single-file schema migration failed after merging this PR. Please review the error details below:
164+
The single-file schema migration failed after merging this PR using plan \`pgschema-singlefile-plan-${{ github.event.pull_request.number }}\`.
165+
166+
This could indicate:
167+
- Database schema changed between plan and apply (fingerprint mismatch)
168+
- Invalid plan.json file
169+
- Database connectivity issues
170+
171+
Please review the error details below:
149172
150173
<details>
151174
<summary>🔍 Error Details</summary>

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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
28+
- Run `pgschema apply --plan plan.json` 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
53+
- Applies using `pgschema apply --plan plan.json`
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)