bun-psql-migrations
v1.0.0
Published
Lightweight PostgreSQL migration tool for Bun - Simple CLI and programmatic API for database schema migrations
Maintainers
Readme
bun-psql-migrations
Lightweight PostgreSQL migrator for Bun. Uses Bun's built-in SQL client.
A simple, fast, and reliable database migration tool designed specifically for Bun projects. Manage your PostgreSQL schema changes with timestamp-based migrations, transaction-safe execution, and both CLI and programmatic APIs.
Table of Contents
- Features
- Why bun-psql-migrations?
- Installation
- CLI Usage
- Environment Variables
- Migration Files
- Programmatic API
- How It Works
- License
Features
- Minimal dependencies (only
pinofor logging) - Simple CLI for managing migrations
- Separate up/down migration files
- Timestamp-based migration naming (no conflicts in teams)
- Transaction-safe migrations
- Programmatic API for custom scripts
Why bun-psql-migrations?
- Built for Bun: Uses Bun's native PostgreSQL client, no external drivers needed
- Zero config: Works out of the box with just
DATABASE_URL - Team-friendly: Timestamp-based naming prevents merge conflicts
- Lightweight: Only ~50KB with minimal dependencies
Installation
bun add -d bun-psql-migrationsCLI Usage
Initialize migrations directory
bunx bun-psql-migrations initCreates a ./migrations directory in your project.
Create a new migration
bunx bun-psql-migrations create add_usersCreates two files:
migrations/20260129143052_add_users.up.sqlmigrations/20260129143052_add_users.down.sql
Apply pending migrations
DATABASE_URL=postgres://user:pass@localhost/db bunx bun-psql-migrations migrateRollback last migration
DATABASE_URL=postgres://user:pass@localhost/db bunx bun-psql-migrations rollbackReset all migrations
DATABASE_URL=postgres://user:pass@localhost/db bunx bun-psql-migrations resetEnvironment Variables
| Variable | Description | Default |
|----------|-------------|---------|
| DATABASE_URL | PostgreSQL connection string | (required) |
| MIGRATIONS_DIR | Path to migrations directory | ./migrations |
Migration Files
Migrations use separate files for up and down operations:
migrations/20260129143052_add_users.up.sql
CREATE TABLE users (
id BIGSERIAL PRIMARY KEY,
email TEXT UNIQUE NOT NULL,
created_at TIMESTAMPTZ DEFAULT now()
);migrations/20260129143052_add_users.down.sql
DROP TABLE users;Programmatic API
You can also use the library programmatically:
import { migrate, rollback, reset, getPendingMigrations } from "bun-psql-migrations";
// Apply all pending migrations
await migrate();
// Rollback the last migration
await rollback();
// Reset all migrations
await reset();
// Get list of pending migrations
const pending = await getPendingMigrations();
console.log("Pending:", pending);API Reference
migrate(dir?: string): Promise<void>
Apply all pending migrations in order.
rollback(dir?: string): Promise<void>
Rollback the last applied migration.
reset(dir?: string): Promise<void>
Rollback all migrations in reverse order.
getPendingMigrations(dir?: string): Promise<string[]>
Get list of migration names that haven't been applied yet.
getAppliedMigrations(): Promise<string[]>
Get list of migration names that have been applied.
ensureMigrationsTable(): Promise<void>
Create the _migrations tracking table if it doesn't exist.
generateTimestamp(): string
Generate a timestamp string for migration naming (YYYYMMDDHHmmss).
getMigrationsDir(): string
Get the migrations directory from MIGRATIONS_DIR env var or default.
How It Works
- Migrations are tracked in a
_migrationstable in your database - Each migration is applied within a transaction
- The migration name (without
.up.sql/.down.sql) is stored as the ID - Migrations are applied in alphabetical order (timestamp prefix ensures correct order)
License
MIT
