SQLX migrator
Migrator for writing sqlx migration using Rust instead of SQL
License | Crates Version | Docs |
---|---|---|
Supported Databases:
- PostgreSQL
- SQLite
- MySql
- Any
Installation
To use postgres migrator along with tokio-native-tls runtime you can configure Cargo.toml
= { = "0.5.0", =["postgres", "runtime-tokio-rustls"] }
Usage
To use sqlx_migrator first you need to implement OperationTrait trait to write your sqlx operation
use Error;
use OperationTrait;
// 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 MigrationTrait;
use OperationTrait;
use sqlx;
pub ;
Now at last you need to create migrator for your database to run migrations
use ;
use sqlx;
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 OperationTrait;
use sqlx;
pub ;