Skip to Content

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:float

Generate a migration with multiple field types:

gofasta g migration Article title:string body:text published:bool view_count:int author_id:uuid

What It Generates

Running gofasta g migration Product name:string price:float creates two files:

FileDescription
db/migrations/000006_create_products.up.sqlSQL to create the table
db/migrations/000006_create_products.down.sqlSQL 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:

DriverUUID Default
PostgresDEFAULT gen_random_uuid()
MySQLDEFAULT (UUID())
SQLitehandled at application level
SQL ServerDEFAULT NEWID()
ClickHouseDEFAULT generateUUIDv4()

Running Migrations

After generating migration files, apply them with:

gofasta migrate up

To roll back:

gofasta migrate down
Last updated on