Skip to content

feat: update single file / multi file github actions #5

feat: update single file / multi file github actions

feat: update single file / multi file github actions #5

name: pgschema Plan - Multi File
on:
pull_request:
types: [opened, synchronize, reopened]
paths:
- 'multifile/**'
- '.github/workflows/pgschema-plan-multi.yml'
permissions:
contents: read
pull-requests: write
jobs:
pgschema-plan-multi:
runs-on: ubuntu-latest
env:
PGPASSWORD: postgres
services:
postgres:
image: postgres:17
env:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: testdb
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: 'stable'
- name: Install pgschema
run: go install github.com/pgschema/pgschema@latest
- name: Setup baseline schema from main branch
run: |
# Checkout main branch to get baseline schema
git fetch origin main
git checkout origin/main -- multifile/ || {
echo "::error::Could not checkout multifile/ from main branch"
exit 1
}
# Copy main branch schema to temporary location
cp -r multifile /tmp/main_multifile
# Restore current branch schema
git checkout HEAD -- multifile/
# Apply main branch schema to establish baseline database state
echo "::group::Applying baseline schema"
pgschema apply \
--auto-approve \
--host localhost \
--port 5432 \
--db testdb \
--user postgres \
--file /tmp/main_multifile/main.sql \
|| echo "::warning::Failed to apply baseline schema"
echo "::endgroup::"
- name: Run pgschema plan
id: plan
run: |
# Run pgschema plan
PLAN_OUTPUT=$(pgschema plan \
--host localhost \
--port 5432 \
--db testdb \
--user postgres \
--file "${{ github.workspace }}/multifile/main.sql" \
--format human 2>&1)
# Set output
echo "plan<<EOF" >> $GITHUB_OUTPUT
echo "$PLAN_OUTPUT" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Comment PR
uses: actions/github-script@v7
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const planOutput = `${{ steps.plan.outputs.plan }}`;
// Decode the escaped output
const decodedOutput = decodeURIComponent(planOutput);
const body = `## pgschema Plan Output
<details>
<summary>Click to expand plan details</summary>
\`\`\`
${decodedOutput}
\`\`\`
</details>
---
*This comment was automatically generated by the pgschema Multi File Plan workflow.*`;
// Try to find existing comment
const { data: comments } = await github.rest.issues.listComments({
...context.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('pgschema Plan Output')
);
if (botComment) {
// Update existing comment
await github.rest.issues.updateComment({
...context.repo,
comment_id: botComment.id,
body: body
});
} else {
// Create new comment
await github.rest.issues.createComment({
...context.repo,
issue_number: context.issue.number,
body: body
});
}