Overview
Ironclad uses SQLx for database migrations, providing compile-time verified SQL queries and robust migration management. Migrations are SQL files stored in themigrations/ directory that are executed in order.
Migration Files
Migrations are plain SQL files with numeric prefixes to ensure correct execution order:Creating Your First Migration
Migration File Structure
Each migration file should:- Have a numeric prefix (e.g.,
001_,002_) - Have a descriptive name
- Use the
.sqlextension - Contain valid SQL statements
Example: Create Users Table
migrations/001_create_users_table.sql:
Example: Alter Existing Table
migrations/002_add_role_to_users.sql:
Example: Create Related Table
migrations/003_create_test_table.sql:
Running Migrations
Using SQLx CLI
Install the SQLx CLI tool:Run All Pending Migrations
- Connects to the database using
DATABASE_URLfrom your.envfile - Checks which migrations have already been applied
- Executes any pending migrations in order
- Records the migration in the
_sqlx_migrationstable
Check Migration Status
- Which migrations have been applied
- Which migrations are pending
- Migration timestamps
Revert Last Migration
Migration Best Practices
1. Always Use Migrations for Schema Changes
Never modify the database schema directly. Always create a migration file:2. Make Migrations Idempotent
UseIF NOT EXISTS and IF EXISTS clauses:
3. Include Indexes
Create indexes for frequently queried columns:4. Use Appropriate Data Types
5. Add Comments
Document the purpose of migrations:6. Test Migrations Locally
Always test migrations on a local database before deploying:SQLx Compile-Time Verification
What is Compile-Time Verification?
SQLx verifies your SQL queries at compile time, catching errors before runtime. This requires the database schema to be available during compilation.Prepare for Offline Mode
Generate query metadata for offline compilation:sqlx-data.json with query metadata, allowing compilation without a database connection.
Example: Compile-Time Checked Query
FROM user instead of FROM users), the code won’t compile!
Advanced Migration Patterns
Data Migrations
Sometimes you need to migrate existing data:Foreign Keys
Create relationships between tables:Enum Types
PostgreSQL supports custom enum types:Full-Text Search
Add full-text search capabilities:Migration Workflow
Development
- Create migration file
- Write SQL statements
- Test locally:
sqlx migrate run - Update query metadata:
cargo sqlx prepare - Commit migration file and metadata
Staging/Production
- Pull latest code with migrations
- Run migrations:
sqlx migrate run - Verify schema changes
- Deploy application
Troubleshooting
Migration Already Applied
Error:migration has already been applied
Solution: The migration has already run. Check with sqlx migrate info or skip if intentional.
Syntax Error in Migration
Error:syntax error at or near "TABLE"
Solution:
- Verify SQL syntax is correct for PostgreSQL
- Test the SQL in
psqlor another PostgreSQL client - Check for missing semicolons or commas
Cannot Connect to Database
Error:error: connection refused
Solution:
- Verify
DATABASE_URLin.envis correct - Ensure database server is running
- Check network connectivity and firewall rules
Migration Conflicts
Error: Multiple migrations with same number Solution:- Use unique numeric prefixes for each migration
- Coordinate with team members on migration numbering
- Consider using timestamps:
20240115_create_users.sql
Compile-Time Query Verification Failed
Error:query does not match database schema
Solution:
- Run
sqlx migrate runto apply latest migrations - Run
cargo sqlx prepareto update query metadata - Ensure
DATABASE_URLpoints to an up-to-date database
Tips
- Keep migrations small: Each migration should do one thing
- Test thoroughly: Always test migrations on a copy of production data
- Version control: Commit migration files to git
- Document changes: Add comments explaining complex migrations
- Use transactions: Most DDL statements in PostgreSQL are transactional
- Plan rollback strategy: Consider how to undo changes if needed
- Coordinate with team: Avoid migration conflicts by communicating schema changes
Next Steps
PostgreSQL Features
Explore PostgreSQL-specific features in Ironclad
Database Configuration
Learn about connection pooling and configuration