We value your privacy

This site uses cookies to improve your browsing experience, analyze site traffic, and show personalized content. See our Privacy Policy.

  1. MooseStack
  2. Moose Migrate
  3. Planned Migrations

Planned Migrations

Planned migrations are the production-grade schema evolution mechanism in MooseStack. Unlike Automatic Migrations, this system separates the generation of schema changes from their execution, introducing a reviewable artifact (the plan) into your deployment lifecycle.

Command

Generate a migration plan by comparing your local code against a production environment:

# For Server Runtime (connect to Moose Admin API)
moose generate migration --url <ADMIN_API_URL> --token <ADMIN_TOKEN> --save
 
# For Serverless (connect to ClickHouse directly)
moose generate migration --clickhouse-url <CLICKHOUSE_CONNECTION_STRING> --save

Key Benefits:

  • Deterministic: The plan is a static file (plan.yaml) that won't change at runtime.
  • Drift Detection: Snapshots (remote_state.json) ensure the DB hasn't changed since the plan was created.
  • Reviewable: You can audit every operation (e.g., DropColumn, AddTable) before it runs, and you can edit the plan to override Moose's assumptions.
  • Versioned: Commit plans to Git to create a permanent audit trail.

Workflow

The lifecycle consists of four distinct stages:

  1. Code Change — Modify your data models (tables, views) in your Moose project.

  2. Generation — Run the CLI to compare your code against production.

    moose generate migration --save ...
  3. Review — Inspect the generated migrations/plan.yaml file and commit it to Git.

  4. Application — Execute the plan during deployment (if using Moose Runtime), manually via the CLI or your own CI/CD pipeline (if using Serverless).

    moose migrate ...
     
    ## or if using the Moose Runtime
    moose prod

Generated Artifacts

Running the generation command produces three files in the migrations/ directory.

FilePurpose
plan.yamlThe imperative list of operations (e.g., AddTableColumn) to execute. See Plan Reference.
remote_state.jsonA snapshot of the production database schema at the time of generation. Used to detect drift.
local_infra_map.jsonA snapshot of your local code's schema definitions. Used to validate the plan against the code.

Configuration

Planned migrations are active whenever a plan.yaml file exists in the migrations/ directory. No feature flag is required -- if the file is present, moose prod applies the static plan; if it is absent, Moose falls back to dynamic OLAP reconciliation.

moose.config.toml
[features]olap = true

Production Safety Gate

By default, moose prod refuses to start if the computed infrastructure diff contains destructive operations (table drops, column drops, table recreates, view removals) and no plan.yaml is present. This prevents accidental data loss when deploying schema changes that haven't been reviewed.

moose.config.toml
[migration_config]# Default: false — destructive changes blocked unless a plan.yaml is presentprod_auto_allow_destructive = false
ScenarioBehavior
No destructive changesmoose prod starts normally regardless of plan.yaml.
Destructive changes + plan.yaml presentmoose prod applies the reviewed plan and starts normally.
Destructive changes + no plan.yamlmoose prod refuses to start with an error listing the destructive operations.

When startup is blocked, the error message lists every destructive operation and suggests two remediation paths:

  1. Run moose generate migration --save to create a reviewed plan.yaml.
  2. Set prod_auto_allow_destructive = true in moose.config.toml (or MOOSE_MIGRATION_CONFIG__PROD_AUTO_ALLOW_DESTRUCTIVE=true as an environment variable) to opt out of the safety gate.
Opting out

Setting prod_auto_allow_destructive = true restores the pre-safety-gate behavior where moose prod applies destructive changes without a plan file. Only do this if you accept the risk of unreviewed destructive schema changes in production.

Command Options

The moose generate migration command accepts different arguments depending on your deployment model for applying changes.

via Moose Runtime (moose prod)

Connect to the Admin API of the running service.

moose generate migration --url <ADMIN_API_URL> --token <ADMIN_TOKEN> --save
OptionDescription
--urlThe endpoint of your production Moose Admin API.
--tokenThe authentication token for the Admin API.
--saveWrites the generated plan to the migrations/ directory. Without this, it performs a dry run.

via Serverless (moose migrate)

Connect directly to the ClickHouse database.

moose generate migration --clickhouse-url <CLICKHOUSE_CONNECTION_STRING> --save
OptionDescription
--clickhouse-urlDirect connection string (e.g., clickhouse://user:pass@host:9440/db).
--saveWrites the generated plan to the migrations/ directory. Without this, it performs a dry run.

Confirmation flags

These flags control the interactive confirmation prompts during plan generation:

OptionEnvironment VariableDescription
--yes-allMOOSE_ACCEPT_ALL=1Skip all confirmation prompts (renames and destructive operations).
--yes-destructiveMOOSE_ACCEPT_DESTRUCTIVE=1Skip the confirmation prompt for destructive operations.
--yes-renameMOOSE_ACCEPT_RENAME=1Auto-accept detected column renames as genuine renames.
--no-auto-backfill-sql—Disable automatic backfill SQL generation for versioned tables.

Drift Detection

Drift occurs when the target database's schema changes between the time you generate a plan and the time you apply it.

How it works:

  1. moose generate writes the current DB state to remote_state.json.
  2. moose migrate (serverless) or moose prod (server runtime) compares remote_state.json with the current DB state.
  3. If they differ (hash mismatch), the migration aborts.

Resolution: To fix drift, you must regenerate the plan against the new production state.

# Regenerate to accept the new state
moose generate migration ... --save

See Also

  • Migration Plan Reference - Detailed syntax of plan.yaml.
  • Manual Execution - Execute migration plans manually via the moose migrate command.
  • Runtime Execution - Execute migration plans automatically via the Moose Server Runtime.

On this page

CommandWorkflowGenerated ArtifactsConfigurationProduction Safety GateCommand Optionsvia Moose Runtime (`moose prod`)via Serverless (`moose migrate`)Confirmation flagsDrift DetectionSee Also
Edit this page
FiveonefourFiveonefour
Fiveonefour Docs
MooseStackHostingTemplatesGuides
Release Notes
Source563
  • Overview
Build a New App
  • 5 Minute Quickstart
  • Browse Templates
  • Existing ClickHouse
Add to Existing App
  • Next.js
  • Fastify
Fundamentals
  • Moose Runtime
  • MooseDev MCP
  • Language Server
  • Data Modeling
Moose Modules
  • Moose OLAP
  • Moose Streaming
  • Moose Workflows
  • Moose APIs & Web Apps
Deployment & Lifecycle
  • Moose Dev
  • Moose Migrate
    • Migration Modes
    • Automatic Migrations
    • Planned Migrations
    • Plan Reference
    • Executing Migrations
    • moose migrate (CLI)
    • moose prod (Runtime)
    • Lifecycle Management
    • Fully Managed
    • Deletion Protected
    • Externally Managed
    • Advanced Topics
    • Failed Migrations
  • Moose Deploy
Reference
  • API Reference
  • Query Layer
  • Testing Utilities
  • Data Types
  • Table Engines
  • CLI
  • Configuration
  • Observability Metrics
  • Help
  • Release Notes
Contribution
  • Documentation
  • Framework