1. MooseStack
  2. Migrations

On this page

How Migrations WorkCore ConceptsLifecycle ManagementGenerating MigrationsApplying ChangesAdvanced Topics

Migrations

Migrations synchronize your code-defined database schema with your production infrastructure. As your application evolves, you'll add/remove fields, change data types, and restructure tables. Moose Migrate handles these schema changes safely and reliably.

How Migrations Work

Moose Migrate operates by comparing two states:

  1. Your code - Tables and streams defined in your application
  2. Your database - The actual schema in ClickHouse, Kafka, or Redpanda

When these states differ, Moose Migrate generates operations to bring them into alignment. These operations might include:

  • Adding or dropping tables
  • Adding, removing, or renaming columns
  • Changing data types
  • Creating or modifying streaming topics

The full list of operations is available in the Migration Plan Format documentation.

Core Concepts

Migrations in Moose revolve around three key decisions:

ConceptWhat it isWhere to go
Lifecycle ManagementControlling what changes are allowed (e.g., preventing data deletion).Overview • Fully Managed • Deletion Protected • Externally Managed
Generating MigrationsDeciding how changes are generated.Overview • Auto-Inferred • Planned
Applying ChangesThe workflow for executing migrations: serverless (moose migrate) vs server runtime (moose prod).Serverless • Server Runtime

Lifecycle Management

For each table and stream resource defined in your code, you can control what changes are allowed (e.g., preventing data deletion, ignoring schema changes, etc.) with the LifeCycle configuration property:

import { OlapTable, LifeCycle } from "@514labs/moose-lib"; interface Schema {  id: string;  name: string;  age: number;} const table = new OlapTable<Schema>("table_name", {  lifeCycle: LifeCycle.FULLY_MANAGED  orderByFields: ["id"]});
OptionBehaviorUse When
FULLY_MANAGED (default)Automatically modifies resources to match your code, including destructive operations.When you're developing new tables that you want your application to manage and evolve over time.
DELETION_PROTECTEDAutomatically modifies resources to match your code, but blocks destructive operations (drops, deletions).When you want to protect critical production tables from accidental data loss.
EXTERNALLY_MANAGEDDoes not modify resources. You manage the schema manually directly in your database.When you have existing tables that you want to manage outside of your application, or if you're using a Managed CDC service like ClickPipes or PeerDB to manage your schema.
Per-resource configuration

You configure lifecycle modes individually on each OlapTable and Stream object. This allows you to mix fully managed development tables with deletion-protected production tables and externally managed legacy resources in the same application.

Compare Lifecycle Management Modes →

Generating Migrations

Moose Migrate provides two complementary ways to generate migrations. Each is designed for use in different stages of the application lifecycle, and it's best practice to use both in your workflow:

OptionBehaviorUse Case
Auto-InferredUpdates database instantly on file save. Fast iteration, but can be destructive.Local development, fast prototyping
PlannedGenerates reviewable plan files. Safe, deterministic, with drift detection.Production deployment, CI/CD

Compare Migration Modes →

Applying Changes

You have two options for executing planned migrations against your production database:

ModeBehaviorUse Case
Serverless (moose migrate)You run migrations manually or via CI/CD.Integrating Moose OLAP into an existing application as a library.
Server Runtime (moose prod)Migrations are automatically run within the Moose Runtime on server startup.Building a dedicated analytics service with the full Moose Runtime.

Advanced Topics

  • Failed Migrations - Recover from migration errors
FiveonefourFiveonefour
Fiveonefour Docs
MooseStackTemplates
Changelog
Source506
  • Overview
  • Quick Start
  • Templates / Examples
Fundamentals
  • Moose Runtime
  • MooseDev MCP
  • Data Modeling
MooseStack in your App
  • App / API frameworks
Modules
  • Moose OLAP
  • Moose Streaming
  • Moose Workflows
  • Moose APIs
Deployment & Lifecycle
  • 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
  • Data Types
  • Table Engines
  • CLI
  • Configuration
  • Observability Metrics
  • Help
  • Changelog
Contribution
  • Documentation
  • Framework