5 unstable releases

0.2.0 Apr 15, 2024
0.1.1 Feb 5, 2024
0.1.0 Feb 5, 2024
0.0.2 Jan 31, 2024
0.0.1 Jan 31, 2024

#1624 in Database interfaces

Download history 7/week @ 2024-01-29 12/week @ 2024-02-05 7/week @ 2024-02-19 5/week @ 2024-02-26 143/week @ 2024-03-25 2/week @ 2024-04-01 147/week @ 2024-04-15

292 downloads per month

MIT license

14KB
251 lines

surrealdb_migration_engine

crates.io License: MIT

A simple and powerful migration engine for SurrealDB. All you need to get it working is the following:

#[derive(rust_embed::RustEmbed)]
#[folder = "migrations"]
struct MigrationFiles;

#[derive(rust_embed::RustEmbed)]
#[folder = "schema"]
struct SchemaFiles;

async fn main() {
    // create surealdb `client`

    surrealdb_migration_engine::run::<MigrationFiles,SchemaFiles>(&client).await?;

    // the rest of your code
}

How It Works

surrealdb_migration_engine works on two concepts Migrations and Schemas. Migrations are queries (changes) to an apply to an existing schema. Schemas are queries that set up the db structure. Schemas and migrations reside in their own directory with each file being numbered in order e.g. 0001_add_age_to_user_table.surql. Each of these directories is compiled with your binary with the help of the rust_embed crate. This means that the appropriate migrations or schema creation will happen at runtime. All migrations and schema changes are done in a single transaction, so if one fails, they all fail.

surrealdb_migration_engine creates a migrations table inside your database to track which migrations have ran. The logic flow works like this:

  • If the migrations table does not exist, run only the schema files, create a migrations table and add all of the current migration files to the table.
  • If the migrations table does exist, run any migration files that are not in the migrations table and insert those migrations in the migrations table.

Simple yet very expressive!

Uses

  • Include surrealdb_migration_engine in your application so whenever you run your application, the schema is always up to date.
  • Include surrealdb_migration_engine in a barebones executable that runs the necessary migrations or schema creation whenever you want them to occur.

Dependencies

~44–60MB
~1M SLoC