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 (
dir
feature):- Apply SQL migration files from a specified directory.
- Ensures migrations are executed in lexicographical order, based on filenames.
- Tracks applied migrations in a
libsql_migrations
table.
-
Content-based Migrations (
content
feature):- Apply a single SQL migration script provided as a string.
- Validate inputs to prevent empty migration IDs or scripts.
-
Remote Migrations (
remote
feature):- 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_migrations
table to track the status of migrations, including whether they were executed successfully.
- Maintains a
-
Asynchronous Execution:
- Leverages the
tokio
runtime 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
.sql
extension. - A
libsql_migrations
table 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_id
andmigration_script
are not empty. - Executes the migration and updates the
libsql_migrations
table.
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_migrations
table.
Migration Files
Directory-based Migrations
- Files should be plain
.sql
files. - 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
id
andurl
. - Example JSON response for remote migrations:
Error Handling
Errors in dir
Migrations
BaseError
: Underlyinglibsql
error.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
: Underlyinglibsql
error.InvalidInput
: Eithermigration_id
ormigration_script
is empty.
Errors in remote
Migrations
BaseError
: Underlyinglibsql
error.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.
TEXT PRIMARY KEY,
status BOOLEAN DEFAULT false,
exec_time DATE
);
(
id
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.