Documentation Index
Fetch the complete documentation index at: https://mnah05-boiler-go-21-79.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Boiler-Go uses golang-migrate to manage database schema migrations. Migrations are SQL files stored in themigrations/ directory that version control your database schema.
Prerequisites
Install the golang-migrate CLI tool:Migration structure
Migrations are stored in themigrations/ directory with sequential numbering:
migrations/000001_init.sql
Creating migrations
Generate migration files
Create a new migration using the Makefile command:This creates a new migration file with sequential numbering:
Running migrations
Apply all pending migrations
Run all migrations that haven’t been applied yet:- Loads the
DATABASE_URLfrom your.envfile - Applies migrations in sequential order
- Updates the schema version in the database
Rollback the last migration
Rollback the most recently applied migration:Migration commands reference
The Makefile provides the following migration commands:| Command | Description |
|---|---|
make migrate-up | Apply all pending migrations |
make migrate-down | Rollback the last migration |
make migrate-create name=<name> | Create a new migration file |
Manual migration commands
You can also use the migrate CLI directly:Best practices
Make migrations idempotent
Use
IF NOT EXISTS and IF EXISTS clauses to make migrations safely rerunnable.Keep migrations small
Create focused migrations that do one thing. This makes debugging and rollbacks easier.
Test before production
Always test migrations in development and staging before applying to production.
Backup before rollback
Create a database backup before running
migrate-down in production.Migration guidelines
- Use descriptive names:
add_user_rolesis better thanupdate_users - Avoid data migrations: Separate schema changes from data transformations
- Document complex logic: Add comments explaining why changes are needed
- Test rollbacks: Verify that
migrate-downworks correctly
Docker Compose configuration
The PostgreSQL service indocker-compose.yml is configured for development:
docker-compose.yml
postgres_data Docker volume, so migrations remain applied even after restarting containers.
Working with sqlc
After modifying your database schema, regenerate type-safe Go code:sqlc generate, which:
- Reads your SQL queries from the codebase
- Analyzes the current database schema
- Generates Go structs and functions with type safety
Troubleshooting
Dirty database version
Dirty database version
If a migration fails midway, the database may be in a “dirty” state:To resolve:
- Manually fix the database schema
- Force the version:
Connection refused
Connection refused
Ensure PostgreSQL is running:Verify your
DATABASE_URL in .env matches the Docker configuration.Migration already applied
Migration already applied
This is normal. The migrate tool tracks which migrations have been applied and skips them automatically.
Syntax errors in SQL
Syntax errors in SQL
Test your SQL statements directly against the database before creating a migration: