| Crates.io | libsql_migration |
| lib.rs | libsql_migration |
| version | 0.2.2 |
| created_at | 2025-04-21 16:04:00.928756+00 |
| updated_at | 2025-04-24 07:56:42.11893+00 |
| description | A simple SQL migration tool for libsql databases |
| homepage | |
| repository | https://github.com/prashant1k99/libsql_migration |
| max_upload_size | |
| id | 1642913 |
| size | 133,350 |
libsql_migration provides a simple migration mechanism for libsql databases. This crate is designed to help developers manage database migrations efficiently by offering support for directory-based migrations, remote migrations, and content-based migrations. Additionally, it tracks the status of applied migrations.
Directory-based Migrations (dir feature):
libsql_migrations table.Content-based Migrations (content feature):
Remote Migrations (remote feature):
Error Handling:
Migration Tracking:
libsql_migrations table to track the status of migrations, including whether they were executed successfully.Asynchronous Execution:
tokio runtime for asynchronous operations, ensuring high performance.To use libsql_migration, add the following to your Cargo.toml:
[dependencies]
libsql_migration = { git = "https://github.com/prashant1k99/libsql_migration.git" }
tokio = { version = "1", features = ["full"] }
libsql = "0.1" # Replace with the appropriate version
The dir feature allows you to apply migrations from a directory containing .sql files.
use libsql_migration::dir::migrate;
use libsql_migration::errors::LibsqlDirMigratorError;
use libsql::Builder;
use std::path::PathBuf;
#[tokio::main]
async fn main() -> Result<(), LibsqlDirMigratorError> {
// Connect to your database
let db = Builder::new_local("my_database.db").build().await.unwrap();
let conn = db.connect().unwrap();
// Specify the path to your migration files
let migrations_folder = PathBuf::from("./migrations");
// Run the migrations
match migrate(&conn, migrations_folder).await {
Ok(applied) => {
if applied {
println!("Migrations applied successfully.");
} else {
println!("No new migrations to apply.");
}
}
Err(e) => {
eprintln!("Migration failed: {}", e);
return Err(e);
}
}
Ok(())
}
.sql extension.libsql_migrations table is created to track applied migrations.The content feature allows you to execute a migration script provided as a string.
use libsql_migration::content::migrate;
use libsql_migration::errors::LibsqlContentMigratorError;
use libsql::Builder;
#[tokio::main]
async fn main() -> Result<(), LibsqlContentMigratorError> {
// Connect to your database
let db = Builder::new_local("my_database.db").build().await.unwrap();
let conn = db.connect().unwrap();
// Define the migration script and its ID
let migration_id = "20230423_initial_setup".to_string();
let migration_script = r#"
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
"#.to_string();
// Run the migration
match migrate(&conn, migration_id, migration_script).await {
Ok(result) => println!("Migration result: {:?}", result),
Err(e) => eprintln!("Migration failed: {}", e),
}
Ok(())
}
migration_id and migration_script are not empty.libsql_migrations table.The remote feature allows you to fetch and apply migrations from a remote source.
use libsql_migration::remote::migrate;
use libsql_migration::errors::LibsqlRemoteMigratorError;
use libsql::Builder;
#[tokio::main]
async fn main() -> Result<(), LibsqlRemoteMigratorError> {
// Connect to your database
let db = Builder::new_local("my_database.db").build().await.unwrap();
let conn = db.connect().unwrap();
// Define the remote URL for migrations
let remote_url = "https://example.com/migrations".to_string();
// Run the migrations
match migrate(&conn, remote_url).await {
Ok(applied) => {
if applied {
println!("Migrations applied successfully.");
} else {
println!("No new migrations to apply.");
}
}
Err(e) => eprintln!("Migration failed: {}", e),
}
Ok(())
}
libsql_migrations table..sql files.Example file structure:
migrations/
├── 0001_initial.sql
├── 0002_add_users_table.sql
├── 0003_add_orders_table.sql
id and url.[
{ "id": "0001_initial", "url": "https://example.com/0001_initial.sql" },
{
"id": "0002_add_users_table",
"url": "https://example.com/0002_add_users_table.sql"
}
]
dir MigrationsBaseError: Underlying libsql error.MigrationDirNotFound: The specified directory does not exist.InvalidMigrationPath: The provided path is not valid.ErrorWhileGettingSQLFiles: Error occurred while traversing the folder.content MigrationsBaseError: Underlying libsql error.InvalidInput: Either migration_id or migration_script is empty.remote MigrationsBaseError: Underlying libsql error.MigrationUrlNotValid: The provided URL is invalid.ReqwestError: Error occurred during HTTP request.libsql_migrations TableThe migration tracking table is created automatically if it does not exist.
CREATE TABLE IF NOT EXISTS libsql_migrations (
id TEXT PRIMARY KEY,
status BOOLEAN DEFAULT false,
exec_time DATE
);
id: Unique identifier for the migration.status: Indicates whether the migration was executed successfully.exec_time: Timestamp of execution.Add tests to validate the library functionality. Use the following command to run the tests:
cargo test
Contributions are welcome! If you have suggestions or find a bug, please open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
Special thanks to the open-source community for their contributions and support.