You've created a database module, but now you need to evolve your schema. How do you add a new table? How do you track what's been deployed? How do you ensure changes can be rolled back safely?
This is where pgpm's migration system shines. In this lesson, you'll learn the fundamental workflow for adding database changes using the pgpm add command. You'll understand the three-file pattern (deploy, revert, verify) that makes migrations safe and reversible.
The Three-File Pattern
pgpm uses a three-file pattern for every database change:
- deploy - The forward migration (creates tables, adds columns, etc.)
- revert - The rollback script (undoes the deploy)
- verify - The verification script (confirms the deploy worked)
This pattern ensures every change is reversible and verifiable. You never deploy a change without knowing how to undo it.
Adding Your First Change
Navigate to your database module:
Add a new change:
pgpm prompts for the change name:
That's it. pgpm creates three SQL files and updates your plan file automatically.
What Just Happened?
pgpm created:
deploy/create_pets_table.sql- Your forward migrationrevert/create_pets_table.sql- Your rollback scriptverify/create_pets_table.sql- Your verification script
And updated pgpm.plan:
The plan file tracks every change in order. When you deploy, pgpm reads this file to know what to apply.
Writing the Deploy Script
Edit deploy/create_pets_table.sql:
Notice the comment at the top. pgpm uses this to track which change this script belongs to. The format is -- Deploy change_name.
Writing the Revert Script
Edit revert/create_pets_table.sql:
The revert script undoes everything the deploy script does. If deploy creates a table, revert drops it. If deploy adds a column, revert removes it.
Writing the Verify Script
Edit verify/create_pets_table.sql:
The verify script checks that the deployment succeeded. It should query the database to confirm the expected state. If the query returns no rows, verification fails.
Deploying Your Change
Deploy to your database:
pgpm shows what it's about to deploy:
Type y and press Enter. pgpm:
- Runs the deploy script in a transaction
- Records the change in
pgpm_migrate.changes - Runs the verify script to confirm success
Verifying the Deployment
Check that everything worked:
Or query the table directly:
You should see your table structure.
Rolling Back (If Needed)
If something goes wrong, roll back:
pgpm runs the revert script, removing the table and updating the tracking schema.
Adding More Changes
Add another change:
Edit the deploy script:
Deploy again:
pgpm only deploys the new change. It tracks what's already been applied and skips it.
Key Takeaways
pgpm addcreates three files - deploy, revert, verify- Deploy scripts are forward migrations - CREATE TABLE, ADD COLUMN, etc.
- Revert scripts undo deploys - DROP TABLE, DROP COLUMN, etc.
- Verify scripts confirm success - Query to check expected state
- pgpm tracks what's deployed - Only applies new changes
- Everything happens in transactions - Safe, atomic changes
What's Next
You've learned the basic workflow for adding changes. But what happens when your schema grows? How do you organize dozens of changes? How do you group related changes together?
In the next lesson, we'll explore nested paths and organizational strategies for managing complex schemas.
