Skip to content
spaceport / docs
The Spaceport handbook

Migrations Overview

Migrations in Spaceport are interactive, CLI-driven Groovy scripts used to prepare, configure, or maintain a Spaceport instance without fully booting the application server. They provide a lightweight runtime with access to your configuration manifest and connected database, making them the standard tool for tasks like creating databases, seeding data, managing user accounts, and performing one-off maintenance operations.

What Migrations Are

A Spaceport migration is a standalone Groovy script that lives in your project's migrations/ directory. When you run the --migrate CLI command, Spaceport loads your configuration manifest, connects to the database, and presents an interactive menu listing all available migration scripts. You select one to execute, and it runs in a small command-line environment with full access to Spaceport's database APIs and an interactive prompt system for gathering user input.

Migrations are not automatically ordered, versioned, or tracked the way they are in many other frameworks. There is no migration history table and no concept of "up" and "down" operations. Each migration is an independent script that you choose to run when you need it. This design favors simplicity and flexibility — you write a script for a specific task, run it when the time comes, and the script itself is responsible for checking preconditions and behaving safely if run more than once.

How Migrations Differ from Other Frameworks

If you have experience with Rails migrations, Django migrations, or Flyway, Spaceport's approach will feel different in several important ways:

When to Use Migrations

Migrations are the right tool when you need to:

Migrations are typically run while the server is stopped, but they can also be executed while the server is running if your workflow requires it.

A Simple Example

The most common migration included with Spaceport projects is CreateSpaceportAdministrator.groovy, which ensures the configured users database exists and creates an administrator account:

import spaceport.Spaceport
import spaceport.bridge.Command
import spaceport.computer.memory.physical.Document
import spaceport.personnel.ClientDocument

Command.with {

    printBox("""
    This migration will create a default Spaceport administrator
    and the configured users database if they do not already exist.
    """)

    // Ensure the users database exists
    Spaceport.main_memory_core.createDatabaseIfNotExists(ClientDocument.usersDatabase())

    // Prompt for credentials
    def username = promptInput('Enter a new username (default: administrator)')
    if (!username) username = 'administrator'

    // Create the user
    def password = ''
    while (!password) {
        password = promptInput('Enter a new password')
        if (password != promptInput('Re-enter password to confirm')) {
            error('Passwords do not match. Please try again.')
            password = null
        }
    }

    def user = ClientDocument.createNewClientDocument(username, password)
    if (user) {
        user.addPermission('spaceport-administrator')
        success("Created new administrator: ${username}")
    } else {
        error("Failed to create new administrator.")
    }
}

This script demonstrates the core migration pattern: explain what will happen, check preconditions, prompt for input, perform database operations, and report the outcome.

Running a Migration

From the command line, pass your configuration manifest path with the --migrate flag:

java -jar spaceport.jar --migrate config.spaceport

Spaceport will load the manifest, connect to the configured database, and display an interactive menu of .groovy files found in the migrations directory. Select a script to execute it.

If the database is not reachable, the CLI will prompt you to continue without a connection or exit.

What's Available (and What's Not)

The migration runtime is deliberately minimal. Spaceport loads only what's needed for database operations and CLI interaction:

Available:

Not available:

This means migration scripts must be self-contained. If you have a helper method in your project that formats user data, you cannot call it from a migration — you need to inline that logic or write it as a closure within the script. This is by design: migrations should work independently of your application code so they can run even when the application itself is broken or mid-refactor.

If you need setup logic that depends on your project's classes — registering CouchDB views, initializing app-specific state, or ensuring databases exist based on your data model — use @Alert('on initialize') or @Alert('on initialized') in your source modules instead. These alerts fire every time the server starts (and on hot-reload), so they have full access to your project code. This is the established pattern: for example, a Document subclass can ensure its own database and views exist each time the application boots:

@Alert('on initialized')
static _init(Result r) {
    Spaceport.main_memory_core.createDatabaseIfNotExists('products')

    ViewDocument.get('views', 'products').setViewIfNeeded('all', '''
        function(doc) { emit(doc._id, null); }
    ''')
}

Use migrations for tasks that happen outside the running application — creating administrator accounts, resetting passwords, seeding data that requires operator input — and on initialize for setup that should happen every time the server starts.

Best Practices

See Also

Unattended Execution

Use --headless with JSON naming the migration and its inputs (or @file.json). Scripts receive report, inputs, and interactive bindings. A single script is auto-selected without a terminal; multiple scripts need an explicit filename. Prompts fail without a terminal, so scripts must read supplied answers rather than calling a prompt unconditionally. See Migrations API.

Make the docs better.Edit this page on GitHub