[][src]Crate barrel

Powerful schema migration builder API in Rust.

Barrel is meant to make writing migrations for different databases as easy as possible. It has three primary models: the Migration which represents all changes and changes made on a database level, the Table and the Column.

When creating or altering tables a lambda which exposes &mut Table is provided for initialisation. Adding columns is then as easy as calling add_column(...) on the table.

Each column is statically typed and some types require some metadata in order to compile the migration (for example Varchar(255)). You can also provide default types and override encodings, nullability or uniqueness of columns. Some checks are performed at compile-time however most things (including) correct default values) are only checked at runtime.

Note Since version 0.3.0 it is required to provide a database backend in order to compile barrel.

The following code is a simple example of how to get going with barrel

extern crate barrel;

use barrel::*;
use barrel::backend::Pg;

fn main() {
    let mut m = Migration::new();
    m.create_table("users", |t| {
        t.add_column("name", Type::Varchar(255));
        t.add_column("age", Type::Integer);
        t.add_column("owns_plushy_sharks", Type::Boolean);
    });
}

barrel also supports more advanced types, such as Foreign(...) and Array(...) however currently doesn't support nested Array types on foreign keys (such as Array(Array(Foreign(...)))). Each column addition returns a Column object which can then be used to provide further configuration.

To generate SQL strings you have two options. If you just want to run the migration yourself simply run Migration::exec() where you provide a generic SqlGenerator type according to your database backend

// Example for pgsql
m.make::<Pg>();

Alternatively, if you're a library developer and you want to more easily embed barrel into your library you can simply implement the DatabaseExecutor trait for a type of yours that knows how to execute SQL. Running a migration with barrel is then super easy.

m.execute(executor);

In this case executor is your provided type which implements the required trait. You can read more about this in the connectors module docs.

Important: This crate is still early in development and the API might change rapidely between pre-release versions. I will try as best I can to include changes in the CHANGELOG but can not guarantee perfect coverage.

Also, if there is missing or invalid documentation for this crate, PR's are always welcome 💚

Re-exports

pub use integrations::*;
pub use table::Column;
pub use table::Table;
pub use table::TableMeta;
pub use migration::Migration;

Modules

backend

A backend module which provides a few generic traits to implement SQL generation for different databases.

connectors

A simple module meant for library developers

integrations

Include external integrations into frameworks and libraries

migration

Core migration creation handler

table

A module that represents tables and columns

types

Types constructor module

Enums

DatabaseChange

An enum set that represents a single change on a database

TableChange

An enum set that represents a single change on a table

Type

Type enum to specificy the type of an SQL column. NOTE: Not all types are supported by all database backends!