Skip to content

hexago add migration

Add a database migration to an existing project.

Synopsis

hexago add migration <name> [flags]

Operates on the project root — use --working-directory (-w) to target a project without changing directories.


Flags

Flag Short Type Default Description
--type -t string sql Migration type: sql

Note

Only sql migrations are supported. The go migration type is reserved for a future release.


Description

Generates a pair of SQL migration files with sequential numbering. Uses golang-migrate conventions.


Examples

hexago add migration create_users_table
hexago add migration add_email_index
hexago add migration alter_products_table
hexago add migration create_orders_table
hexago add migration add_foreign_keys

Generated Files

For hexago add migration create_users_table (assuming this is migration #1):

migrations/
├── 000001_create_users_table.up.sql    # Apply migration
└── 000001_create_users_table.down.sql  # Rollback migration

Subsequent migrations are automatically numbered sequentially:

migrations/
├── 000001_create_users_table.up.sql
├── 000001_create_users_table.down.sql
├── 000002_add_email_index.up.sql
├── 000002_add_email_index.down.sql
├── 000003_create_orders_table.up.sql
└── 000003_create_orders_table.down.sql

Generated File Contents

000001_create_users_table.up.sql:

-- Migration: create_users_table
-- TODO: Add your UP migration SQL here

-- Example:
-- CREATE TABLE users (
--     id UUID PRIMARY KEY,
--     name VARCHAR(255) NOT NULL,
--     email VARCHAR(255) UNIQUE NOT NULL,
--     created_at TIMESTAMP NOT NULL DEFAULT NOW()
-- );

000001_create_users_table.down.sql:

-- Migration: create_users_table (rollback)
-- TODO: Add your DOWN migration SQL here

-- Example:
-- DROP TABLE IF EXISTS users;

Running Migrations

Generated projects with --with-migrations include Makefile targets:

make migrate-up       # Apply all pending migrations
make migrate-down     # Rollback the last migration
make migrate-version  # Show current migration version

Or run directly with golang-migrate:

migrate -path ./migrations -database "postgres://..." up
migrate -path ./migrations -database "postgres://..." down 1

Prerequisites

Migrations require the project was initialized with --with-migrations:

hexago init my-app --module github.com/me/my-app --with-migrations

Or the migrations directory must exist.