11name : pgschema Apply - Single File
22
33on :
4- push :
4+ pull_request :
5+ types : [closed]
56 branches :
67 - main
78 paths :
1011
1112permissions :
1213 contents : read
14+ pull-requests : write
1315
1416jobs :
1517 pgschema-apply-single :
1618 runs-on : ubuntu-latest
19+ if : github.event.pull_request.merged == true
1720
1821 env :
1922 PGPASSWORD : postgres
5558 id : apply
5659 run : |
5760 echo "::group::Applying schema changes"
61+ echo "Running pgschema apply with detailed logging..."
62+
63+ # Enable detailed error reporting
64+ set -x # Show commands as they execute
5865
5966 # Run pgschema apply with auto-approve
6067 APPLY_OUTPUT=$(pgschema apply \
@@ -67,11 +74,14 @@ jobs:
6774 --file "${{ github.workspace }}/singlefile/schema.sql" \
6875 --lock-timeout "30s" \
6976 --application-name "pgschema-github-action-apply" \
70- --format human 2>&1)
77+ 2>&1)
7178
7279 APPLY_EXIT_CODE=$?
7380
74- # Output the results
81+ set +x # Disable command tracing
82+
83+ echo "Apply exit code: $APPLY_EXIT_CODE"
84+ echo "Apply output:"
7585 echo "$APPLY_OUTPUT"
7686
7787 echo "::endgroup::"
8393
8494 echo "exit_code=$APPLY_EXIT_CODE" >> $GITHUB_OUTPUT
8595
96+
8697 # Exit with the same code as pgschema
8798 exit $APPLY_EXIT_CODE
8899
@@ -101,3 +112,79 @@ jobs:
101112 echo "❌ Failed to apply schema changes!"
102113 echo ""
103114 echo "Please check the logs above for details."
115+
116+ - name : Comment on PR with migration results
117+ if : always()
118+ uses : actions/github-script@v7
119+ with :
120+ script : |
121+ // Get the apply output from the previous step
122+ const applyOutput = `${{ steps.apply.outputs.output }}` || 'No output captured';
123+
124+ // Determine if migration was successful based on job outcome
125+ const wasSuccessful = `${{ job.status }}` === 'success';
126+
127+ let commentBody;
128+
129+ if (wasSuccessful) {
130+ commentBody = `## ✅ Schema Changes Applied Successfully!
131+
132+ <details>
133+ <summary>📋 Applied Changes</summary>
134+
135+ \`\`\`
136+ ${applyOutput}
137+ \`\`\`
138+
139+ </details>
140+
141+ **Database:** testdb
142+
143+ ---
144+ *This comment was automatically generated by the [pgschema](https://www.pgschema.com) Single File Apply workflow.*`;
145+ } else {
146+ commentBody = `## ❌ Schema Migration Failed!
147+
148+ The single-file schema migration failed after merging this PR. Please review the error details below:
149+
150+ <details>
151+ <summary>🔍 Error Details</summary>
152+
153+ \`\`\`
154+ ${applyOutput}
155+ \`\`\`
156+
157+ </details>
158+
159+ **Database:** testdb
160+
161+ ---
162+ *This comment was automatically generated by the [pgschema](https://www.pgschema.com) Single File Apply workflow.*`;
163+ }
164+
165+ // Try to find existing comment
166+ const { data: comments } = await github.rest.issues.listComments({
167+ ...context.repo,
168+ issue_number: context.issue.number,
169+ });
170+
171+ const botComment = comments.find(comment =>
172+ comment.user.type === 'Bot' &&
173+ comment.body.includes('pgschema Single File Apply workflow')
174+ );
175+
176+ if (botComment) {
177+ // Update existing comment
178+ await github.rest.issues.updateComment({
179+ ...context.repo,
180+ comment_id: botComment.id,
181+ body: commentBody
182+ });
183+ } else {
184+ // Create new comment
185+ await github.rest.issues.createComment({
186+ ...context.repo,
187+ issue_number: context.issue.number,
188+ body: commentBody
189+ });
190+ }
0 commit comments