SurrealDB CLI Migrations for Node.js:
- Edit your database in Surrealist, then generate a migration with Surrealigrate
- Versioned migration files such as
0001.do.sql,0002.undo.sql, etc. - Migrate to the latest version or a specific version
- Rollback to previous migration or a specific version
- Display current migration status with pending migrations
- Built-in help
- Supports YAML configuration files, environment variables, and a combination of both
Using npm:
npm install surrealigrate --save-dev- Create your database
- Run
npx surrealdb infoto verify your database - Run
npx surrealdb extractto create an introspection snapshot - Make changes to your database schema in Surrealist
- Run
npx surrealdb generate -n testto create a migration files, such as0001.do.test.surqland0001.undo.test.surql - Run
npx surrealdb fastforwardto do a version only migration (you already made the changes manually) - Use
npx surrealdb rollbackfollowed bynpx surrealdb migrateto test your migration - Repeat steps 3 - 7
- Create your database
- Run
npx surrealdb infoto verify your database - Run
npx surrealdb migrate
The migrations folder defaults to ./migrations but can be configured using the -d or --dir option with any command or in a configuration file.
- The migration file naming convention is
0001.do.surqlfor the first migration,0002.do.surqlfor the second migration, and so on. - You can also use a title in the migration file name, such as
0001.do.posts.surqland0002.undo.posts.surql. - The version number is padded with leading zeros to ensure proper sorting order, you can use any number of leading zeros as long as they are consistent throughout the migration files.
- Version numbers simplify to an integer, so you can use --to 3 to specify a migration file that uses
003in the file name. - Version numbers can be configured using the
digitssetting for generated migrations to follow your naming convention. - The
doorundosegment indicates whether the file is for a migration (do) or a rollback (undo).
database:
url: 'http://127.0.0.1:8000/rpc'
user: 'myuser'
pass: 'mypassword'
namespace: 'myNamespace'
dbname: 'myDatabase'
migrations:
folder: './migrations'
digits: 4SURREAL_URL=http://127.0.0.1:8000/rpc
SURREAL_USER=myuser
SURREAL_PASS=mypassword
SURREAL_NAMESPACE=myNamespace
SURREAL_DATABASE=myDatabase
SURREAL_MIGRATIONS_DIGITS=4
SURREAL_MIGRATIONS_FOLDER='./migrations'npx surrealdb --help
npx surrealdb extract --help
npx surrealdb generate --help
npx surrealdb migrate -- --help
npx surrealdb rollback --help
npx surreald info --help
- Creates an introspection snapshot to be used when generating the next migration
- Must be manually ran between each generated migration
npx surrealdb extract
- Can be ran multiple times to override a new migration during development
- Requires a snapshot of the previous migration to be created using
extract
npx surrealdb generate
npx surreald migrate
npx surreald migrate --to 3
3is migration up to 0003.do.surql- Includes any migrations prior to the specified version
- Migrate version without changing database schema
- Useful during development; generate a migration from introspection, then skip to the correct migration status with
fastforward
npx surrealdb fastforward
- Useful during development; fastforward then use rollback, finally perform a test migration
npx surreald rollback
npx surreald rollback --to 2
2rollsback down to 0002.undo.surql- Removes any migrations after the specified version
npx surreald migrate
npx surreald migrate -c ./config.yml
DB_USER=admin DB_PASS=secretpassword npx surreald migrate
A migrations.log file will be created in the `/logs folder to log all migration activity.
Surrealigrate is licensed under the MIT License. You are free to use it in your projects, commercial or non-commercial, as long as you retain the copyright notice and license text. Please note that the authors of Surrealigrate are not responsible for any damages or losses caused by the use of this software. See the LICENSE file for more details.
Copyright (c) 2024-2025 David Dyess II and contributors. All rights reserved.