libsql_migration
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.
Features
-
Directory-based Migrations (
dirfeature):- Apply SQL migration files from a specified directory.
- Ensures migrations are executed in lexicographical order, based on filenames.
- Tracks applied migrations in a
libsql_migrationstable.
-
Content-based Migrations (
contentfeature):- Apply a single SQL migration script provided as a string.
- Validate inputs to prevent empty migration IDs or scripts.
-
Remote Migrations (
remotefeature):- Fetch and apply migrations from a remote source (e.g., a URL).
- Supports sorting and executing migrations in the correct order.
-
Error Handling:
- Handles errors gracefully with detailed error messages.
- Provides specific error types for different migration contexts (e.g., directory, content, and remote).
-
Migration Tracking:
- Maintains a
libsql_migrationstable to track the status of migrations, including whether they were executed successfully.
- Maintains a
-
Asynchronous Execution:
- Leverages the
tokioruntime for asynchronous operations, ensuring high performance.
- Leverages the
Installation
To use libsql_migration, add the following to your Cargo.toml:
[]
= { = "https://github.com/prashant1k99/libsql_migration.git" }
= { = "1", = ["full"] }
= "0.1" # Replace with the appropriate version
Usage
Directory-based Migrations
The dir feature allows you to apply migrations from a directory containing .sql files.
use migrate;
use LibsqlDirMigratorError;
use Builder;
use PathBuf;
async
Behavior:
- Files are executed in lexicographical order.
- Files must have a
.sqlextension. - A
libsql_migrationstable is created to track applied migrations.
Content-based Migrations
The content feature allows you to execute a migration script provided as a string.
use migrate;
use LibsqlContentMigratorError;
use Builder;
async
Behavior:
- Validates that
migration_idandmigration_scriptare not empty. - Executes the migration and updates the
libsql_migrationstable.
Remote Migrations
The remote feature allows you to fetch and apply migrations from a remote source.
use migrate;
use LibsqlRemoteMigratorError;
use Builder;
async
Behavior:
- Fetches a list of migration files from the provided URL.
- Executes migrations in order and updates the
libsql_migrationstable.
Migration Files
Directory-based Migrations
- Files should be plain
.sqlfiles. - Located in a specified folder.
- Executed in lexicographical order.
Example file structure:
migrations/
├── 0001_initial.sql
├── 0002_add_users_table.sql
├── 0003_add_orders_table.sql
Remote Migrations
- Remote migrations should follow a structure where each migration file has an
idandurl. - Example JSON response for remote migrations:
Error Handling
Errors in dir Migrations
BaseError: Underlyinglibsqlerror.MigrationDirNotFound: The specified directory does not exist.InvalidMigrationPath: The provided path is not valid.ErrorWhileGettingSQLFiles: Error occurred while traversing the folder.
Errors in content Migrations
BaseError: Underlyinglibsqlerror.InvalidInput: Eithermigration_idormigration_scriptis empty.
Errors in remote Migrations
BaseError: Underlyinglibsqlerror.MigrationUrlNotValid: The provided URL is invalid.ReqwestError: Error occurred during HTTP request.
Design Details
libsql_migrations Table
The migration tracking table is created automatically if it does not exist.
(
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.
Development
Running Tests
Add tests to validate the library functionality. Use the following command to run the tests:
Contributing
Contributions are welcome! If you have suggestions or find a bug, please open an issue or submit a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Acknowledgments
Special thanks to the open-source community for their contributions and support.