This comprehensive reference covers all nself db commands available in v0.2.0. These commands provide complete database management capabilities, from schema design to production maintenance.
The nself db command suite is organized into logical groups:
nself db init - Initialize database managementnself db validate - Validate schema syntax and structurenself db diff - Compare schema versionsnself db generate:from-database - Generate DBML from existing databasenself db run - Generate migrations from schema changesnself db migrate:up - Apply pending migrationsnself db migrate:down - Rollback applied migrationsnself db migrate:status - Check migration statusnself db seed - Apply seed datanself db seed:generate - Generate seed templatesnself db seed:refresh - Refresh seed datanself db backup - Create database backupnself db restore - Restore from backupnself db backup:list - List available backupsInitialize database management for your project.
nself db init [options]--template <name> - Use a predefined schema template--from-existing - Generate schema from existing database--force - Overwrite existing configuration# Initialize with default blog template
nself db init --template blog
# Initialize from existing database
nself db init --from-existing
# Force reinitialize
nself db init --forceValidate your DBML schema for syntax errors and consistency issues.
nself db validate [options] [schema-file]--verbose - Show detailed validation output--performance - Include performance recommendations--breaking-changes - Check for breaking changes--fix - Automatically fix common issues# Validate current schema
nself db validate
# Validate with performance checks
nself db validate --performance --verbose
# Check for breaking changes
nself db validate --breaking-changesGenerate SQL migrations from schema changes.
nself db run [options]--message <text> - Custom migration message--dry-run - Preview migration without creating files--auto-apply - Automatically apply after generation--force - Generate even if no changes detected# Generate migration with custom message
nself db run --message "Add user profiles table"
# Preview migration changes
nself db run --dry-run
# Generate and apply immediately
nself db run --auto-applyApply pending database migrations.
nself db migrate:up [options]--steps <number> - Apply specific number of migrations--to <version> - Migrate to specific version--force - Skip confirmation prompts--verbose - Show detailed output# Apply all pending migrations
nself db migrate:up
# Apply only next 2 migrations
nself db migrate:up --steps 2
# Migrate to specific version
nself db migrate:up --to 005Apply seed data to your database.
nself db seed [options]--env <environment> - Specify environment (development/staging/production)--tables <list> - Seed specific tables only--fresh - Clear existing data before seeding--force - Skip safety confirmations# Seed development data
nself db seed
# Seed production with specific tables
nself db seed --env production --tables users,roles
# Fresh seed (clear and repopulate)
nself db seed --freshCreate a backup of your database.
nself db backup [options]--name <backup-name> - Custom backup name--compress - Compress backup file--include-seeds - Include seed data tables--exclude-tables <list> - Exclude specific tables# Create timestamped backup
nself db backup
# Create named compressed backup
nself db backup --name pre-migration-backup --compress
# Backup with seed data
nself db backup --include-seedsThese options work with all nself db commands:
--config <path> - Use custom configuration file--database-url <url> - Override database connection--quiet - Suppress non-error output--debug - Enable debug logging--help - Show command helpConfigure database commands with environment variables:
# Database connection
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_DB=myapp
POSTGRES_USER=postgres
POSTGRES_PASSWORD=password
# Command behavior
NSELF_DB_AUTO_CONFIRM=false
NSELF_DB_BACKUP_DIR=./backups
NSELF_DB_MIGRATION_TIMEOUT=300
# Development settings
NSELF_DB_DEBUG=false
NSELF_DB_DRY_RUN=falseAll nself db commands use standard exit codes:
0 - Success1 - General error2 - Invalid usage/arguments3 - Database connection error4 - Schema validation error5 - Migration errordatabase/schema.dbmlnself db validatenself db runnself db migrate:upnself db seednself db backup --compressnself db migrate:up --dry-runnself db migrate:up --forcenself db status# Test database connection
nself db status
# Use custom connection
nself db status --database-url postgresql://user:pass@host:port/db# Check migration status
nself db migrate:status
# Reset migration state (dangerous)
nself db migrate:reset
# Force apply specific migration
nself db migrate:up --to 003 --force# Get detailed validation info
nself db validate --verbose
# Auto-fix common issues
nself db validate --fix
# Check performance issues
nself db validate --performance# .github/workflows/database.yml
name: Database Migration
on:
push:
paths: ['database/**']
jobs:
migrate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install nself
run: curl -fsSL nself.org/install.sh | bash
- name: Validate schema
run: nself db validate --breaking-changes
- name: Create backup
run: nself db backup --name "pre-${{ github.sha }}"
- name: Apply migrations
run: nself db migrate:up --force
env:
POSTGRES_URL: ${{ secrets.DATABASE_URL }}# docker-compose.yml
services:
db-manager:
image: nself/cli:latest
depends_on:
- postgres
volumes:
- ./database:/app/database
command: ["nself", "db", "migrate:up", "--force"]
environment:
- POSTGRES_HOST=postgres
- POSTGRES_DB=myappNow that you understand the CLI database commands:
The database CLI commands provide comprehensive database management capabilities for development and production workflows.