#sqlite #database-migrations #database-schema #rusqlite #database-table #schema-version

rusqlite_migration

Simple schema migration library for rusqlite using user_version instead of an SQL table to maintain the current schema version

22 releases (7 stable)

1.3.1 Oct 4, 2024
1.3.0-alpha-without-tokio… Aug 11, 2024
1.2.0 Mar 24, 2024
1.1.0 Dec 17, 2023
0.2.0 Nov 15, 2020

#67 in Database interfaces

Download history 3978/week @ 2024-07-17 4115/week @ 2024-07-24 4423/week @ 2024-07-31 4353/week @ 2024-08-07 4114/week @ 2024-08-14 3886/week @ 2024-08-21 4607/week @ 2024-08-28 4443/week @ 2024-09-04 5190/week @ 2024-09-11 4232/week @ 2024-09-18 4754/week @ 2024-09-25 6535/week @ 2024-10-02 4470/week @ 2024-10-09 4588/week @ 2024-10-16 5004/week @ 2024-10-23 5348/week @ 2024-10-30

20,394 downloads per month
Used in 33 crates (24 directly)

Apache-2.0

95KB
1.5K SLoC

Rusqlite Migration

docs.rs Crates.io unsafe forbidden dependency status Coveralls

Rusqlite Migration is a simple and performant schema migration library for rusqlite.

  • Performance:
    • Fast database opening: to keep track of the current migration state, most tools create one or more tables in the database. These tables require parsing by SQLite and are queried with SQL statements. This library uses the user_version value instead. It’s much lighter as it is just an integer at a fixed offset in the SQLite file.
    • Fast compilation: this crate is very small and does not use macros to define the migrations.
  • Simplicity: this crate strives for simplicity. Just define a set of SQL statements as strings in your Rust code. Add more SQL statements over time as needed. No external CLI required. Additionally, rusqlite_migration works especially well with other small libraries complementing rusqlite, like serde_rusqlite.

Example

Here, we define SQL statements to run with Migrations::new() and run these (if necessary) with Migrations::to_latest().

use rusqlite::{params, Connection};
use rusqlite_migration::{Migrations, M};

// 1️⃣ Define migrations
let migrations = Migrations::new(vec![
    M::up("CREATE TABLE friend(name TEXT NOT NULL);"),
    // In the future, add more migrations here:
    //M::up("ALTER TABLE friend ADD COLUMN email TEXT;"),
]);

let mut conn = Connection::open_in_memory().unwrap();

// Apply some PRAGMA, often better to do it outside of migrations
conn.pragma_update_and_check(None, "journal_mode", &"WAL", |_| Ok(())).unwrap();

// 2️⃣ Update the database schema, atomically
migrations.to_latest(&mut conn).unwrap();

// 3️⃣ Use the database 🥳
conn.execute("INSERT INTO friend (name) VALUES (?1)", params!["John"])
    .unwrap();

Please see the examples folder for more, in particular:

  • async migrations in the quick_start_async.rs file
  • migrations with multiple SQL statements (using for instance r#"…" or include_str!())
  • migrations defined from a directory with SQL files
  • use of lazy_static
  • migrations to previous versions (downward migrations)

I’ve also made a cheatsheet of SQLite pragma for improved performance and consistency.

Built-in tests

To test that the migrations are working, you can add this in your test module:

#[test]
fn migrations_test() {
    assert!(MIGRATIONS.validate().is_ok());
}

The migrations object is also suitable for serialisation with insta, using the Debug serialisation. You can store a snapshot of your migrations like this:

#[test]
fn migrations_insta_snapshot() {
    let migrations = Migrations::new(vec![
        // ...
    ]);
    insta::assert_debug_snapshot!(migrations);
}

Optional Features

Rusqlite_migration provides several Cargo features. They are:

  • from-directory: enable loading migrations from *.sql files in a given directory
  • alpha-async-tokio-rusqlite: enable support for async migrations with tokio-rusqlite. As the name implies, there are no API stability guarantees on this feature.

Active Users

Crates.io Downloads Crates.io Downloads (recent)

This crate is actively used in a number of projects. You can find up-to-date list of those on:

A number of contributors are also reporting issues as they arise, another indicator of active use.

Contributing

Contributions (documentation or code improvements in particular) are welcome, see contributing!

We use various tools for testing that you may find helpful to install locally (e.g. to fix failing CI checks):

Acknowledgments

I would like to thank all the contributors, as well as the authors of the dependencies this crate uses.

Thanks to Migadu for offering a discounted service to support this project. It is not an endorsement by Migadu though.

Dependencies

~28MB
~442K SLoC