flyway-rs
flyway-rs is a collection of Rust crates for loading and executing database migrations, inspired by Flyway in Java.
It was created as an alternative to refinery, because refinery has a closed driver architecture — the refinery::Migration::applied(...) method is not public, preventing external crates from implementing the refinery::AsyncMigrate trait. See this issue for more details.
Features
- Multi-database support: MySQL, PostgreSQL, SQLite, MSSql, TDengine
- Compile-time migration embedding: SQL files are parsed and embedded into the binary at compile time via procedural macros — no runtime file I/O needed
- Version-based migration management: Automatic tracking of applied migrations with version state management
- Transaction support: Each migration runs within its own database transaction, with automatic rollback on failure
- Fail-continue mode: Optionally continue executing subsequent migrations when a single migration fails
- SQL statement annotations: Support
--! may_fail: trueannotations to allow individual SQL statements to fail without aborting the migration - Multi-database runtime dispatch: Dynamically select migration scripts based on the detected database type at runtime
- Pluggable driver architecture: Easy to implement custom database drivers via the
MigrationStateManagerandMigrationExecutortraits
Crate Structure
| Crate | Description |
|---|---|
flyway |
Core crate. Contains the migration runner, traits (MigrationStore, MigrationStateManager, MigrationExecutor), and re-exports macros from sub-crates. |
flyway-rbatis |
Database driver implementation using Rbatis. Supports MySQL, PostgreSQL, SQLite, MSSql, and TDengine. |
flyway-codegen |
Procedural macro crate. Provides the #[migrations(...)] attribute macro for compile-time migration loading. |
flyway-sql-changelog |
SQL file parsing library. Handles SQL splitting, checksum calculation, and statement annotation parsing. |
Quick Start
1. Add Dependencies
[]
= "0.5.0"
= "0.5.0"
= "4.8"
= "4.8" # or rbdc-pg, rbdc-tdengine, etc.
= { = "1", = ["full"] }
2. Create Migration Files
Place SQL files in a directory following the naming convention V<version>_<description>.sql:
migrations/
├── V1_Create_DeviceData.sql
├── V2_Create_PatientUseDevice.sql
├── V3__Create_VitalSign.sql
└── V6__Add_PatientNo.sql
Each file can contain multiple SQL statements separated by semicolons.
3. Write Migration Code
use Arc;
use RBatis;
use MysqlDriver;
use ;
use RbatisMigrationDriver;
// Load migration SQL files at compile time
async
async
Multi-Database Support
flyway-rs supports runtime database type detection and automatic migration script dispatch. Organize migration scripts by database dialect:
migrations/
├── mysql/
│ ├── V1_Create_DeviceData.sql
│ └── V2_Create_PatientUseDevice.sql
├── postgres/
│ ├── V1_Create_DeviceData.sql
│ └── V2_Create_PatientUseDevice.sql
└── taos/
├── V1_Create_DeviceData.sql
└── V2_Create_PatientUseDevice.sql
Define separate migration stores for each database type:
use migrations;
Then use RbatisMigrationDriver::driver_type() to detect the database at runtime and select the appropriate migration store. See the multi_db example for a complete implementation.
SQL Annotations
You can annotate SQL statements using special comments to control error handling:
--! may_fail: true
(patient_no);
When may_fail: true is set, the migration will continue even if that specific statement fails.
Migration State Table
flyway-rs automatically creates a migration tracking table (default name: flyway_migrations) in your database to track which versions have been applied. The table schema adapts to each database dialect automatically.
Running Tests
Examples
See the example directory for complete working examples:
mysql.rs— MySQL migration exampletaos.rs— TDengine migration examplemulti_db.rs— Multi-database runtime dispatch example
License
This project is licensed under the MIT License.