Migrations
Spooky includes a built-in migration system for managing incremental, version-controlled changes to your SurrealDB schema. Instead of applying your full schema on every deploy, migrations let you evolve your database safely over time.
Creating a Migration
Use the create subcommand to scaffold a new migration file:
This creates a timestamped .surql file in your migrations directory.
Options
| Flag | Description |
|---|---|
--schema <path> | Pre-populate the migration with an existing .surql schema file |
--migrations-dir <path> | Directory to store migrations (default: migrations) |
Migration File Format
Migration files follow a strict naming convention:
For example:
Each file contains the SurrealQL statements for that migration:
The name is automatically sanitized — spaces and hyphens become underscores, and special characters are stripped.
Applying Migrations
Run all pending migrations against your database:
Migrations execute in version order (by timestamp). Each applied migration is recorded in a _spooky_migrations tracking table with its version, name, and SHA-256 checksum.
Connection Flags
All connection flags can also be set via environment variables (see below).
Checking Status
View which migrations have been applied and which are still pending:
Example output:
The status command also detects drift — if a migration file was modified after it was applied, you’ll see:
The status command accepts the same connection flags and --migrations-dir option as apply.
Environment Variables
Connection details can be configured through environment variables instead of CLI flags:
| Variable | Default | Description |
|---|---|---|
SURREAL_URL | http://localhost:8000 | SurrealDB server endpoint |
SURREAL_NS | main | SurrealDB namespace |
SURREAL_DB | main | SurrealDB database |
SURREAL_USER | root | Authentication username |
SURREAL_PASS | root | Authentication password |
CLI flags take precedence over environment variables when both are provided.
Safety Features
The migration system includes several safeguards to prevent data loss:
- SHA-256 checksums — Every migration file is hashed when applied. If a file is modified after application, the checksum mismatch is detected and further applies are blocked.
- Drift detection — The
statuscommand flags any applied migration whose file has changed on disk, making it easy to catch accidental edits. - Immutability — Once a migration has been applied, its file should never be modified. If you need to make changes, create a new migration instead.
- Ordered execution — Migrations always run in chronological order based on their timestamp prefix.
If a checksum mismatch is detected during apply, you’ll see an error like:
Bootstrapping from Schema
If you have an existing .surql schema file and want to start using migrations, use the --schema flag to create an initial migration pre-populated with your schema:
This generates a migration file containing your full schema with a warning comment at the top:
For the very first migration this is typically fine as-is — the full schema is the incremental change from an empty database.
CI/CD Integration
Run migrations as part of your deploy pipeline to keep your database in sync:
Running status before apply lets you catch drift or unexpected state before making changes to your production database.