Kysely Migration CLI

0.9.1 · active · verified Wed Apr 22

Kysely-Mig is a TypeScript-first command-line interface (CLI) tool designed to manage database migrations and seeding for PostgreSQL databases, built on top of the Kysely query builder. The current stable version is 0.9.1, indicating it's still in active development, likely with frequent updates as features are added or refined. Key differentiators include atomic operations, automatic rollback on errors, transaction support, migration locking, multi-environment configuration, and selective/partial migration execution. It provides both a robust CLI for schema evolution and a programmatic API for integrating migration logic directly into applications, simplifying database management for Kysely users.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates the initial setup, environment configuration, migration generation, and execution using the `kysely-mig` CLI.

npm install -g kysely-mig

mig setup

# Generate a database configuration file
mig env generate local-db

# Open 'local-db.env' and fill in your PostgreSQL connection details
# Example: DATABASE_URL="postgres://user:password@host:port/database"

# Add the configuration to kysely-mig's context and set it as default
mig env add ./local-db.env -d

# Verify the current environment is set
mig env get

# Generate a new migration file for 'users' table
mig make users

# Edit the generated migration file (e.g., migrations/timestamp_users.ts)
// Example content for migrations/timestamp_users.ts:
// import { Kysely, sql } from 'kysely';
//
// export async function up(db: Kysely<any>): Promise<void> {
//   await db.schema
//     .createTable('users')
//     .addColumn('id', 'uuid', (col) => col.primaryKey().defaultTo(sql`gen_random_uuid()`))
//     .addColumn('email', 'varchar(255)', (col) => col.unique().notNull())
//     .addColumn('username', 'varchar(255)', (col) => col.unique().notNull())
//     .addColumn('created_at', 'timestamp', (col) => col.defaultTo(sql`now()`).notNull())
//     .execute();
// }
//
// export async function down(db: Kysely<any>): Promise<void> {
//   await db.schema.dropTable('users').execute();
// }

# Run all pending migrations
mig up

view raw JSON →