gofasta g migration
Generates a pair of SQL migration files (up and down) for creating a database table. The migration number is automatically incremented based on existing migration files in the db/migrations/ directory. Migration templates are driver-aware — the generated SQL adapts to the database driver configured in config.yaml.
Usage
gofasta g migration <Name> [field:type ...]This command has no flags.
Examples
Generate a migration to create a products table:
gofasta g migration Product name:string price:floatGenerate a migration with multiple field types:
gofasta g migration Article title:string body:text published:bool view_count:int author_id:uuidWhat It Generates
Running gofasta g migration Product name:string price:float creates two files:
| File | Description |
|---|---|
db/migrations/000006_create_products.up.sql | SQL to create the table |
db/migrations/000006_create_products.down.sql | SQL to drop the table |
The migration number (000006) is automatically determined by scanning existing migration files and incrementing.
Up Migration
-- db/migrations/000006_create_products.up.sql
CREATE TABLE products (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(255) NOT NULL,
price DECIMAL(10,2) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
deleted_at TIMESTAMP
);
CREATE INDEX idx_products_deleted_at ON products(deleted_at);Down Migration
-- db/migrations/000006_create_products.down.sql
DROP TABLE IF EXISTS products;Database Driver Adaptation
The generated SQL syntax adapts based on the database driver configured in config/config.yaml. For example, UUID generation differs across databases:
| Driver | UUID Default |
|---|---|
| Postgres | DEFAULT gen_random_uuid() |
| MySQL | DEFAULT (UUID()) |
| SQLite | handled at application level |
| SQL Server | DEFAULT NEWID() |
| ClickHouse | DEFAULT generateUUIDv4() |
Running Migrations
After generating migration files, apply them with:
gofasta migrate upTo roll back:
gofasta migrate downRelated
- gofasta migrate — run migration files
- gofasta g model — generate the corresponding model
- gofasta g scaffold — generate all layers including migrations