SQLX migrator
Migrator library for writing sqlx migration using Rust instead of SQL
License | Crates Version | Docs |
---|---|---|
Supported Databases:
- PostgreSQL
- SQLite
- MySql
- Any
Installation
To use sqlx migrator you can configure Cargo.toml as shown below according to your requirements
= { = "0.9.1", =["postgres"] }
OR
= { = "0.9.1", =["mysql"] }
OR
= { = "0.9.1", =["sqlite"] }
OR
= { = "0.9.1", =[
"any",
# Plus any one of above database driver
] }
Usage
To use sqlx_migrator first you need to implement Operation trait to write your sqlx operation below are examples for using postgres based migration
use Error;
use Operation;
// Its better to use sqlx imported from sqlx_migrator
use sqlx;
pub ;
After creation of operation you can implement Migration struct to create single migration
use Error;
use Migration;
use Operation;
use sqlx;
pub ;
Now at last you need to create migrator for your database to run migrations
use ;
use Postgres;
async
Now you can use two ways to run migrator either directly running migration or creating cli from migrator For directly run
// use apply all to apply all pending migration
migrator.apply_all.await.unwrap;
// or use revert all to revert all applied migrations
migrator.revert_all.await.unwrap;
Or you can create cli
.await.unwrap;
run
Migrate from sqlx default sql based migration
To migrate from sqlx sql based migration you have two alternative:
- Write all sql migration as rust operation
- Write single rust based operation which apply and revert all sqlx sql based migration
Option: 1
Can be easily applied by following above usage docs where you only need to write your sql based migration as sqlx query Then you can create cli for migrator
.await.unwrap;
run
and run fake apply cli command
<COMMAND_NAME> apply --fake
which actually doesn't apply migration query but only update migration table
Option: 2
To run all sqlx sql based migration as single command create new operation
use Error;
use Operation;
use sqlx;
pub ;