|
| 1 | +# PGSchema GitHub Actions Example |
1 | 2 |
|
| 3 | +This repository demonstrates how to use [pgschema](https://www.pgschema.com/) with GitHub Actions to automatically run schema migration plans on pull requests. It includes examples for both single-file and multi-file schema approaches. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This repository contains two workflows: |
| 8 | +1. **Single File Workflow** - For projects with all schema definitions in one file |
| 9 | +2. **Multi File Workflow** - For projects with schema split across multiple SQL files |
| 10 | + |
| 11 | +Both workflows automatically: |
| 12 | +- Run `pgschema plan` when a PR modifies schema files |
| 13 | +- Post the migration plan as a comment on the PR |
| 14 | +- Update the comment if the PR is synchronized with new changes |
| 15 | + |
| 16 | +## Setup |
| 17 | + |
| 18 | +### 1. Required GitHub Secrets |
| 19 | + |
| 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 |
| 27 | + |
| 28 | +### 2. Schema Organization |
| 29 | + |
| 30 | +#### Single File Approach |
| 31 | +- Place your complete schema in `singlefile/schema.sql` |
| 32 | +- All tables, indexes, functions, and triggers in one file |
| 33 | +- Workflow: `.github/workflows/pgschema-plan-single.yml` |
| 34 | + |
| 35 | +#### Multi File Approach |
| 36 | +- Place SQL files in the `multifile/` directory |
| 37 | +- Uses `main.sql` as the entry point with psql `\i` directives |
| 38 | +- Organize files by type in subdirectories (tables/, functions/, views/, etc.) |
| 39 | +- Each file contains a specific database object |
| 40 | +- Workflow: `.github/workflows/pgschema-plan-multi.yml` |
| 41 | + |
| 42 | +## Usage |
| 43 | + |
| 44 | +1. Choose your schema approach (single file or multi file) |
| 45 | +2. Create or update schema files in the appropriate directory |
| 46 | +3. Open a pull request |
| 47 | +4. The relevant workflow will automatically run and comment with the migration plan |
| 48 | +5. Review the plan to understand what changes will be applied |
| 49 | +6. The comment will be updated if you push more changes to the PR |
| 50 | + |
| 51 | +## Example Schemas |
| 52 | + |
| 53 | +### Single File Example |
| 54 | +See `singlefile/schema.sql` for a complete schema with: |
| 55 | +- Users and posts tables |
| 56 | +- Foreign key relationships |
| 57 | +- Indexes for performance |
| 58 | +- Triggers for auto-updating timestamps |
| 59 | + |
| 60 | +### Multi File Example |
| 61 | +See `multifile/` directory for a comprehensive schema organization: |
| 62 | +- `main.sql` - Entry file using psql `\i` directives to include other files |
| 63 | +- `domains/` - Custom domain types (email_address, positive_integer) |
| 64 | +- `types/` - Custom types (address, order_status, user_status) |
| 65 | +- `sequences/` - Sequence definitions (global_id_seq, order_number_seq) |
| 66 | +- `tables/` - Table definitions (users, orders) |
| 67 | +- `functions/` - Database functions (get_user_count, update_timestamp) |
| 68 | +- `procedures/` - Stored procedures (cleanup_orders, update_status) |
| 69 | +- `views/` - Database views (order_details, user_summary) |
| 70 | + |
| 71 | +This structure demonstrates how to organize complex schemas across multiple files and directories. |
| 72 | + |
| 73 | +## Security Notes |
| 74 | + |
| 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 |
| 78 | + |
| 79 | +## Next Steps |
| 80 | + |
| 81 | +To apply migrations, you would typically: |
| 82 | +1. Review and approve the PR |
| 83 | +2. Run `pgschema apply` in your deployment pipeline |
| 84 | +3. Consider using `--auto-approve` flag for automated deployments |
| 85 | + |
| 86 | +For more information, visit the [pgschema documentation](https://www.pgschema.com/). |
0 commit comments