1
0
Fork
You've already forked rusqlite_migration
0
Mirror of the GitHub repo https://cj.rs/rusqlite_migration/
  • Rust 99.4%
  • Shell 0.6%
Find a file
2026年01月19日 22:01:21 +00:00
.cargo test: stop installing an old cargo mutants in CI 2023年12月10日 00:57:28 +00:00
.github ci: fix yaml typo 2025年11月22日 13:04:14 +00:00
examples chore: update tokio-rusqlite-new 2026年01月19日 22:01:21 +00:00
rusqlite_migration refactor: move foreign key checks in their own module and struct 2026年01月19日 22:01:21 +00:00
rusqlite_migration_benches chore: update rusqlite_migration and bump version 2026年01月19日 22:01:21 +00:00
rusqlite_migration_tests chore: update rusqlite_migration and bump version 2026年01月19日 22:01:21 +00:00
.editorconfig style: add editorconfig file 2025年12月25日 20:32:44 +00:00
.gitignore Fix clippy error 2024年03月17日 12:20:50 +01:00
Cargo.lock chore: update tokio-rusqlite-new 2026年01月19日 22:01:21 +00:00
Cargo.toml chore: update rusqlite_migration and bump version 2026年01月19日 22:01:21 +00:00
CHANGELOG.md doc: refine changelog 2026年01月19日 22:01:21 +00:00
clippy.toml doc: update the example to use lazylock instead of lazy_static 2025年02月01日 20:49:37 +00:00
LICENSE.txt First ideas 2020年11月07日 20:10:02 +00:00
README.md ci: introduce dependency cool down 2025年11月22日 12:58:10 +00:00

Rusqlite Migration is a performant and simple 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().

userusqlite::{params,Connection};userusqlite_migration::{Migrations,M};// 1️⃣ Define migrations
constMIGRATIONS_SLICE: &[M<'_>]=&[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;"),
];constMIGRATIONS: Migrations<'_>=Migrations::from_slice(MIGRATIONS_SLICE);fn main(){letmutconn=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(&mutconn).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:

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(){letmigrations=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

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.

Minimum Supported Rust Version (MSRV)

This crate extends rusqlite and as such is tightly integrated with it. Thus, it supports the same MSRV as rusqlite. At the time of writing, this means:

Latest stable Rust version at the time of release. It might compile with older versions.

Limits

  1. Since this crate uses the user_version field, if your program or any other library changes it, this library will behave in an unspecified way: it may return an error, apply the wrong set of migrations, do nothing at all...

  2. The user_version field is effectively a i32, so there is a theoretical limit (about two billion) on the number of migrations that can be applied by this library. You are likely to hit memory limits well before that though, so in practice, you can think of the number of migrations as limitless. And you would need to create 10 000 new migrations, every day, for over 5 centuries, before getting close to the limit.

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.