LogoLogo
HomeAPIBlog
3.0.0-SNAPSHOT
3.0.0-SNAPSHOT
  • INTRODUCTION
    • Getting Started
      • Running Local Development Servers
      • Beginner Tutorial: Hello World
      • Beginner Tutorial: Hello Database
      • Tutorial: Wheels, AJAX, and You
    • Frameworks and Wheels
    • Requirements
    • Manual Installation
    • Upgrading
    • Screencasts
  • Command Line Tools
    • CLI Overview
    • Quick Start Guide
    • Command Reference
      • Core Commands
        • wheels init
        • wheels info
        • wheels reload
        • wheels deps
        • wheels destroy
        • wheels watch
      • Code Generation
        • wheels generate app
        • wheels generate app-wizard
        • wheels generate controller
        • wheels generate model
        • wheels generate view
        • wheels generate property
        • wheels generate route
        • wheels generate resource
        • wheels generate api-resource
        • wheels generate frontend
        • wheels generate test
        • wheels generate snippets
        • wheels scaffold
      • Database Commands
        • wheels dbmigrate info
        • wheels dbmigrate latest
        • wheels dbmigrate up
        • wheels dbmigrate down
        • wheels dbmigrate reset
        • wheels dbmigrate exec
        • wheels dbmigrate create blank
        • wheels dbmigrate create table
        • wheels dbmigrate create column
        • wheels dbmigrate remove table
        • wheels db schema
        • wheels db seed
      • Testing Commands
        • wheels test
        • wheels test run
        • wheels test coverage
        • wheels test debug
      • Configuration Commands
        • wheels config list
        • wheels config set
        • wheels config env
      • Environment Management
        • wheels env
        • wheels env setup
        • wheels env list
        • wheels env switch
      • Plugin Management
        • wheels plugins
        • wheels plugins list
        • wheels plugins install
        • wheels plugins remove
      • Code Analysis
        • wheels analyze
        • wheels analyze code
        • wheels analyze performance
        • wheels analyze security
      • Security Commands
        • wheels security
        • wheels security scan
      • Performance Commands
        • wheels optimize
        • wheels optimize performance
      • Documentation Commands
        • wheels docs
        • wheels docs generate
        • wheels docs serve
      • CI/CD Commands
        • wheels ci init
      • Docker Commands
        • wheels docker init
        • wheels docker deploy
      • Deployment Commands
        • wheels deploy
        • wheels deploy audit
        • wheels deploy exec
        • wheels deploy hooks
        • wheels deploy init
        • wheels deploy lock
        • wheels deploy logs
        • wheels deploy proxy
        • wheels deploy push
        • wheels deploy rollback
        • wheels deploy secrets
        • wheels deploy setup
        • wheels deploy status
        • wheels deploy stop
    • CLI Development Guides
      • Creating Commands
      • Service Architecture
      • Migrations Guide
      • Testing Guide
  • Working with Wheels
    • Conventions
    • Configuration and Defaults
    • Directory Structure
    • Switching Environments
    • Testing Your Application
    • Using the Test Environment
    • Contributing to Wheels
    • Submitting Pull Requests
    • Documenting your Code
  • Handling Requests with Controllers
    • Request Handling
    • Rendering Content
    • Redirecting Users
    • Sending Files
    • Sending Email
    • Responding with Multiple Formats
    • Using the Flash
    • Using Filters
    • Verification
    • Event Handlers
    • Routing
    • URL Rewriting
      • Apache
      • IIS
      • Tomcat
      • Nginx
    • Obfuscating URLs
    • Caching
    • Nesting Controllers
    • CORS Requests
  • Displaying Views to Users
    • Pages
    • Partials
    • Linking Pages
    • Layouts
    • Form Helpers and Showing Errors
    • Displaying Links for Pagination
    • Date, Media, and Text Helpers
    • Creating Custom View Helpers
    • Localization
  • Database Interaction Through Models
    • Object Relational Mapping
    • Creating Records
    • Reading Records
    • Updating Records
    • Deleting Records
    • Column Statistics
    • Dynamic Finders
    • Getting Paginated Data
    • Associations
    • Nested Properties
    • Object Validation
    • Object Callbacks
    • Calculated Properties
    • Transactions
    • Dirty Records
    • Soft Delete
    • Automatic Time Stamps
    • Database Migrations
      • Migrations in Production
    • Using Multiple Data Sources
  • Plugins
    • Installing and Using Plugins
    • Developing Plugins
    • Publishing Plugins
  • Project Documentation
    • Overview
  • External Links
    • Source Code
    • Issue Tracker
    • Sponsor Us
    • Community
