- Rust 100%
Barrel is a flexible toolkit for writing SQL schema migrations as Rust code.
Migrations consist of individual changes to tables, columns, indices, foreign keys, and constraints. It provides composable type builders for all of these changes. Finally a specific backend type (which must be provided as a feature) handle SQL code generation.
Barrel aims to cover all existing SQL features and throws errors (currently panics) when a specific feature doesn't exist for a given database implementation.
Get started
Simply add barrel to your project:
barrel = { version = "0.7", features = [ "..." ] }
Possible feature flags are: sqlite3, mysql, mssql, and pg. You can then get started on writing your first SQL migration in Rust:
usebarrel::{types,Migration};usebarrel::backend::Pg;fn main(){letmutm=Migration::new();m.create_table("users",|t|{t.add_column("name",types::varchar(255));t.add_column("age",types::integer());t.add_column("owns_plushy_sharks",types::boolean());});println!("{}",m.make::<Pg>());}Note: some generator backends have gotten more attention than others. While I have opted to merge any backend that was offered as a PR, personally I have only ever used Barrel with the sqlite3 backend, which is thus the most well tested. Your mileage may vary!
Upgrade guide & maintenance notice
This crate has been quasi unmaintained from 2021 to 2025 (I wonder what happened 🤔) with multiple versions fragmenting across personal forks. However due to my day job I have been able to pick up development again. A genuine and massive THANK YOU to everyone who has contributed small fixes over the years of my (削除) burnout (削除ここまで) inactivity! 💚💚💚
The version bump from the last "stable" release 0.7.0 to 0.10.0 is meant to indicate a gap in development and represents the chance to fix some long-standing issues both on the API side and the generator internals.
I will keep this README updated with changes that accumulate which may break your migrations, and where possible a migration (hehe) tool will be provided.
License
barrel is free software: you can redistribute it and/or modify it under the terms of the MIT Public License.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MIT Public License for more details.