#migration #postgresql

macro reshape_helper

Helper library for Reshape

2 releases

0.1.1 Aug 3, 2022
0.1.0 Apr 19, 2022

#9 in #migrations

MIT license

7KB
74 lines

Reshape Rust helper

Test status badge

This is a Rust helper library for the automated, zero-downtime schema migration tool Reshape. To achieve zero-downtime migrations, Reshape requires that your application runs a simple query when it opens a connection to the database to select the right schema. This library automates that process and provides a macro to embed the right query directly in your application.

Installation

Add reshape_helper as a dependency to your Cargo.toml:

[dependencies]
reshape_helper = "0.1.1"

Usage

The library exposes a single macro which will find all your Reshape migration files and determine the right schema query to run. The macro will be evaluated at compile time and the query embedded directly in your binary, so you won't need to keep your migration files around at runtime.

The following is an example of how to use the library together with SQLx (but it works with any Postgres client):

use reshape_helper::schema_query
use sqlx::postgres::PgPoolOptions;

#[async_std::main]
async fn main() {
	let reshape_schema_query = schema_query!();

	let pool = PgPoolOptions::new()
		.after_connect(|conn| Box::pin(async move {
			conn.execute(reshape_schema_query).await?;
			Ok(())
		}))
		.connect("postgres://postgres@localhost:5432/db").await?;
}

By default, schema_query! will look for migration files in migrations/ but you can specify your own directories as well:

use reshape_helper::schema_query

fn main() {
	let reshape_schema_query = schema_query!(
		"src/users/migrations",
		"src/todos/migrations",
	);

	// Execute reshape_schema_query against your database...
}

License

Released under the MIT license.

Dependencies

~3MB
~62K SLoC