launchbadge / sqlx

🧰 The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, and SQLite.
Apache License 2.0
13.39k stars 1.27k forks source link

Migrations with namespaces #1356

Open lain-dono opened 3 years ago

lain-dono commented 3 years ago

When using a microservice architecture, it would be convenient to use migrations on a single database from several crates.

// in crate foo
fn migrate(...) {
  sqlx::migrate!(namespace = "foo")
}

// in crate bar
fn migrate(...) {
  sqlx::migrate!("db/migrations", namespace = "bar")
}

// somewhere among the tests
fn foo_bar() -> Pool {
  let pool = create_sqlx_pool();

  foo::migrate(pool);
  bar::migrate(pool);

  pool
}
damszew commented 2 years ago

This would be useful not only in microservices but also in modular monoliths. foo and bar could be independent modules that happened to work in the same app, using the same database.