Skip to content

Commit 91d3d18

Browse files
committed
chore: multi file change
1 parent 639a617 commit 91d3d18

5 files changed

Lines changed: 62 additions & 38 deletions

File tree

.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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ jobs:
100100
// Decode the escaped output
101101
const decodedOutput = decodeURIComponent(planOutput);
102102
103-
const body = `## pgschema Plan Output (Single File)
103+
const body = `## pgschema Plan Output
104104
105105
<details>
106106
<summary>Click to expand plan details</summary>
@@ -122,7 +122,7 @@ jobs:
122122
123123
const botComment = comments.find(comment =>
124124
comment.user.type === 'Bot' &&
125-
comment.body.includes('pgschema Plan Output (Single File)')
125+
comment.body.includes('pgschema Plan Output')
126126
);
127127
128128
if (botComment) {

README.md

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,7 @@ Both workflows automatically:
1717

1818
### 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)
24-
Configure the following secrets in your repository settings:
25-
26-
- `DB_HOST` - PostgreSQL database host (default: localhost)
27-
- `DB_PORT` - PostgreSQL database port (default: 5432)
28-
- `DB_NAME` - Database name
29-
- `DB_USER` - Database username
30-
- `DB_PASSWORD` - Database password
20+
No secrets required! Both workflows use a PostgreSQL 17 test container for demonstration purposes, making them fully self-contained.
3121

3222
### 2. Schema Organization
3323

@@ -43,6 +33,8 @@ Configure the following secrets in your repository settings:
4333
- Uses `main.sql` as the entry point with psql `\i` directives
4434
- Organize files by type in subdirectories (tables/, functions/, views/, etc.)
4535
- 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
4638
- Workflow: `.github/workflows/pgschema-plan-multi.yml`
4739

4840
## Usage
@@ -78,15 +70,10 @@ This structure demonstrates how to organize complex schemas across multiple file
7870

7971
## Security Notes
8072

81-
### Single File Workflow
82-
- Uses isolated PostgreSQL 17 test container
73+
- Both workflows use isolated PostgreSQL 17 test containers
8374
- No external database credentials required
8475
- Safe for demonstration and testing purposes
85-
86-
### Multi File Workflow
87-
- Database credentials are stored as encrypted GitHub secrets
88-
- The workflow only has read access to your database (it runs `plan`, not `apply`)
89-
- Consider using a read-only database user for additional security
76+
- Containers are ephemeral and destroyed after each workflow run
9077

9178
## Next Steps
9279

multifile/tables/orders.sql

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ CREATE TABLE IF NOT EXISTS orders (
99
amount numeric(10,2) DEFAULT 0.00
1010
);
1111

12-
COMMENT ON TABLE orders IS 'Customer orders';
13-
1412
COMMENT ON COLUMN orders.user_id IS 'Reference to user';
1513

1614
--

multifile/tables/users.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
CREATE TABLE IF NOT EXISTS users (
66
id integer PRIMARY KEY,
7-
email text NOT NULL CHECK (email LIKE '%@%'),
8-
name text NOT NULL
7+
email text NOT NULL,
8+
name text NOT NULL,
9+
city text NOT NULL
910
);
1011

1112
COMMENT ON TABLE users IS 'User accounts';

0 commit comments

Comments
 (0)