With public database (Neon, Supabase, Planetscale etc.)
Running database migrations against services like Neon, Supabase or Planetscale is simple with Suga.Prerequisites
-
Ensure your application build is up to date by running
suga build
to generate the necessary Terraform configuration -
Identify your database module name from your
suga.yaml
file:
- Ensure your migration tool is available in your CI/CD environment (e.g., golang-migrate, flyway, liquibase, or a custom migration script)
Migration Strategy
The recommended approach follows a four-phase deployment pattern:- Deploy database infrastructure first - Create or update database resources
- Extract connection details - Get the database connection string from Terraform state
- Run migrations - Apply schema changes before deploying application code
- Deploy application - Roll out the updated application that depends on the new schema
Implementation Steps
1
Deploy Database Module
First, apply only the database module using Terraform’s
-target
flag:2
Extract Database Connection String
After the database module is deployed, extract the connection details from Terraform state:For Neon databases specifically, you’ll typically need:
- Role name and password
- Endpoint host
- Database name
- SSL mode (usually
require
)
3
Run Migrations
With the connection string available, run your migrations:
4
Deploy Application
After migrations succeed, deploy the complete application stack:
Best Practices
- Use environment-specific workspaces to isolate different stages (dev, staging, production)
- Implement proper concurrency control to prevent simultaneous migrations
- Always backup data before running migrations in production
- Test migrations in lower environments first
- Use transactional migrations when possible to enable rollback on failure
- Version control your migration files alongside your application code
Full Example: Platform-Agnostic Script
Here’s a general approach that can be adapted to any CI/CD platform:Example: GitHub Actions Implementation
Here’s a complete GitHub Actions workflow that implements the migration pattern:This is a copy of the workflow used to deploy the Suga platform itself 😃
Security Considerations
- Never log database credentials - Use masked outputs in CI/CD logs
- Use secure secret management - Store credentials in CI/CD secret stores
- Rotate credentials regularly - Implement credential rotation policies
- Limit network access - Use private endpoints or IP allowlisting where possible