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
85
stars
2
forks
source link
42 implement fully automated db schema migration #44
This pull request adds support for database migration workflow, focusing on the user experience with new CLI commands for generating and managing migrations. The updates aim to provide a more intuitive and efficient process for handling database changes, ensuring that developers can easily create, apply, roll back, and list migrations with minimal friction.
Features
CLI Migration Generation: Developers can now generate new migration files directly from the command line using the cargo run -- generate command. This feature streamlines the creation of migration files and diffs, automatically handling the naming and placement within the specified migrations directory.
Embedded Migrations: Migrations can be embedded into the codebase as constants, allowing for easy access and manipulation within the application logic. This is facilitated through the embed_migrations! macro, which simplifies the process of including migrations in the build.
One-Way and Two-Way Migrations: The system supports both one-way and two-way migrations, providing flexibility in how changes are applied and rolled back. This ensures that developers can choose the most appropriate strategy for their database schema evolution.
Migration Application and Rollback: Applying and rolling back migrations is now as simple as running cargo run -- run and cargo run -- rollback commands, respectively. These commands interact with the database directly, applying the necessary changes or reverting them as needed.
Testing and Validation: The PR includes tests that validate the success of the migration commands, ensuring that the generated files are correct and that the commands interact with the database as expected.
use surreal_models::migrations::Resources;
use surreal_orm::migrator::cli;
use surrealdb::engine::any::{connect, Any};
#[tokio::main]
async fn main() {
// Option 1: Db connection is setup for you depending on CLI arguments
cli::migration_cli(Resources, None).await;
// Option 2:
// Create your own db connection
// let db = setup_db().await;
// cli::migration_cli(Resources, Some(db)).await;
}
Generate a New Migration:
cargo run -- generate --name [migration_name]
cargo run -- generate --name "create user"
This command will create new migration files in the specified directory.
Apply Migrations to the Database:
cargo run -- run --db [database] --ns [namespace] --path [db_path]
cargo run -- run --db test --ns test --path http://localhost:8000
This will apply all pending migrations in the specified directory to the database.
Roll Back Applied Migrations:
cargo run -- rollback --db [database] --ns [namespace] --path [db_path]
cargo run -- rollback --db test --ns test --path http://localhost:8000
cargo run -- rollback --db test --ns test --path http://localhost:8000 --latest
cargo run -- rollback --db test --ns test --path http://localhost:8000 --by_count 2
cargo run -- rollback --db test --ns test --path http://localhost:8000 --till <id>
This command will roll back migrations that have been applied, in reverse order.
List Migrations:
cargo run -- list --db test --ns test
Conclusion
The enhancements to the database migration workflow represent a significant improvement in the developer experience.
Overview
This pull request adds support for database migration workflow, focusing on the user experience with new CLI commands for generating and managing migrations. The updates aim to provide a more intuitive and efficient process for handling database changes, ensuring that developers can easily create, apply, roll back, and list migrations with minimal friction.
Features
CLI Migration Generation: Developers can now generate new migration files directly from the command line using the
cargo run -- generate
command. This feature streamlines the creation of migration files and diffs, automatically handling the naming and placement within the specified migrations directory.Embedded Migrations: Migrations can be embedded into the codebase as constants, allowing for easy access and manipulation within the application logic. This is facilitated through the
embed_migrations!
macro, which simplifies the process of including migrations in the build.Example:
One-Way and Two-Way Migrations: The system supports both one-way and two-way migrations, providing flexibility in how changes are applied and rolled back. This ensures that developers can choose the most appropriate strategy for their database schema evolution.
Migration Application and Rollback: Applying and rolling back migrations is now as simple as running
cargo run -- run
andcargo run -- rollback
commands, respectively. These commands interact with the database directly, applying the necessary changes or reverting them as needed.Testing and Validation: The PR includes tests that validate the success of the migration commands, ensuring that the generated files are correct and that the commands interact with the database as expected.
Usage Examples
Gather Resources from the code base:
Set up CLI Binary:
Generate a New Migration:
This command will create new migration files in the specified directory.
Apply Migrations to the Database:
This will apply all pending migrations in the specified directory to the database.
Roll Back Applied Migrations:
This command will roll back migrations that have been applied, in reverse order.
List Migrations:
Conclusion
The enhancements to the database migration workflow represent a significant improvement in the developer experience.