Powerful & expressive ORM/query-builder/static checker for raw queries/Fully Automated migration tooling , designed to offer an intuitive API, strict type-checking, novel features, & full specification support. It provides a fresh perspective in data management. Currently supports SurrealDB engine. RDMSs(PG, MYSQL etc) and others coming soon
This PR introduces a comprehensive overhaul of the Migration CLI, streamlining its functionality for a more intuitive and efficient user experience. It's a step forward in simplifying and unifying the migration process in SurrealDB environments.
Streamlined CLI Interface: Redesigned to ensure consistency and ease of use. All database connection flags are now universally applicable at the top level.
New Commands implemented: Introducing init, reset, list, and prune for expanded control over migration processes.
Direct Execution Post-Generation: The gen, init, and reset commands now offer optional execution against the current live database instance post-migration generation.
Simplified Migration Strategies: The gen, up and down commands have been refined for straightforward execution, eliminating the need for the --reversible flag during routine operations.
Automated Testing Suite: Comprehensive integration and end-to-end tests cover all migration operations, including edge cases.
Enhanced Renaming and Deletion Operations: Automatic and manual renaming and deletion processes have been optimized for better user control and understanding.
Surreal ORM provides a powerful command-line interface (CLI) for automatically diffing and managing database migrations in a SurrealDB environment. This tool offers functionalities ranging from initializing migrations, generating migration files, applying migrations up or down,
resetting migrations, listing migrations, pruning pending migration files and various other tasks to manage your database schema effectively.
It supports both CLI and Emdded-migrations. Embedded migration includes the migration files
data into your binary at compile time and is accessible to your binary at runtime.
Usage
It involves several processes including gathering codebase resources,
then setting those up for initializing and generating new migrations.
Step 1: Setting Up And Gathering Codebase Resources
Step 2: Running the CLI and/or Embedding Migrations
The CLI tool offers a range of commands, each with specific options and flags. Here's a quick overview:
Initialize Migrations:
cargo run -- init --name "initial migration" -r
This initializes the migrations directory with a reversible migration named "initial_migration".
Omit the -r flag if you want up only migrations.
Generate Migrations:
cargo run -- gen --name "add users table"
Generates a new migration file named "add_users_table".
Notice that we do not need to include the -r or --reversiable flag.
Because we specified whether we want a reversible or non-reversible migration when we initialized,
the migration type is automatically detected subsequently.
Apply Migrations Up:
# Applies all pending till latest by default
cargo run -- up
# Applies all pending till latest
cargo run -- up -l
# Applies by the number specified
cargo run -- up -n 5
cargo run -- up --number 5
# Applies till specified migration
cargo run -- up -t "20240107015727114_create_first.up.surql"
cargo run -- up --till "20240107015727114_create_first.up.surql"
Applies pending migrations forward using various strategies: till latest, by number count and till a specified migration.
Rollback Migrations:
# Rollback migration to previous by default
cargo run -- down
# Rollback all pending till previous
cargo run -- down --previous
# Rollback by the number specified
cargo run -- down -n 5
cargo run -- down --number 5
# Rollback till specified migration
cargo run -- down -t "20240107015727114_create_first.up.surql"
cargo run -- down --till "20240107015727114_create_first.up.surql"
# In addition, you can use the --prune flag to delete local migration
# files after rolling back. This can be useful in development for rapid changes.
cargo run -- down -n 5 --prune
Rolls back the last applied migration.
Reset Migrations:
cargo run -- reset --name "initial_migration" -r
Resets all migrations and initializes a new reversible migration named "initial_migration".
Skip the -r or --reversible flag if you want up only migrations,
Prune Migrations:
# List pending migrations by default
cargo run -- prune
Prune all pending unapplied migrations.
List Migrations:
# List pending migrations by default
cargo run -- ls
cargo run -- list
# List all migrations
cargo run -- list --status all
# List pending migrations
cargo run -- list --status pending
# List applied migrations
cargo run -- list --status applied
Lists migrations by their statuses i.e, all, pending and applied.
Advanced Migration CLI Usage
Advanced usage involves specifying additional flags and options to tailor the migration process to your specific needs. Here's how you can use these advanced features:
Custom Migration Directory:
cargo run -- init --name "initial_migration" --dir "custom_migrations" -r
Initializes migrations in a custom directory named "custom_migrations".
Verbose Output:
cargo run -- up -vvv
Runs migrations with 3 levels verbose output.
Database Connection Configuration:
URL: ws://localhost:8000
Database Name: test
Namespace: test
User: root
Password: root
cargo run -- up --url "ws://localhost:8000" --db "mydb" --ns "myns" --user "username" --pass "password"
Connects to the specified SurrealDB instance with custom credentials and applies migrations.
Overview
This PR introduces a comprehensive overhaul of the Migration CLI, streamlining its functionality for a more intuitive and efficient user experience. It's a step forward in simplifying and unifying the migration process in SurrealDB environments.
init
,reset
,list
, andprune
for expanded control over migration processes.gen
,init
, andreset
commands now offer optional execution against the current live database instance post-migration generation.gen
,up
anddown
commands have been refined for straightforward execution, eliminating the need for the--reversible
flag during routine operations.More details
Migration: Fully Automated Database Schema Migrations
Surreal ORM provides a powerful command-line interface (CLI) for automatically diffing and managing database migrations in a SurrealDB environment. This tool offers functionalities ranging from initializing migrations, generating migration files, applying migrations up or down, resetting migrations, listing migrations, pruning pending migration files and various other tasks to manage your database schema effectively.
It supports both CLI and Emdded-migrations. Embedded migration includes the migration files data into your binary at compile time and is accessible to your binary at runtime.
Usage
It involves several processes including gathering codebase resources, then setting those up for initializing and generating new migrations.
Step 1: Setting Up And Gathering Codebase Resources
Step 2: Running the CLI and/or Embedding Migrations
The CLI tool offers a range of commands, each with specific options and flags. Here's a quick overview:
Initialize Migrations:
This initializes the migrations directory with a reversible migration named "initial_migration". Omit the
-r
flag if you want up only migrations.Generate Migrations:
Generates a new migration file named "add_users_table". Notice that we do not need to include the
-r
or--reversiable
flag. Because we specified whether we want a reversible or non-reversible migration when we initialized, the migration type is automatically detected subsequently.Apply Migrations Up:
Applies pending migrations forward using various strategies: till latest, by number count and till a specified migration.
Rollback Migrations:
Rolls back the last applied migration.
Reset Migrations:
Resets all migrations and initializes a new reversible migration named "initial_migration". Skip the
-r
or--reversible
flag if you want up only migrations,Prune Migrations:
Prune all pending unapplied migrations.
List Migrations:
Lists migrations by their statuses i.e,
all
,pending
andapplied
.Advanced Migration CLI Usage
Advanced usage involves specifying additional flags and options to tailor the migration process to your specific needs. Here's how you can use these advanced features:
Custom Migration Directory:
Initializes migrations in a custom directory named "custom_migrations".
Verbose Output:
Runs migrations with 3 levels verbose output.
Database Connection Configuration:
ws://localhost:8000
test
test
root
root
Connects to the specified SurrealDB instance with custom credentials and applies migrations.
Other supported urls types include:
This configuration enables the CLI to connect to different database backends including WebSocket, HTTP(S), In-Memory, File-Backend, and more.
Embedded Migrations