9 releases

0.1.8 May 7, 2024
0.1.7 Jan 1, 2024
0.1.6 Dec 30, 2023

#1113 in Database interfaces

Download history 99/week @ 2024-03-28 2/week @ 2024-04-04 32/week @ 2024-04-11 77/week @ 2024-04-18 42/week @ 2024-04-25 171/week @ 2024-05-02 37/week @ 2024-05-09 24/week @ 2024-05-16 33/week @ 2024-05-23 30/week @ 2024-05-30 177/week @ 2024-06-06 59/week @ 2024-06-13 21/week @ 2024-06-20 15/week @ 2024-06-27 22/week @ 2024-07-04 28/week @ 2024-07-11

98 downloads per month

Apache-2.0

39KB
698 lines

SurrealDB Migrator

Migrator library for SurrealDB.

Example

cargo add surrealdb-migrator

Using code for migration scripts

use surrealdb_migrator::{Migrations, M};

let db = surrealdb::engine::any::connect("mem://");

let migrations = Migrations::new(vec![
    M::up("DEFINE TABLE animal; DEFINE FIELD name ON animal TYPE string;").down("REMOVE TABLE user;"),
    M::up("DEFINE TABLE food; DEFINE FIELD name ON food TYPE string;").down("REMOVE TABLE food;"),
]);

// Go to the latest version
migrations.to_latest(&db).unwrap();

// Go to a specific version
migrations.to_version(&db, 0).unwrap();

Using files for migration script

The migrations are loaded and stored in the binary. from-directory feature flags needs to be enabled.

The migration directory pointed to by include_dir!() must contain subdirectories in accordance with the given pattern: {usize id indicating the order}-{convenient migration name}

Those directories must contain at least an up.surql file containing a valid upward migration. They can also contain a down.surql file containing a downward migration.

Example structure

migrations
├── 01-friend_car
│  └── up.surql
├── 02-add_birthday_column
│  └── up.surql
└── 03-add_animal_table
    ├── down.surql
    └── up.surql
use include_dir::{include_dir, Dir}; // cargo add include_dir
static MIGRATION_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/migrations");

let migrations = Migrations::from_directory(&MIGRATION_DIR).unwrap();
migrations.to_latest(&db).await?;

LICENSE

Apache License

Acknowledgments

Thanks to rusqlite_migration where the code for surrealdb-migrator is inspired from.

Dependencies

~58MB
~1M SLoC