Skip to content

Commit cdfd2a1

Browse files
authored
Merge pull request #2 from pgschema/feature-1
feat: update single file / multi file github actions
2 parents b377c95 + 0863a15 commit cdfd2a1

File tree

3 files changed

+115
-39
lines changed

3 files changed

+115
-39
lines changed

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

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@ jobs:
1515
pgschema-plan-multi:
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,29 +45,49 @@ jobs:
2745
- name: Install pgschema
2846
run: go install github.com/pgschema/pgschema@latest
2947

48+
- name: Setup baseline schema from main branch
49+
run: |
50+
# Checkout main branch to get baseline schema
51+
git fetch origin main
52+
git checkout origin/main -- multifile/ || {
53+
echo "::error::Could not checkout multifile/ from main branch"
54+
exit 1
55+
}
56+
57+
# Copy main branch schema to temporary location
58+
cp -r multifile /tmp/main_multifile
59+
60+
# Restore current branch schema
61+
git checkout HEAD -- multifile/
62+
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_multifile/main.sql \
72+
|| echo "::warning::Failed to apply baseline schema"
73+
echo "::endgroup::"
74+
3075
- name: Run pgschema plan
3176
id: plan
3277
run: |
33-
# Run pgschema plan with directory of SQL files
78+
# Run pgschema plan
3479
PLAN_OUTPUT=$(pgschema plan \
35-
--host "${{ secrets.DB_HOST }}" \
36-
--port "${{ secrets.DB_PORT }}" \
37-
--db "${{ secrets.DB_NAME }}" \
38-
--user "${{ secrets.DB_USER }}" \
80+
--host localhost \
81+
--port 5432 \
82+
--db testdb \
83+
--user postgres \
3984
--file "${{ github.workspace }}/multifile/main.sql" \
4085
--format human 2>&1)
4186
42-
# Escape special characters for GitHub Actions
43-
PLAN_OUTPUT="${PLAN_OUTPUT//'%'/'%25'}"
44-
PLAN_OUTPUT="${PLAN_OUTPUT//$'\n'/'%0A'}"
45-
PLAN_OUTPUT="${PLAN_OUTPUT//$'\r'/'%0D'}"
46-
4787
# Set output
4888
echo "plan<<EOF" >> $GITHUB_OUTPUT
4989
echo "$PLAN_OUTPUT" >> $GITHUB_OUTPUT
5090
echo "EOF" >> $GITHUB_OUTPUT
51-
env:
52-
PGPASSWORD: ${{ secrets.DB_PASSWORD }}
5391
5492
- name: Comment PR
5593
uses: actions/github-script@v7
@@ -61,7 +99,7 @@ jobs:
6199
// Decode the escaped output
62100
const decodedOutput = decodeURIComponent(planOutput);
63101
64-
const body = `## pgschema Plan Output (Multi File)
102+
const body = `## pgschema Plan Output
65103
66104
<details>
67105
<summary>Click to expand plan details</summary>
@@ -83,7 +121,7 @@ jobs:
83121
84122
const botComment = comments.find(comment =>
85123
comment.user.type === 'Bot' &&
86-
comment.body.includes('pgschema Plan Output (Multi File)')
124+
comment.body.includes('pgschema Plan Output')
87125
);
88126
89127
if (botComment) {

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

Lines changed: 53 additions & 14 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,29 +45,50 @@ jobs:
2745
- name: Install pgschema
2846
run: go install github.com/pgschema/pgschema@latest
2947

48+
- name: Setup baseline schema from main branch
49+
run: |
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+
}
56+
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
62+
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+
3075
- name: Run pgschema plan
3176
id: plan
3277
run: |
33-
# Run pgschema plan and capture output
78+
# Run pgschema plan
3479
PLAN_OUTPUT=$(pgschema plan \
35-
--host "${{ secrets.DB_HOST }}" \
36-
--port "${{ secrets.DB_PORT }}" \
37-
--db "${{ secrets.DB_NAME }}" \
38-
--user "${{ secrets.DB_USER }}" \
80+
--debug \
81+
--host localhost \
82+
--port 5432 \
83+
--db testdb \
84+
--user postgres \
3985
--file "${{ github.workspace }}/singlefile/schema.sql" \
4086
--format human 2>&1)
4187
42-
# Escape special characters for GitHub Actions
43-
PLAN_OUTPUT="${PLAN_OUTPUT//'%'/'%25'}"
44-
PLAN_OUTPUT="${PLAN_OUTPUT//$'\n'/'%0A'}"
45-
PLAN_OUTPUT="${PLAN_OUTPUT//$'\r'/'%0D'}"
46-
4788
# Set output
4889
echo "plan<<EOF" >> $GITHUB_OUTPUT
4990
echo "$PLAN_OUTPUT" >> $GITHUB_OUTPUT
5091
echo "EOF" >> $GITHUB_OUTPUT
51-
env:
52-
PGPASSWORD: ${{ secrets.DB_PASSWORD }}
5392
5493
- name: Comment PR
5594
uses: actions/github-script@v7
@@ -61,7 +100,7 @@ jobs:
61100
// Decode the escaped output
62101
const decodedOutput = decodeURIComponent(planOutput);
63102
64-
const body = `## pgschema Plan Output (Single File)
103+
const body = `## pgschema Plan Output
65104
66105
<details>
67106
<summary>Click to expand plan details</summary>
@@ -83,7 +122,7 @@ jobs:
83122
84123
const botComment = comments.find(comment =>
85124
comment.user.type === 'Bot' &&
86-
comment.body.includes('pgschema Plan Output (Single File)')
125+
comment.body.includes('pgschema Plan Output')
87126
);
88127
89128
if (botComment) {

README.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,26 @@ Both workflows automatically:
1515

1616
## Setup
1717

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

20-
Configure the following secrets in your repository settings:
21-
22-
- `DB_HOST` - PostgreSQL database host (default: localhost)
23-
- `DB_PORT` - PostgreSQL database port (default: 5432)
24-
- `DB_NAME` - Database name
25-
- `DB_USER` - Database username
26-
- `DB_PASSWORD` - Database password
20+
No secrets required! Both workflows use a PostgreSQL 17 test container for demonstration purposes, making them fully self-contained.
2721

2822
### 2. Schema Organization
2923

3024
#### Single File Approach
3125
- Place your complete schema in `singlefile/schema.sql`
3226
- All tables, indexes, functions, and triggers in one file
27+
- Uses PostgreSQL 17 test container (no external database needed)
28+
- Compares current branch schema against main branch schema
3329
- Workflow: `.github/workflows/pgschema-plan-single.yml`
3430

3531
#### Multi File Approach
3632
- Place SQL files in the `multifile/` directory
3733
- Uses `main.sql` as the entry point with psql `\i` directives
3834
- Organize files by type in subdirectories (tables/, functions/, views/, etc.)
3935
- Each file contains a specific database object
36+
- Uses PostgreSQL 17 test container (no external database needed)
37+
- Compares current branch schema against main branch schema
4038
- Workflow: `.github/workflows/pgschema-plan-multi.yml`
4139

4240
## Usage
@@ -72,9 +70,10 @@ This structure demonstrates how to organize complex schemas across multiple file
7270

7371
## Security Notes
7472

75-
- Database credentials are stored as encrypted GitHub secrets
76-
- The workflow only has read access to your database (it runs `plan`, not `apply`)
77-
- Consider using a read-only database user for additional security
73+
- Both workflows use isolated PostgreSQL 17 test containers
74+
- No external database credentials required
75+
- Safe for demonstration and testing purposes
76+
- Containers are ephemeral and destroyed after each workflow run
7877

7978
## Next Steps
8079

0 commit comments

Comments
 (0)