Skip to content

[FEATURE] Provide Option to pass rollback whole migration directory in down api #476

@jkjsanthoshkumar

Description

@jkjsanthoshkumar

Feature Request: Add Block Rollback to down API

Feature Description

The CLI supports migrate-mongo down -b to rollback all migrations from the same block. The API's down() method lacks this option - it only rolls back one migration at a time.

Request: Add { block: true } option to the down() API method.

Use Case

We use the migrate-mongo API in our deployment automation. When a deployment fails, we need to rollback all migrations that were just applied together (a block), but currently have to call down() multiple times without knowing when the block ends.

Current Problem:

// Applied 3 migrations in one block via up()
await up(db, client); // Applies: migration-1.js, migration-2.js, migration-3.js

// On failure, must rollback one by one
await down(db, client); // Rolls back migration-3.js only
await down(db, client); // Rolls back migration-2.js only
await down(db, client); // Rolls back migration-1.js only
// No way to know how many to call

Proposed Solution

// New: Rollback entire block at once
const migrations = await down(db, client, { block: true });
// Returns: ['migration-3.js', 'migration-2.js', 'migration-1.js']

Example Usage

Before (current workaround)

async function rollbackDeployment() {
  const { db, client } = await database.connect();
  
  // Must manually loop and check status
  let rolledBack = [];
  const status = await status(db);
  const lastTimestamp = status[0]?.appliedAt;
  
  while (true) {
    const result = await down(db, client);
    rolledBack.push(result);
    
    const newStatus = await status(db);
    if (newStatus[0]?.appliedAt !== lastTimestamp) break;
  }
  
  await client.close();
}

After (with this feature)

async function rollbackDeployment() {
  const { db, client } = await database.connect();
  
  const rolledBack = await down(db, client, { block: true });
  console.log(`Rolled back ${rolledBack.length} migrations`);
  
  await client.close();
}

Alternatives Considered

  1. Manual loop - Current workaround shown above. Complex and requires multiple DB queries.
  2. Shell execution - exec('migrate-mongo down -b') - Loses programmatic control.
  3. Direct DB access - Query changelog manually. Bypasses library logic.

All alternatives are inferior to adding this to the API.

Breaking Changes

  • This feature is backwards compatible
  • This feature would require breaking changes

Existing code continues to work:

await down(db, client);              // Still works (rolls back 1)
await down(db, client, { block: true }); // New (rolls back block)

Additional Context

  • The CLI -b flag logic already exists to rollback entire block
  • This provides API/CLI feature parity
  • Common need in automated deployment pipelines

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions