Platforma provides a built-in migration system that allows you to version control your database schema changes using SQL files with special markers.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/platforma-dev/platforma/llms.txt
Use this file to discover all available pages before exploring further.
Overview
The migration system is built into thedatabase.Database type and automatically discovers migrations from registered repositories. Migrations are executed in order based on their filenames, and the system tracks which migrations have been applied.
Migration File Format
Migration files use special comment markers to define up and down migrations:Migration Markers
-- +migrate Up: Required marker that indicates the beginning of the forward migration-- +migrate Down: Optional marker for rollback SQL (placed after the Up section)-- +migrate ID: <custom_id>: Optional ID override (must be the first marker if used)
Migration ID
By default, the migration ID is derived from the filename without the.sql extension. For example, 1770095236_init.sql has an ID of 1770095236_init.
You can override this by adding an ID marker as the first line:
Embedding Migrations
Use Go’sembed package to bundle migration files with your repository:
Migrations() method must return an fs.FS containing your SQL files. Place your migration files in a migrations/ subdirectory next to your repository code.
Registering Repositories
Register your repository with the database to enable automatic migration discovery:RegisterRepository method automatically detects if your repository implements the migrator interface (has a Migrations() method) and registers it for migration.
Running Migrations
Run all pending migrations with theMigrate method:
- Creates a migrations tracking table if it doesn’t exist
- Reads all migration logs to determine which migrations have been applied
- Collects migrations from all registered repositories
- Applies pending migrations in lexicographic order by filename
- Records successfully applied migrations
Best Practices
Use timestamps in filenames
Use timestamps in filenames
Name migration files with a timestamp prefix to ensure correct ordering:
Make migrations idempotent
Make migrations idempotent
Use
IF NOT EXISTS and IF EXISTS clauses to make migrations safe to re-run:Test both Up and Down migrations
Test both Up and Down migrations
Always provide a Down migration and test that it correctly reverses the Up migration. This ensures you can roll back changes if needed.
One logical change per migration
One logical change per migration
Keep migrations focused on a single logical change to make them easier to understand and roll back if necessary.
Example: Complete Migration Setup
Write migration file
Create a migration file with timestamp prefix:
myrepo/migrations/1770095236_init.sql
Migration Tracking
The system tracks applied migrations in amigrations table with columns:
repository: The name used when callingRegisterRepositoryid: The migration ID (filename without extension or custom ID)timestamp: When the migration was applied