Skip to content

Commit 639a617

Browse files
committed
chore: use test container
1 parent fc0397c commit 639a617

2 files changed

Lines changed: 64 additions & 47 deletions

File tree

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

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@ jobs:
1515
pgschema-plan-single:
1616
runs-on: ubuntu-latest
1717

18+
env:
19+
PGPASSWORD: postgres
20+
21+
services:
22+
postgres:
23+
image: postgres:17
24+
env:
25+
POSTGRES_PASSWORD: postgres
26+
POSTGRES_USER: postgres
27+
POSTGRES_DB: testdb
28+
options: >-
29+
--health-cmd pg_isready
30+
--health-interval 10s
31+
--health-timeout 5s
32+
--health-retries 5
33+
ports:
34+
- 5432:5432
35+
1836
steps:
1937
- name: Checkout code
2038
uses: actions/checkout@v4
@@ -27,63 +45,50 @@ jobs:
2745
- name: Install pgschema
2846
run: go install github.com/pgschema/pgschema@latest
2947

30-
- name: Run pgschema plan
31-
id: plan
48+
- name: Setup baseline schema from main branch
3249
run: |
33-
# Debug: Print environment info
34-
echo "::group::Environment Info"
35-
echo "Current directory: $(pwd)"
36-
echo "Schema file path: ${{ github.workspace }}/singlefile/schema.sql"
37-
echo "Checking if schema file exists:"
38-
ls -la "${{ github.workspace }}/singlefile/schema.sql" || echo "Schema file not found!"
39-
echo "Go version: $(go version)"
40-
echo "pgschema location: $(which pgschema)"
41-
echo "::endgroup::"
50+
# Checkout main branch to get baseline schema
51+
git fetch origin main
52+
git checkout origin/main -- singlefile/schema.sql || {
53+
echo "::error::Could not checkout schema.sql from main branch"
54+
exit 1
55+
}
4256
43-
# Debug: Test pgschema is installed
44-
echo "::group::Testing pgschema installation"
45-
pgschema --version || echo "Failed to run pgschema --version"
46-
echo "::endgroup::"
57+
# Copy main branch schema to temporary location
58+
cp singlefile/schema.sql /tmp/main_schema.sql
59+
60+
# Restore current branch schema
61+
git checkout HEAD -- singlefile/schema.sql
4762
48-
# Run pgschema plan with timeout and capture output
49-
echo "::group::Running pgschema plan"
50-
set +e # Don't exit on error
51-
PLAN_OUTPUT=$(timeout 60s pgschema plan \
63+
# Apply main branch schema to establish baseline database state
64+
echo "::group::Applying baseline schema"
65+
pgschema apply \
66+
--auto-approve \
67+
--host localhost \
68+
--port 5432 \
69+
--db testdb \
70+
--user postgres \
71+
--file /tmp/main_schema.sql \
72+
|| echo "::warning::Failed to apply baseline schema"
73+
echo "::endgroup::"
74+
75+
- name: Run pgschema plan
76+
id: plan
77+
run: |
78+
# Run pgschema plan
79+
PLAN_OUTPUT=$(pgschema plan \
5280
--debug \
53-
--host "${{ secrets.DB_HOST }}" \
54-
--port "${{ secrets.DB_PORT }}" \
55-
--db "${{ secrets.DB_NAME }}" \
56-
--user "${{ secrets.DB_USER }}" \
81+
--host localhost \
82+
--port 5432 \
83+
--db testdb \
84+
--user postgres \
5785
--file "${{ github.workspace }}/singlefile/schema.sql" \
5886
--format human 2>&1)
59-
EXIT_CODE=$?
60-
set -e # Re-enable exit on error
61-
echo "::endgroup::"
62-
63-
# Check exit code
64-
if [ $EXIT_CODE -eq 124 ]; then
65-
echo "::error::pgschema plan timed out after 60 seconds"
66-
PLAN_OUTPUT="Error: pgschema plan timed out after 60 seconds. This might indicate a connection issue to the database."
67-
elif [ $EXIT_CODE -ne 0 ]; then
68-
echo "::error::pgschema plan failed with exit code $EXIT_CODE"
69-
fi
70-
71-
# Debug: Show raw output
72-
echo "::group::Raw pgschema output"
73-
echo "$PLAN_OUTPUT"
74-
echo "::endgroup::"
75-
76-
# Escape special characters for GitHub Actions
77-
PLAN_OUTPUT="${PLAN_OUTPUT//'%'/'%25'}"
78-
PLAN_OUTPUT="${PLAN_OUTPUT//$'\n'/'%0A'}"
79-
PLAN_OUTPUT="${PLAN_OUTPUT//$'\r'/'%0D'}"
8087
8188
# Set output
8289
echo "plan<<EOF" >> $GITHUB_OUTPUT
8390
echo "$PLAN_OUTPUT" >> $GITHUB_OUTPUT
8491
echo "EOF" >> $GITHUB_OUTPUT
85-
env:
86-
PGPASSWORD: ${{ secrets.DB_PASSWORD }}
8792
8893
- name: Comment PR
8994
uses: actions/github-script@v7

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ Both workflows automatically:
1515

1616
## Setup
1717

18-
### 1. Required GitHub Secrets
18+
### 1. GitHub Secrets
1919

20+
#### Single File Workflow
21+
No secrets required! The single-file workflow uses a PostgreSQL 17 test container for demonstration purposes.
22+
23+
#### Multi File Workflow (if using external database)
2024
Configure the following secrets in your repository settings:
2125

2226
- `DB_HOST` - PostgreSQL database host (default: localhost)
@@ -30,6 +34,8 @@ Configure the following secrets in your repository settings:
3034
#### Single File Approach
3135
- Place your complete schema in `singlefile/schema.sql`
3236
- All tables, indexes, functions, and triggers in one file
37+
- Uses PostgreSQL 17 test container (no external database needed)
38+
- Compares current branch schema against main branch schema
3339
- Workflow: `.github/workflows/pgschema-plan-single.yml`
3440

3541
#### Multi File Approach
@@ -72,6 +78,12 @@ This structure demonstrates how to organize complex schemas across multiple file
7278

7379
## Security Notes
7480

81+
### Single File Workflow
82+
- Uses isolated PostgreSQL 17 test container
83+
- No external database credentials required
84+
- Safe for demonstration and testing purposes
85+
86+
### Multi File Workflow
7587
- Database credentials are stored as encrypted GitHub secrets
7688
- The workflow only has read access to your database (it runs `plan`, not `apply`)
7789
- Consider using a read-only database user for additional security

0 commit comments

Comments
 (0)