A tool that extracts database schema from PostgreSQL migration files with MCP to discover the schema by running a temporary db. Useful when you work on a project that has a migration folder containing a lot of files.
mig2schema takes a directory containing PostgreSQL migration files (.up.sql and .down.sql) and extracts the resulting database schema after running the migrations. It uses testcontainers to spin up a temporary PostgreSQL instance, runs the migrations, and then extracts the schema information.
go build -o mig2schemaShows human-readable schema information:
./mig2schema /path/to/migrationsOutputs SQL CREATE statements that can be used to recreate the schema:
./mig2schema -e /path/to/migrations
./mig2schema --extract /path/to/migrationsBy default, the tool uses postgres:16-alpine. You can specify a different PostgreSQL Docker image:
./mig2schema --pg-image postgres:17-alpine /path/to/migrations
./mig2schema --pg-image pgvector/pgvector:pg16 /path/to/migrationsThe tool supports multiple providers for extracting schema:
- native (default): Built-in provider using SQL queries to information_schema
- pg_dump: Uses PostgreSQL's pg_dump utility for complete DDL extraction
# List available providers
./mig2schema --list-providers
# Use native provider (default)
./mig2schema -p native -e /path/to/migrations
# Use pg_dump provider (requires pg_dump in PATH, only supports extract mode)
./mig2schema -p pg_dump -e /path/to/migrationsNote: The pg_dump provider only works with extract mode (-e) and provides more complete schema information including foreign keys, sequences, and all constraints.
The tool expects migration files to follow the naming convention:
001_create_users.up.sql- Migration up file001_create_users.down.sql- Migration down file (optional)
Files are executed in alphabetical order by filename.
./mig2schema examples/migrations
# Output:
# === DATABASE SCHEMA ===
# Table: users
# Columns:
# - id integer NOT NULL (PRIMARY KEY)
# - email character varying NOT NULL
# - username character varying NOT NULL
# Indexes:
# - idx_users_email on (email)
# - users_email_key on (email) (UNIQUE)Using native provider:
./mig2schema -e examples/migrations
# Output:
# create table users (
# id integer not null default nextval('users_id_seq'::regclass),
# email varchar(255) not null,
# username varchar(255) not null,
# primary key (id)
# );
#
# create index idx_users_email on users (email);
# create unique index users_email_key on users (email);Using pg_dump provider (more complete output):
./mig2schema -p pg_dump -e examples/migrations
# Output includes:
# - Complete CREATE TABLE statements
# - ALTER TABLE statements for sequences
# - Foreign key constraints
# - All indexes with proper syntax
# - Default values and constraints- Docker (for testcontainers)
- Go 1.24.2+
- PostgreSQL client tools (optional, required for pg_dump provider)
This tool can be used as an MCP (Model Context Protocol) server in Claude Code:
claude mcp add mig2schema -- {path}/mig2schema --mcpWhere {path} is the full path to the mig2schema binary (e.g., /Users/username/go/bin/mig2schema).
Once added, Claude Code can use the following tools:
Extract database schema from migration files using pg_dump for complete DDL output.
Parameters:
migration_directory(required): Path to directory containing migration filesformat(optional): Output format - "sql" (default and only option)postgres_image(optional): PostgreSQL Docker image to use (default: "postgres:16-alpine")
Example usage in Claude Code:
Use the extract_schema tool with migration_directory="./migrations"
Note: MCP mode always uses the pg_dump provider and outputs SQL DDL for the most complete and accurate schema extraction including foreign keys, sequences, and all constraints.
Validate migration files without running them.
Parameters:
migration_directory(required): Path to directory containing migration filespostgres_image(optional): PostgreSQL Docker image to use (default: "postgres:16-alpine")