Skip to content

Simple PostgreSQL migration tool for Node.js applications.

Notifications You must be signed in to change notification settings

kzemlyak/migrilla

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🦍 Migrilla

Simple PostgreSQL migration tool for Node.js applications.

πŸ‡·πŸ‡Ί Русская вСрсия | πŸ‡ΊπŸ‡Έ English version

πŸš€ Quick Start Guide - Get started in under 5 minutes! 🐳 Docker Setup - Test with Docker! 🐳 Docker Quick Start - Docker setup in 2 minutes!

πŸš€ Installation

npm install migrilla

βš™οΈ Usage

Commands

migrilla up              # Apply all pending migrations
migrilla down [step]     # Rollback migrations (default: 1, max: all applied)
migrilla status          # Show migration status
migrilla help            # Show help message

Environment Variables

Configure database connection using environment variables:

MIGRILLA_DB_HOST=localhost           # Database host (default: localhost)
MIGRILLA_DB_PORT=5432               # Database port (default: 5432)
MIGRILLA_DB_NAME=myapp              # Database name (default: postgres)
MIGRILLA_DB_USER=postgres           # Database user (default: postgres)
MIGRILLA_DB_PASSWORD=mypassword     # Database password
MIGRILLA_DATABASE_URL=postgres://user:pass@host:port/db  # Full connection string
MIGRILLA_DB_SSL=true                # Use SSL connection (default: false)

πŸ’Ύ How It Works

βœ… Reads SQL files from migrations/ directory
βœ… Tracks applied migrations in migrilla_state table
βœ… Atomic execution - each migration runs in a transaction
βœ… Provides beautiful console output
βœ… Handles errors gracefully

🦍 Migration Files

Create migration files in the migrations/ directory:

File Naming Pattern

001_migration_name_up.sql    # Apply migration
001_migration_name_down.sql  # Rollback migration

Example Structure

migrations/
β”œβ”€β”€ 001_add_users_table_up.sql
β”œβ”€β”€ 001_add_users_table_down.sql
β”œβ”€β”€ 002_add_orders_table_up.sql
β”œβ”€β”€ 002_add_orders_table_down.sql
└── 003_add_indexes_up.sql
└── 003_add_indexes_down.sql

πŸ“š Examples

Example Migration Files

001_add_users_table_up.sql

CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  email VARCHAR(255) UNIQUE NOT NULL,
  name VARCHAR(255) NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

001_add_users_table_down.sql

DROP TABLE IF EXISTS users;

002_add_orders_table_up.sql

CREATE TABLE orders (
  id SERIAL PRIMARY KEY,
  user_id INTEGER REFERENCES users(id),
  amount DECIMAL(10,2) NOT NULL,
  status VARCHAR(50) DEFAULT 'pending',
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

002_add_orders_table_down.sql

DROP TABLE IF EXISTS orders;

Running Migrations

# Apply all pending migrations
migrilla up

# Check migration status
migrilla status

# Rollback 1 migration (default)
migrilla down

# Rollback 3 migrations
migrilla down 3

# Rollback all applied migrations
migrilla down 999

Example Output

$ migrilla up
βœ… Migrilla initialized successfully
πŸ”„ Applying migration: 001_add_users_table
βœ… Executed: 001_add_users_table
πŸ”„ Applying migration: 002_add_orders_table
βœ… Executed: 002_add_orders_table
πŸŽ‰ All migrations applied successfully!

$ migrilla status

πŸ“Š Migration Status:
==================
βœ… Applied | 001_add_users_table
βœ… Applied | 002_add_orders_table
⏳ Pending | 003_add_indexes

πŸ“ˆ Summary:
Total migrations: 3
Applied: 2
Pending: 1

$ migrilla down
βœ… Migrilla initialized successfully
πŸ”„ Rolling back 1 migration(s)...
πŸ”„ Rolling back migration: 002_add_orders_table
βœ… Executed: 002_add_orders_table
βœ… Migration rolled back successfully!

$ migrilla down 3
βœ… Migrilla initialized successfully
πŸ”„ Rolling back 3 migration(s)...
πŸ”„ Rolling back migration: 003_add_indexes
βœ… Executed: 003_add_indexes
πŸ”„ Rolling back migration: 002_add_orders_table
βœ… Executed: 002_add_orders_table
πŸ”„ Rolling back migration: 001_add_users_table
βœ… Executed: 001_add_users_table
βœ… 3 migrations rolled back successfully!

πŸ”§ Programmatic Usage

You can also use Migrilla programmatically with functional approach:

const createMigrilla = require('migrilla');

const migrilla = createMigrilla({
  host: 'localhost',
  port: 5432,
  database: 'myapp',
  user: 'postgres',
  password: 'password'
});

// Apply migrations
await migrilla.up();

// Rollback 1 migration (default)
await migrilla.down();

// Rollback 3 migrations
await migrilla.down(3);

// Rollback all applied migrations
await migrilla.down(999);

// Get status
await migrilla.status();

// Close connection
await migrilla.close();

Why Functions Instead of Classes?

  • Immutable Configuration: Configuration is closed over by functions
  • No State Mutations: More predictable and testable code
  • Better Performance: Reduced object instantiation overhead
  • Functional Style: Clean separation of concerns

Manual Transaction Control

You can also execute custom operations within transactions:

// Transaction API with automatic management
await migrilla.executeInTransaction(async (client) => {
  await client.query('INSERT INTO users (name) VALUES ($1)', ['John']);
  await client.query('UPDATE users SET active = true WHERE name = $1', ['John']);
});

πŸ› οΈ Configuration Options

const migrilla = new Migrilla({
  // Database connection options (pg Pool options)
  host: 'localhost',
  port: 5432,
  database: 'myapp',
  user: 'postgres',
  password: 'password',
  
  // Migrilla specific options
  migrationsDir: './migrations',    // Directory with migration files
  stateTable: 'migrilla_state'      // Table name for tracking migrations
});

πŸ”’ Transaction Safety

Migrilla ensures atomic execution of migrations:

  • Each migration runs in a transaction
  • If any part of the migration fails, everything is rolled back
  • Migration state is updated only after successful execution
  • No partial migrations or inconsistent state

This means your database will always be in a consistent state, even if a migration fails halfway through.

πŸ“– Read the complete Transaction Guide for detailed information about transaction handling.

πŸ“‹ See API Reference for complete method documentation.

πŸš€ View all Features for a comprehensive overview of capabilities.

πŸ“ Requirements

  • Node.js 18.0.0 or higher
  • PostgreSQL database
  • Migration files in migrations/ directory

πŸ§ͺ Testing

# Run basic tests
npm test

# Run detailed down step tests (requires applied migrations)
npm run test:down-step

πŸŽ‰ Key Benefits

  • Zero Dependencies (except pg module)
  • No Configuration Required (works with defaults)
  • Production Ready (atomic operations)
  • Developer Friendly (beautiful output)
  • Extensible (programmatic API)
  • Transaction Safe (automatic rollback on errors)

πŸ“ Project Structure

πŸ“– See PROJECT_STRUCTURE.md for detailed information about project organization and file descriptions.

πŸ“Š View PROJECT SUMMARY for a complete overview of achievements and implementation details.

πŸ”„ Check MIGRATION GUIDE for upgrading between versions.

🀝 Contributing

Issues and pull requests are welcome!

πŸ“„ License

MIT

About

Simple PostgreSQL migration tool for Node.js applications.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published