agentleFS
vndeevndee/engineering-skills/.claude/skills/db-migrate/SKILL.md

db-migrate

Skill3 starsChanged 9 months agoMITView on GitHub

Use when creating or modifying database schema in Go projects using golang-migrate with PostgreSQL

  • Deletes or force-pushes

12 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use e2e and ship.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • SentrySkill

    e2e

    Run E2E tests for Sentry JavaScript SDK test applications

    Also in 3 projects that use this

  • AWSSkill

    ship

    Finalize dev work on an issue branch — update docs, push, and open a PR.

    Also in 5 projects that use this

The file

We split this skill into the rules it gives an agent

3 rules. 0 are common, 1 is distinctive, and 2 are very specific to this project.

  • 0

    Common

    Most of the 9 edited versions we read keep these.

  • 1

    Distinctive

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Core principle: Every up.sql must have a working down.sql.

    line 12
  2. Distinctive:

    Always run the idempotency check:

    line 111
  3. Very specific:

    REQUIRED: Update CLAUDE.md if new migration commands are added (claude-md)

    line 126

“Common” is measured: kept by at least 6 in 10 of the 9 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Database Migrations (golang-migrate)

Overview

Safe, reversible PostgreSQL schema changes using golang-migrate. Every migration must be idempotent and have a working rollback.

Core principle: Every up.sql must have a working down.sql. Verify with up → down → up cycle.

When to Use

  • Adding tables, columns, indexes, or constraints
  • Modifying existing schema
  • Creating enum types
  • Adding seed data via migrations

File Naming

migrations/
  000001_create_users_table.up.sql
  000001_create_users_table.down.sql
  000002_add_user_email_index.up.sql
  000002_add_user_email_index.down.sql

Sequential numbering. Descriptive names. Always pairs.

Makefile Targets

MIGRATE=migrate -path migrations -database "$(DATABASE_URL)"

migrate-up:
	$(MIGRATE) up

migrate-down:
	$(MIGRATE) down 1

migrate-create:
	migrate create -ext sql -dir migrations -seq $(name)

migrate-force:
	$(MIGRATE) force $(version)

Safe DDL Patterns

-- Tables: always IF NOT EXISTS
CREATE TABLE IF NOT EXISTS users (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    email TEXT NOT NULL UNIQUE,
    name TEXT NOT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Indexes: CONCURRENTLY to avoid locks
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_users_email ON users (email);

-- Columns: nullable first for zero-downtime
ALTER TABLE users ADD COLUMN IF NOT EXISTS phone TEXT;
-- Later migration: backfill, then NOT NULL

Zero-Downtime Checklist

OperationSafe Approach
Add columnAdd as nullable → backfill → add NOT NULL constraint
Drop columnStop reading → deploy → drop in next migration
Rename columnAdd new → copy data → update code → drop old
Add NOT NULLAdd with DEFAULT first
Add indexUse CONCURRENTLY
Drop tableRemove all references first

PostgreSQL Type Conventions

Go TypePostgres Type
uuid.UUIDUUID
time.TimeTIMESTAMPTZ
stringTEXT
int64BIGINT
float64DOUBLE PRECISION
map/structJSONB
boolBOOLEAN

Enum Patterns

-- up.sql
CREATE TYPE user_role AS ENUM ('admin', 'member', 'viewer');
ALTER TABLE users ADD COLUMN role user_role NOT NULL DEFAULT 'member';

-- down.sql
ALTER TABLE users DROP COLUMN IF EXISTS role;
DROP TYPE IF EXISTS user_role;

-- Adding values to existing enum (cannot be in transaction)
ALTER TYPE user_role ADD VALUE IF NOT EXISTS 'moderator';

Verification

Always run the idempotency check:

make migrate-up && make migrate-down && make migrate-up

Common Mistakes

  • Missing down.sql — always write rollback
  • Using VARCHAR(n) — prefer TEXT in Postgres
  • TIMESTAMP without timezone — always use TIMESTAMPTZ
  • Non-concurrent index creation on large tables — causes locks
  • Adding NOT NULL without DEFAULT — fails on existing rows

Chains

  • REQUIRED: Update CLAUDE.md if new migration commands are added (claude-md)

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.