Monarch-DB
Monarch-DB is a lightweight rusqlite database migration tool designed to run whenever the first connection in an app opens. It provides a simple, reliable way to manage SQLite database schema evolution in Rust applications.
Installation
Add this to your Cargo.toml:
[]
= "0.1"
# Optional: Enable serde support for configuration
= { = "0.1", = ["serde"] }
Quick Start
Static Configuration
Use static configuration when you want to embed migrations directly in your binary:
use ;
Directory-Based Configuration
Use directory-based configuration when you want to manage migrations as separate files:
use ;
With migration files in ./migrations/:
migrations/
├── 001_create_users.sql
├── 002_create_posts.sql
└── 003_add_indexes.sql
001_create_users.sql:
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE,
email TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
002_create_posts.sql:
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
title TEXT NOT NULL,
content TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id)
);
003_add_indexes.sql:
(username);
(user_id);
Advanced Usage
In-Memory Databases
Create temporary databases perfect for testing:
let connection = monarch_db.open_in_memory?;
Using with Include Files
For static configuration, you can use include_str! for better organization:
let config = StaticMonarchConfiguration ;
Configuration with Serde
Enable the serde feature to deserialize configurations:
use Deserialize;
let config: AppConfig = from_str?;
let monarch_db = from_configuration?;
let connection = monarch_db.create_connection?;
Version Management
Check the current schema version:
let current_version = monarch_db.current_version;
println!;
Applying Migrations to Existing Connections
You can apply migrations to an existing connection:
use Connection;
let raw_connection = open?;
let migrated_connection = monarch_db.migrations?;
Command Line Interface
Monarch-DB includes a command-line tool for running migrations outside of your application code. This is useful for deployment scripts, CI/CD pipelines, or manual database management.
Migrate Command
Apply all pending migrations to a database:
Arguments:
migrations_dir- Path to directory containing migration filesapp_name- Name of the application (used for version tracking)sqlite_url- SQLite database URL (file path or:memory:)
Examples:
# Apply migrations to a file database
# Apply migrations to an in-memory database
# Apply migrations for a specific environment
Sample Output:
Running migrations...
Migrations directory: ./migrations
Application name: my_app
Database: ./database.db
Found 3 migration(s)
Migration completed successfully!
Current schema version: 3
Database is up to date.
Version Command
Check the current migration status without applying changes:
Examples:
# Check migration status
# Check status of a database that doesn't exist yet
Sample Output:
Checking migration version...
Migrations directory: ./migrations
Application name: my_app
Database: ./database.db
Available migrations: 5
Current schema version: 3
Migrations pending: 3 -> 5 (2 new migration(s))
Testing
Run the test suite:
# Run all tests (unit + integration)
# Run only unit tests
# Run only integration tests
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
License
This project is licensed under the MIT License - see the LICENSE file for details.