# Rusqlite Migration [![docs.rs](https://img.shields.io/docsrs/rusqlite_migration)](https://docs.rs/rusqlite_migration) [![Crates.io](https://img.shields.io/crates/v/rusqlite_migration)](https://crates.io/crates/rusqlite_migration) [![unsafe forbidden](https://img.shields.io/badge/unsafe-forbidden-success.svg)](https://github.com/rust-secure-code/safety-dance/) [![dependency status](https://deps.rs/crate/rusqlite_migration/1.0.2/status.svg)](https://deps.rs/crate/rusqlite_migration) [![Coveralls](https://img.shields.io/coverallsCoverage/github/cljoly/rusqlite_migration)](https://coveralls.io/github/cljoly/rusqlite_migration)

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

Example

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

``` rust 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::openinmemory().unwrap();

// Apply some PRAGMA, often better to do it outside of migrations conn.pragmaupdate(None, "journalmode", &"WAL").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!(…)) - 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:

``` rust

[test]

fn migrationstest() { assert!(MIGRATIONS.validate().isok()); } ```

Contributing

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

Please note that if you want to change something in the main README.md file, you likely need to edit the top comment in rusqlite_migration/src/lib.rs and then run cargo sync-readme from the rusqlite_migration subdirectory (the one that contain src). This command copies the content of the top comment to the README.md file, keeping lib.rs and the README.md sync. The only exception to this is when you want to edit something outside of the <!-- cargo-sync-readme start --> and <!-- cargo-sync-readme end --> markers.

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.