CLI Examples

Real-world CLI workflows and usage patterns.

Table of contents

  1. Quick Start
    1. Basic Migration
    2. Using Environment Variables
  2. Common Workflows
    1. Pre-Migration Check
    2. Safe Migration with Backup
    3. Dry Run First
  3. Environment-Specific Usage
    1. Development (Local Emulator)
    2. Staging
    3. Production
  4. Multi-Environment Database
  5. Rollback Scenarios
    1. Roll Back to a Target Version
    2. Restore from a Backup
  6. Inspection and Debugging
    1. Check Connection and Configuration
    2. List Root Nodes
    3. Back Up Selected Nodes
  7. Migration Locking
    1. Check Lock Status
    2. Force Release Stuck Lock
  8. Scripting and Automation
    1. Migration Status Check
    2. Safe Production Deploy
    3. Parallel Environment Migrations
  9. Output Formats
    1. Table Format (Default)
    2. JSON Format
  10. Configuration Reference
  11. See Also

Quick Start

Basic Migration

Run migrations with inline credentials:

npx msr-firebase migrate \
  --database-url https://DATABASE-NAME.REGION.firebasedatabase.app \
  --credentials ./serviceAccountKey.json

Using Environment Variables

# Set environment variables
export DATABASE_URL=https://DATABASE-NAME.REGION.firebasedatabase.app
export GOOGLE_APPLICATION_CREDENTIALS=./serviceAccountKey.json

# Run migrations
npx msr-firebase migrate

Common Workflows

Pre-Migration Check

Always validate before running migrations:

# Set these once so each CLI process can connect to the same database.
export DATABASE_URL="https://DATABASE-NAME.REGION.firebasedatabase.app"
export GOOGLE_APPLICATION_CREDENTIALS="./key.json"

# 1. Test connection
npx msr-firebase firebase:test-connection

# 2. List pending migrations
npx msr-firebase list

# 3. Validate migration files
npx msr-firebase validate

# 4. Run migrations
npx msr-firebase migrate \
  --backup-mode full

Safe Migration with Backup

Use Core’s full backup mode to create a backup before migration and restore it automatically if migration execution fails:

npx msr-firebase migrate \
  --database-url "$DATABASE_URL" \
  --credentials "$GOOGLE_APPLICATION_CREDENTIALS" \
  --backup-mode full

For a separate manual backup, run npx msr-firebase backup create; the command prints the file path. Review Backup & Restore before manually restoring data.

Dry Run First

Preview changes without applying:

# Dry run to see what would happen
npx msr-firebase migrate --dry-run \
  --database-url https://DATABASE-NAME.REGION.firebasedatabase.app \
  --credentials ./key.json

# If satisfied, run for real
npx msr-firebase migrate \
  --database-url https://DATABASE-NAME.REGION.firebasedatabase.app \
  --credentials ./key.json

Environment-Specific Usage

Development (Local Emulator)

# Start Firebase emulator
firebase emulators:start --only database

# Run migrations against local emulator
npx msr-firebase migrate \
  --database-url http://localhost:9000?ns=my-project-dev \
  --credentials ./dev-key.json \
  --backup-mode manual

Staging

Core can load an ordered list of environment files. The first file takes precedence when the same variable appears more than once. Set envFileSources in the config file passed to the CLI:

// msr.config.staging.js
module.exports = {
  envFileSources: ['.env.staging.local', '.env.staging', '.env']
};

Core loads its standard MSR_* settings from these files. Provide Firebase-specific values using CLI flags or process environment variables:

npx msr-firebase migrate \
  --config-file ./msr.config.staging.js \
  --database-url "$FIREBASE_DATABASE_URL" \
  --credentials "$GOOGLE_APPLICATION_CREDENTIALS"

Production

# Production with full safety measures
npx msr-firebase migrate \
  --database-url https://PRODUCTION-DATABASE.REGION.firebasedatabase.app \
  --credentials ./prod-key.json \
  --backup-mode full \
  --log-level info

Multi-Environment Database

Using shift paths to namespace environments in a single database:

# Development environment
npx msr-firebase migrate \
  --database-url https://SHARED-DATABASE.REGION.firebasedatabase.app \
  --credentials ./key.json \
  --shift development

# Staging environment
npx msr-firebase migrate \
  --database-url https://SHARED-DATABASE.REGION.firebasedatabase.app \
  --credentials ./key.json \
  --shift staging

# Production environment
npx msr-firebase migrate \
  --database-url https://SHARED-DATABASE.REGION.firebasedatabase.app \
  --credentials ./key.json \
  --shift production

