generator Package
Thegenerator package converts detected changes into DDL statements and produces migration files compatible with golang-migrate.
Generator
Main type for migration generation.Options
TransactionMode
Generate
Generate migration files from diff result:Example Usage
GenerateResult
MigrationPair
MigrationFile
DDL Builders
The generator uses a registry of builders for each change type:Builder Registration
DDLStatement
Built-in Builders
| Change Type | Builder |
|---|---|
| ADD_TABLE | Creates full CREATE TABLE with columns, constraints |
| DROP_TABLE | DROP TABLE IF EXISTS with warning |
| ADD_COLUMN | ALTER TABLE ADD COLUMN |
| DROP_COLUMN | ALTER TABLE DROP COLUMN with warning |
| MODIFY_COLUMN_TYPE | ALTER TABLE ALTER COLUMN TYPE |
| ADD_INDEX | CREATE INDEX IF NOT EXISTS |
| DROP_INDEX | DROP INDEX IF EXISTS |
| ADD_VIEW | CREATE OR REPLACE VIEW |
| DROP_VIEW | DROP VIEW IF EXISTS |
| ADD_FUNCTION | CREATE OR REPLACE FUNCTION |
| ADD_TRIGGER | CREATE TRIGGER |
Custom Builder Example
File Naming
Migration files follow golang-migrate convention:{version:06d}_{description}.{direction}.sql
Examples:
000001_add_users_table.up.sql000001_add_users_table.down.sql
Version Detection
Auto-detect next version from existing migrations:DDL Templates
Table Creation
Index Creation
Function Creation
Identifier Quoting
All identifiers are properly quoted:users→"users"public,users→"public"."users"User Table→"User Table"
Transaction Handling
Auto Mode
Wraps statements in transaction when safe:Cannot Use Transaction
Some operations cannot run in transactions:- CREATE INDEX CONCURRENTLY
- DROP INDEX CONCURRENTLY
- CREATE DATABASE
See Also
- Migration Generation - Concept documentation
generatecommand - CLI reference- differ Package - Change detection