CLI Examples
Real-world CLI workflows and usage patterns.
Table of contents
- Quick Start
- Common Workflows
- Environment-Specific Usage
- Multi-Environment Database
- Rollback Scenarios
- Inspection and Debugging
- Migration Locking
- Scripting and Automation
- Output Formats
- Configuration Reference
- 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:
- Check lock status shows lock is stuck
- Verify process holding lock has terminated
- Confirm no migrations are running
- 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
- Commands - Complete command reference
- Configuration - CLI flags and options
- CI/CD Integration - Pipeline examples