Result:

/development/users
/development/posts
/staging/users
/staging/posts
/production/users
/production/posts

Rollback Scenarios

Roll Back to a Target Version

The CLI requires a numeric target version. It reverses migrations newer than that version:

npx msr-firebase down 202501150001

To roll back every migration, use the version before the first migration (commonly 0):

npx msr-firebase down 0

Restore from a Backup

If migration rollback is not appropriate, restore a backup by its file path:

npx msr-firebase backup restore ./backups/backup-file.bkp

See Backup & Restore for the effects of restoring full and selective backups.


Inspection and Debugging

Check Connection and Configuration

npx msr-firebase firebase:test-connection \
  --database-url "$DATABASE_URL" \
  --credentials "$GOOGLE_APPLICATION_CREDENTIALS"

npx msr-firebase firebase:info

List Root Nodes

npx msr-firebase firebase:nodes

Back Up Selected Nodes

npx msr-firebase firebase:backup-nodes users posts settings

Migration Locking

Check Lock Status

# See if migrations are currently running
npx msr-firebase lock:status

Locked output:

πŸ”’ Lock Status: LOCKED

   Runner ID:  web-server-1-12345-a1b2c3d4
   Acquired At:  2024-01-15T10:30:00.000Z
   Expires At:   2024-01-15T10:40:00.000Z
   Process ID:   12345

Unlocked output:

πŸ”“ Lock Status: UNLOCKED

   No active migration lock

Force Release Stuck Lock

# WARNING: Only use when certain no migration is running
npx msr-firebase lock:release --force

Safety checklist:

  1. Check lock status shows lock is stuck
  2. Verify process holding lock has terminated
  3. Confirm no migrations are running
  4. Consider waiting for lock to expire naturally

Scripting and Automation

Migration Status Check

#!/bin/bash
# Requires DATABASE_URL and GOOGLE_APPLICATION_CREDENTIALS in the environment.

# Check for pending migrations
PENDING=$(npx msr-firebase list --format json | jq -s '[.[] | .pending[]?] | length')

if [ "$PENDING" -gt 0 ]; then
  echo "⚠️  $PENDING pending migrations found"
  npx msr-firebase list
else
  echo "βœ“ All migrations applied"
fi

Safe Production Deploy

#!/bin/bash
set -e

# Provide these through the deployment environment or export them here.
export DATABASE_URL="$PROD_DATABASE_URL"
export GOOGLE_APPLICATION_CREDENTIALS="./prod-key.json"

echo "πŸ” Validating migrations..."
npx msr-firebase validate

echo "πŸ’Ύ Creating backup..."
# Configure backupMode=full on the migration command to create and restore backups automatically.

echo "πŸš€ Running migrations..."
if npx msr-firebase migrate \
  --backup-mode full; then
  echo "βœ“ Migrations successful"
else
  echo "βœ— Migrations failed; Core attempted restoration according to backupMode."
  exit 1
fi

Parallel Environment Migrations

#!/bin/bash

ENVIRONMENTS=("development" "staging")

for env in "${ENVIRONMENTS[@]}"; do
  echo "Migrating $env..."
  npx msr-firebase migrate \
    --database-url "https://SHARED-DATABASE.REGION.firebasedatabase.app" \
    --credentials ./key.json \
    --shift "$env" &
done

wait
echo "All environments migrated"

Output Formats

Table Format (Default)

npx msr-firebase list

The renderer prints separate tables for available groups: Migrated (timestamp, name, execution time, duration, username, and local-file status), Pending (timestamp, name, and path), and Ignored Scripts (timestamp, name, and path). Empty groups are omitted.

JSON Format

npx msr-firebase list --format json

The JSON renderer writes separate grouped objects for available categories such as banner, migrated, pending, and ignored; it does not return one flat array with status fields. For example, it may write:

{
  "migrated": [
    {
      "timestamp": 202501220100,
      "name": "V202501220100_create_users.ts",
      "executed": "2025-01-22T10:00:00.000Z",
      "duration": 2.5,
      "foundLocally": true
    }
  ]
}
{
  "pending": [
    {
      "timestamp": 202501220200,
      "name": "V202501220200_add_email.ts",
      "path": "./migrations/V202501220200_add_email.ts"
    }
  ]
}

Use jq -s to process those separate JSON objects:

PENDING=$(npx msr-firebase list --format json | jq -s '[.[] | .pending[]?] | length')
echo "Pending migrations: $PENDING"

Configuration Reference

For logger, log-level, format, and config-file options, see Configuration. For complete pipeline examples, see CI/CD Integration.


See Also