Marv

6.1.0 · active · verified Wed Apr 22

Marv is a programmatic database migration tool designed for Node.js environments, offering a flexible, driver-based approach to managing schema changes. Currently stable at version 6.1.0, it supports a wide range of relational databases including MySQL, PostgreSQL, SQLite, Microsoft SQL Server, and Oracle DB through dedicated pluggable drivers (e.g., `marv-pg-driver`). It distinguishes itself by providing both Promise and Callback-based APIs for integration into various application architectures. Marv's core functionality involves scanning a directory for SQL migration files, validating their sequence, and applying them to the target database. It enforces strict ordering, reporting errors for duplicate levels or out-of-sequence execution, which necessitates careful branching strategies when developing new migrations. Releases appear to follow a semantic versioning approach, with major versions introducing significant changes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use Marv with its Promise API and a PostgreSQL driver to scan for and apply migrations from a local directory. It includes boilerplate for database connection configuration and creates a sample migration file if none exists for immediate execution.

const path = require('path');
const marv = require('marv/api/promise');
const pgDriver = require('marv-pg-driver');

const migrationsDirectory = path.resolve(__dirname, 'migrations');

// Create a dummy migrations directory and file for demonstration
const fs = require('fs');
if (!fs.existsSync(migrationsDirectory)) {
  fs.mkdirSync(migrationsDirectory);
}
const migrationFile = path.join(migrationsDirectory, '001.create-test-table.sql');
if (!fs.existsSync(migrationFile)) {
  fs.writeFileSync(migrationFile, 'CREATE TABLE IF NOT EXISTS test_table (id SERIAL PRIMARY KEY, name VARCHAR(255));');
}

async function runMigrations() {
  try {
    // Placeholder for actual PostgreSQL connection details
    const connection = {
      host: process.env.DB_HOST ?? 'localhost',
      port: parseInt(process.env.DB_PORT ?? '5432', 10),
      user: process.env.DB_USER ?? 'postgres',
      password: process.env.DB_PASSWORD ?? 'password',
      database: process.env.DB_NAME ?? 'mydatabase',
      // Optional: ssl: { rejectUnauthorized: false } for local testing if needed
    };

    const migrations = await marv.scan(migrationsDirectory);
    await marv.migrate(migrations, pgDriver({ connection }));
    console.log('Migrations applied successfully!');
  } catch (err) {
    console.error('Migration failed:', err.message);
    process.exit(1);
  }
}

runMigrations();

view raw JSON →