Skip to Content

gofasta g migration

Generates a pair of SQL migration files (up and down) for creating or modifying a database table. The migration number is automatically incremented based on existing migration files in the db/migrations/ directory.

Usage

gofasta g migration <ResourceName> [field:type ...] [flags]

Flags

FlagShortDefaultDescription
--action-acreateMigration action. Options: create, add, remove, modify
--table-tpluralized resource nameOverride the table name

Examples

Generate a migration to create a products table:

gofasta g migration Product name:string price:float

Generate a migration to add columns to an existing table:

gofasta g migration Product description:text sku:string --action add

Generate a migration to remove a column:

gofasta g migration Product sku:string --action remove

Generate with a custom table name:

gofasta g migration UserProfile bio:text --table profiles

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 (create action)

-- 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 (create action)

-- db/migrations/000006_create_products.down.sql DROP TABLE IF EXISTS products;

Up Migration (add action)

-- db/migrations/000007_add_columns_to_products.up.sql ALTER TABLE products ADD COLUMN description TEXT; ALTER TABLE products ADD COLUMN sku VARCHAR(255) NOT NULL;

Down Migration (add action)

-- db/migrations/000007_add_columns_to_products.down.sql ALTER TABLE products DROP COLUMN IF EXISTS sku; ALTER TABLE products DROP COLUMN IF EXISTS description;

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