Powered by GitBook
LogoLogo
On this page
  • Synopsis
  • Description
  • Subcommands
  • export
  • import
  • diff
  • validate
  • Global Options
  • --env
  • --datasource
  • --format
  • Export Command
  • Synopsis
  • Options
  • Examples
  • Import Command
  • Synopsis
  • Options
  • Examples
  • Diff Command
  • Synopsis
  • Options
  • Examples
  • Use Cases
  • Version Control
  • Environment Setup
  • Database Documentation
  • Continuous Integration
  • Backup and Recovery
  • Schema File Formats
  • SQL Format (Default)
  • JSON Format
  • CFM Format
  • Best Practices
  • 1. Regular Exports
  • 2. Environment Comparison
  • 3. Schema Validation
  • 4. Backup Strategy
  • Integration with Migrations
  • Schema vs Migrations
  • Workflow
  • Notes
  • Related Commands

Was this helpful?

Edit on GitHub
Export as PDF
  1. Command Line Tools
  2. Command Reference
  3. Database Commands

wheels db schema

Export and import database schema definitions.

Synopsis

wheels db schema [command] [options]

Description

The db schema command provides tools for exporting database schemas to files and importing them back. This is useful for version control, documentation, sharing database structures, and setting up new environments.

Subcommands

export

Export database schema to a file

import

Import database schema from a file

diff

Compare two schema files or database states

validate

Validate a schema file without importing

Global Options

--env

  • Type: String

  • Default: development

  • Description: The environment to work with

--datasource

  • Type: String

  • Default: Application default

  • Description: Specific datasource to use

--format

  • Type: String

  • Default: sql

  • Options: sql, json, xml, cfm

  • Description: Output format for schema

Export Command

Synopsis

wheels db schema export [options]

Options

--output

  • Type: String

  • Default: db/schema.sql

  • Description: Output file path

--tables

  • Type: String

  • Default: All tables

  • Description: Comma-separated list of tables to export

--no-data

  • Type: Boolean

  • Default: true

  • Description: Export structure only, no data

--include-drops

  • Type: Boolean

  • Default: false

  • Description: Include DROP statements

--include-indexes

  • Type: Boolean

  • Default: true

  • Description: Include index definitions

--include-constraints

  • Type: Boolean

  • Default: true

  • Description: Include foreign key constraints

Examples

# Export entire schema
wheels db schema export

# Export specific tables
wheels db schema export --tables=user,order,product

# Export as JSON
wheels db schema export --format=json --output=db/schema.json

# Export with DROP statements
wheels db schema export --include-drops --output=db/recreate-schema.sql

Import Command

Synopsis

wheels db schema import [options]

Options

--input

  • Type: String

  • Default: db/schema.sql

  • Description: Input file path

--dry-run

  • Type: Boolean

  • Default: false

  • Description: Preview import without executing

--force

  • Type: Boolean

  • Default: false

  • Description: Drop existing objects before import

--skip-errors

  • Type: Boolean

  • Default: false

  • Description: Continue on errors

Examples

# Import schema
wheels db schema import

# Import from specific file
wheels db schema import --input=db/production-schema.sql

# Preview import
wheels db schema import --dry-run

# Force recreate schema
wheels db schema import --force

Diff Command

Synopsis

wheels db schema diff [source] [target] [options]

