Code Monkey home page Code Monkey logo

refinery's Introduction

refinery Logo

Powerful SQL migration toolkit for Rust.

Crates.io docs.rs MIT licensed Build Status

refinery makes running migrations for different databases as easy as possible. It works by running your migrations on a provided database connection, either by embedding them on your Rust code, or via refinery_cli. Currently postgres, tokio-postgres , mysql, mysql_async and rusqlite are supported.
If you are using a driver that is not yet supported, namely SQLx you can run migrations providing a Config instead of the connection type, as Config impl's Migrate. You will still need to provide the postgres/mysql/rusqlite driver as a feature for Runner::run and tokio-postgres/mysql_async for Runner::run_async.
refinery works best with Barrel but you can also have your migrations in .sql files or use any other Rust crate for schema generation.

Usage

  • Add refinery to your Cargo.toml dependencies with the selected driver as feature eg: refinery = { version = "0.3", features = ["rusqlite"]}
  • Migrations can be defined in .sql files or Rust modules that must have a function called migration that returns a String.
  • Migrations, both .sql files and Rust modules must be named in the format V{1}__{2}.sql or V{1}__{2}.rs, where {1} represents the migration version and {2} the name.
  • Migrations can be run either by embedding them in your Rust code with embed_migrations and include_migration_mods macros, or via refinery_cli.

Example

use rusqlite::Connection;

mod embedded {
    use refinery::embed_migrations;
    embed_migrations!("./tests/sql_migrations");
}

fn main() {
    let mut conn = Connection::open_in_memory().unwrap();
    embedded::migrations::runner().run(&mut conn).unwrap();
}

For more examples, refer to the examples.

Implementation details

refinery works by creating a table that keeps all the applied migrations' versions and their metadata. When you run the migrations Runner, refinery compares the applied migrations with the the ones to be applied, checking for divergent and missing and executing unapplied migrations.
By default, refinery runs each migration in a single transaction. Alternatively, you can also configure refinery to wrap the entire execution of all migrations in a single transaction by setting set_grouped to true.

Rollback

refinery's design is based on flyway and so, shares its perspective on undo/rollback migrations. To undo/rollback a migration, you have to generate a new one and write specifically what you want to undo.

Compatibility

refinery aims to support stable Rust, the previous Rust version, and nightly.

Async

Starting with version 0.2 refinery supports tokio-postgres and mysql_async. To migrate async you have to call Runner's run_async. There are plans to support Tiberius when futures 0.3 support stabilizes. For Rusqlite, the best way to run migrations in an async context is to run them inside tokio's spawn_blocking for example.

Contributing

๐ŸŽˆ Thanks for your help improving the project! No contribution is too small and all contributions are valued, feel free to open Issues and submit Pull Requests

License

This project is licensed under the MIT license.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in refinery by you, shall be licensed as MIT, without any additional terms or conditions.

refinery's People

Contributors

akhilles avatar aloucks avatar c410-f3r avatar edmellum avatar elliottminns avatar foldu avatar jtdowney avatar jxs avatar letscodehu avatar mcobzarenco avatar robinhundt avatar someonetoignore avatar spacekookie avatar spebern avatar tmcw avatar walfie avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.