Options

--output

  • Type: String

  • Default: Console output

  • Description: Save diff to file

--format

  • Type: String

  • Default: text

  • Options: text, sql, html

  • Description: Diff output format

Examples

# Compare dev and production
wheels db schema diff --env=development --env=production

# Compare schema files
wheels db schema diff db/schema-v1.sql db/schema-v2.sql

# Generate migration SQL
wheels db schema diff --env=development --env=production --format=sql

Use Cases

Version Control

Track schema changes in git:

# Export schema after migrations
wheels dbmigrate latest
wheels db schema export

# Commit schema file
git add db/schema.sql
git commit -m "Update database schema"

Environment Setup

Set up new developer environment:

# Clone repository
git clone repo-url

# Import schema
wheels db schema import

# Run any pending migrations
wheels dbmigrate latest

Database Documentation

Generate schema documentation:

# Export as JSON for documentation tools
wheels db schema export --format=json --output=docs/database-schema.json

# Export with comments
wheels db schema export --include-comments --output=docs/schema-annotated.sql

Continuous Integration

Validate schema in CI:

# Export current schema
wheels db schema export --output=current-schema.sql

# Compare with committed schema
wheels db schema diff db/schema.sql current-schema.sql

Backup and Recovery

Create schema backups:

# Backup before major changes
wheels db schema export --output=backups/schema-$(date +%Y%m%d).sql

# Include drops for full recreate
wheels db schema export --include-drops --output=backups/recreate-$(date +%Y%m%d).sql

Schema File Formats

SQL Format (Default)

-- Generated by CFWheels
-- Date: 2024-01-15 10:30:00

CREATE TABLE user (
    id INT PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(50) NOT NULL,
    email VARCHAR(150) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE UNIQUE INDEX idx_user_email ON user(email);

JSON Format

{
  "version": "1.0",
  "generated": "2024-01-15T10:30:00Z",
  "tables": {
    "user": {
      "columns": {
        "id": {
          "type": "integer",
          "primaryKey": true,
          "autoIncrement": true
        },
        "username": {
          "type": "string",
          "length": 50,
          "nullable": false
        }
      },
      "indexes": {
        "idx_user_email": {
          "columns": ["email"],
          "unique": true
        }
      }
    }
  }
}

CFM Format

<cfscript>
schema = {
    tables: {
        user: {
            columns: [
                {name: "id", type: "integer", primaryKey: true},
                {name: "username", type: "string", length: 50},
                {name: "email", type: "string", length: 150}
            ],
            indexes: [
                {name: "idx_user_email", columns: ["email"], unique: true}
            ]
        }
    }
};
</cfscript>

Best Practices

1. Regular Exports

Export schema after each migration:

# In deployment script
wheels dbmigrate latest
wheels db schema export
git add db/schema.sql
git commit -m "Update schema after migrations"

2. Environment Comparison

Regularly compare environments:

# Weekly check
wheels db schema diff --env=development --env=production

3. Schema Validation

Validate before deployment:

# In CI pipeline
wheels db schema validate --input=db/schema.sql

4. Backup Strategy

Maintain schema history:

# Before major updates
wheels db schema export --output=db/history/schema-$(git rev-parse --short HEAD).sql

Integration with Migrations

Schema vs Migrations

  • Migrations: Incremental changes

  • Schema: Current state snapshot

Workflow

  1. Run migrations: wheels dbmigrate latest

  2. Export schema: wheels db schema export

  3. Commit both: git add db/migrate/* db/schema.sql

Notes

  • Schema export captures current database state

  • Some database-specific features may not export perfectly

  • Always review exported schemas before importing

  • Use migrations for incremental changes, schemas for full setup

Related Commands

Previouswheels dbmigrate remove tableNextwheels db seed

Last updated 2 days ago

Was this helpful?

- Run all migrations

- View migration status

- Seed database with data

- Generate models from schema

wheels dbmigrate latest
wheels dbmigrate info
wheels db seed
wheels generate model